PageRenderTime 151ms CodeModel.GetById 39ms RepoModel.GetById 0ms app.codeStats 2ms

/templates/pdf/tcpdf/tcpdf.php

https://github.com/chnm/anthologize
PHP | 13934 lines | 8532 code | 515 blank | 4887 comment | 1666 complexity | 827e5f8b99d524f347b43c92c5033235 MD5 | raw file
Possible License(s): BSD-2-Clause, GPL-3.0
  1. <?php
  2. //============================================================+
  3. // File name : tcpdf.php
  4. // Version : 5.9.109
  5. // Begin : 2002-08-03
  6. // Last Update : 2011-08-12
  7. // Author : Nicola Asuni - Tecnick.com S.r.l - Via Della Pace, 11 - 09044 - Quartucciu (CA) - ITALY - www.tecnick.com - info@tecnick.com
  8. // License : http://www.tecnick.com/pagefiles/tcpdf/LICENSE.TXT GNU-LGPLv3 + YOU CAN'T REMOVE ANY TCPDF COPYRIGHT NOTICE OR LINK FROM THE GENERATED PDF DOCUMENTS.
  9. // -------------------------------------------------------------------
  10. // Copyright (C) 2002-2011 Nicola Asuni - Tecnick.com S.r.l.
  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. Additionally,
  18. // YOU CAN'T REMOVE ANY TCPDF COPYRIGHT NOTICE OR LINK FROM THE
  19. // GENERATED PDF DOCUMENTS.
  20. //
  21. // TCPDF is distributed in the hope that it will be useful, but
  22. // WITHOUT ANY WARRANTY; without even the implied warranty of
  23. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  24. // See the GNU Lesser General Public License for more details.
  25. //
  26. // You should have received a copy of the License
  27. // along with TCPDF. If not, see
  28. // <http://www.tecnick.com/pagefiles/tcpdf/LICENSE.TXT>.
  29. //
  30. // See LICENSE.TXT file for more information.
  31. // -------------------------------------------------------------------
  32. //
  33. // Description :
  34. // This is a PHP class for generating PDF documents without requiring external extensions.
  35. //
  36. // NOTE:
  37. // This class was originally derived in 2002 from the Public
  38. // Domain FPDF class by Olivier Plathey (http://www.fpdf.org),
  39. // but now is almost entirely rewritten and contains thousands of
  40. // new lines of code and hundreds new features.
  41. //
  42. // Main features:
  43. // * no external libraries are required for the basic functions;
  44. // * all standard page formats, custom page formats, custom margins and units of measure;
  45. // * UTF-8 Unicode and Right-To-Left languages;
  46. // * TrueTypeUnicode, OpenTypeUnicode, TrueType, OpenType, Type1 and CID-0 fonts;
  47. // * font subsetting;
  48. // * methods to publish some XHTML + CSS code, Javascript and Forms;
  49. // * images, graphic (geometric figures) and transformation methods;
  50. // * 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)
  51. // * 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, QR-Code, PDF417;
  52. // * JPEG and PNG ICC profiles, Grayscale, RGB, CMYK, Spot Colors and Transparencies;
  53. // * automatic page header and footer management;
  54. // * document encryption up to 256 bit and digital signature certifications;
  55. // * transactions to UNDO commands;
  56. // * PDF annotations, including links, text and file attachments;
  57. // * text rendering modes (fill, stroke and clipping);
  58. // * multiple columns mode;
  59. // * no-write page regions;
  60. // * bookmarks, named destinations and table of content;
  61. // * text hyphenation;
  62. // * text stretching and spacing (tracking/kerning);
  63. // * automatic page break, line break and text alignments including justification;
  64. // * automatic page numbering and page groups;
  65. // * move and delete pages;
  66. // * page compression (requires php-zlib extension);
  67. // * XOBject Templates;
  68. // * Layers and object visibility.
  69. //
  70. // -----------------------------------------------------------
  71. // THANKS TO:
  72. //
  73. // Olivier Plathey (http://www.fpdf.org) for original FPDF.
  74. // Efthimios Mavrogeorgiadis (emavro@yahoo.com) for suggestions on RTL language support.
  75. // Klemen Vodopivec (http://www.fpdf.de/downloads/addons/37/) for Encryption algorithm.
  76. // Warren Sherliker (wsherliker@gmail.com) for better image handling.
  77. // dullus for text Justification.
  78. // Bob Vincent (pillarsdotnet@users.sourceforge.net) for <li> value attribute.
  79. // Patrick Benny for text stretch suggestion on Cell().
  80. // Johannes Güntert for JavaScript support.
  81. // Denis Van Nuffelen for Dynamic Form.
  82. // Jacek Czekaj for multibyte justification
  83. // Anthony Ferrara for the reintroduction of legacy image methods.
  84. // Sourceforge user 1707880 (hucste) for line-trough mode.
  85. // Larry Stanbery for page groups.
  86. // Martin Hall-May for transparency.
  87. // Aaron C. Spike for Polycurve method.
  88. // Mohamad Ali Golkar, Saleh AlMatrafe, Charles Abbott for Arabic and Persian support.
  89. // Moritz Wagner and Andreas Wurmser for graphic functions.
  90. // Andrew Whitehead for core fonts support.
  91. // Esteban Joël Marín for OpenType font conversion.
  92. // Teus Hagen for several suggestions and fixes.
  93. // Yukihiro Nakadaira for CID-0 CJK fonts fixes.
  94. // Kosmas Papachristos for some CSS improvements.
  95. // Marcel Partap for some fixes.
  96. // Won Kyu Park for several suggestions, fixes and patches.
  97. // Dominik Dzienia for QR-code support.
  98. // Laurent Minguet for some suggestions.
  99. // Christian Deligant for some suggestions and fixes.
  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, OpenTypeUnicode, TrueType, OpenType, 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, 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. * </ul>
  135. * Tools to encode your unicode fonts are on fonts/utils directory.</p>
  136. * @package com.tecnick.tcpdf
  137. * @author Nicola Asuni
  138. * @version 5.9.109
  139. */
  140. // Main configuration file. Define the K_TCPDF_EXTERNAL_CONFIG constant to skip this file.
  141. require_once(dirname(__FILE__).'/config/tcpdf_config.php');
  142. /**
  143. * @class TCPDF
  144. * PHP class for generating PDF documents without requiring external extensions.
  145. * 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>
  146. * @package com.tecnick.tcpdf
  147. * @brief PHP class for generating PDF documents without requiring external extensions.
  148. * @version 5.9.109
  149. * @author Nicola Asuni - info@tecnick.com
  150. */
  151. class TCPDF {
  152. // private properties
  153. /**
  154. * Current TCPDF version.
  155. * @private
  156. */
  157. private $tcpdf_version = '5.9.109';
  158. // Protected properties
  159. /**
  160. * Current page number.
  161. * @protected
  162. */
  163. protected $page;
  164. /**
  165. * Current object number.
  166. * @protected
  167. */
  168. protected $n;
  169. /**
  170. * Array of object offsets.
  171. * @protected
  172. */
  173. protected $offsets;
  174. /**
  175. * Buffer holding in-memory PDF.
  176. * @protected
  177. */
  178. protected $buffer;
  179. /**
  180. * Array containing pages.
  181. * @protected
  182. */
  183. protected $pages = array();
  184. /**
  185. * Current document state.
  186. * @protected
  187. */
  188. protected $state;
  189. /**
  190. * Compression flag.
  191. * @protected
  192. */
  193. protected $compress;
  194. /**
  195. * Current page orientation (P = Portrait, L = Landscape).
  196. * @protected
  197. */
  198. protected $CurOrientation;
  199. /**
  200. * Page dimensions.
  201. * @protected
  202. */
  203. protected $pagedim = array();
  204. /**
  205. * Scale factor (number of points in user unit).
  206. * @protected
  207. */
  208. protected $k;
  209. /**
  210. * Width of page format in points.
  211. * @protected
  212. */
  213. protected $fwPt;
  214. /**
  215. * Height of page format in points.
  216. * @protected
  217. */
  218. protected $fhPt;
  219. /**
  220. * Current width of page in points.
  221. * @protected
  222. */
  223. protected $wPt;
  224. /**
  225. * Current height of page in points.
  226. * @protected
  227. */
  228. protected $hPt;
  229. /**
  230. * Current width of page in user unit.
  231. * @protected
  232. */
  233. protected $w;
  234. /**
  235. * Current height of page in user unit.
  236. * @protected
  237. */
  238. protected $h;
  239. /**
  240. * Left margin.
  241. * @protected
  242. */
  243. protected $lMargin;
  244. /**
  245. * Top margin.
  246. * @protected
  247. */
  248. protected $tMargin;
  249. /**
  250. * Right margin.
  251. * @protected
  252. */
  253. protected $rMargin;
  254. /**
  255. * Page break margin.
  256. * @protected
  257. */
  258. protected $bMargin;
  259. /**
  260. * Array of cell internal paddings ('T' => top, 'R' => right, 'B' => bottom, 'L' => left).
  261. * @since 5.9.000 (2010-10-03)
  262. * @protected
  263. */
  264. protected $cell_padding = array('T' => 0, 'R' => 0, 'B' => 0, 'L' => 0);
  265. /**
  266. * Array of cell margins ('T' => top, 'R' => right, 'B' => bottom, 'L' => left).
  267. * @since 5.9.000 (2010-10-04)
  268. * @protected
  269. */
  270. protected $cell_margin = array('T' => 0, 'R' => 0, 'B' => 0, 'L' => 0);
  271. /**
  272. * Current horizontal position in user unit for cell positioning.
  273. * @protected
  274. */
  275. protected $x;
  276. /**
  277. * Current vertical position in user unit for cell positioning.
  278. * @protected
  279. */
  280. protected $y;
  281. /**
  282. * Height of last cell printed.
  283. * @protected
  284. */
  285. protected $lasth;
  286. /**
  287. * Line width in user unit.
  288. * @protected
  289. */
  290. protected $LineWidth;
  291. /**
  292. * Array of standard font names.
  293. * @protected
  294. */
  295. protected $CoreFonts;
  296. /**
  297. * Array of used fonts.
  298. * @protected
  299. */
  300. protected $fonts = array();
  301. /**
  302. * Array of font files.
  303. * @protected
  304. */
  305. protected $FontFiles = array();
  306. /**
  307. * Array of encoding differences.
  308. * @protected
  309. */
  310. protected $diffs = array();
  311. /**
  312. * Array of used images.
  313. * @protected
  314. */
  315. protected $images = array();
  316. /**
  317. * Array of Annotations in pages.
  318. * @protected
  319. */
  320. protected $PageAnnots = array();
  321. /**
  322. * Array of internal links.
  323. * @protected
  324. */
  325. protected $links = array();
  326. /**
  327. * Current font family.
  328. * @protected
  329. */
  330. protected $FontFamily;
  331. /**
  332. * Current font style.
  333. * @protected
  334. */
  335. protected $FontStyle;
  336. /**
  337. * Current font ascent (distance between font top and baseline).
  338. * @protected
  339. * @since 2.8.000 (2007-03-29)
  340. */
  341. protected $FontAscent;
  342. /**
  343. * Current font descent (distance between font bottom and baseline).
  344. * @protected
  345. * @since 2.8.000 (2007-03-29)
  346. */
  347. protected $FontDescent;
  348. /**
  349. * Underlining flag.
  350. * @protected
  351. */
  352. protected $underline;
  353. /**
  354. * Overlining flag.
  355. * @protected
  356. */
  357. protected $overline;
  358. /**
  359. * Current font info.
  360. * @protected
  361. */
  362. protected $CurrentFont;
  363. /**
  364. * Current font size in points.
  365. * @protected
  366. */
  367. protected $FontSizePt;
  368. /**
  369. * Current font size in user unit.
  370. * @protected
  371. */
  372. protected $FontSize;
  373. /**
  374. * Commands for drawing color.
  375. * @protected
  376. */
  377. protected $DrawColor;
  378. /**
  379. * Commands for filling color.
  380. * @protected
  381. */
  382. protected $FillColor;
  383. /**
  384. * Commands for text color.
  385. * @protected
  386. */
  387. protected $TextColor;
  388. /**
  389. * Indicates whether fill and text colors are different.
  390. * @protected
  391. */
  392. protected $ColorFlag;
  393. /**
  394. * Automatic page breaking.
  395. * @protected
  396. */
  397. protected $AutoPageBreak;
  398. /**
  399. * Threshold used to trigger page breaks.
  400. * @protected
  401. */
  402. protected $PageBreakTrigger;
  403. /**
  404. * Flag set when processing page header.
  405. * @protected
  406. */
  407. protected $InHeader = false;
  408. /**
  409. * Flag set when processing page footer.
  410. * @protected
  411. */
  412. protected $InFooter = false;
  413. /**
  414. * Zoom display mode.
  415. * @protected
  416. */
  417. protected $ZoomMode;
  418. /**
  419. * Layout display mode.
  420. * @protected
  421. */
  422. protected $LayoutMode;
  423. /**
  424. * If true set the document information dictionary in Unicode.
  425. * @protected
  426. */
  427. protected $docinfounicode = true;
  428. /**
  429. * Document title.
  430. * @protected
  431. */
  432. protected $title = '';
  433. /**
  434. * Document subject.
  435. * @protected
  436. */
  437. protected $subject = '';
  438. /**
  439. * Document author.
  440. * @protected
  441. */
  442. protected $author = '';
  443. /**
  444. * Document keywords.
  445. * @protected
  446. */
  447. protected $keywords = '';
  448. /**
  449. * Document creator.
  450. * @protected
  451. */
  452. protected $creator = '';
  453. /**
  454. * Starting page number.
  455. * @protected
  456. */
  457. protected $starting_page_number = 1;
  458. /**
  459. * String alias for total number of pages.
  460. * @protected
  461. */
  462. protected $alias_tot_pages = '{:ptp:}';
  463. /**
  464. * String alias for page number.
  465. * @protected
  466. */
  467. protected $alias_num_page = '{:pnp:}';
  468. /**
  469. * String alias for total number of pages in a single group.
  470. * @protected
  471. */
  472. protected $alias_group_tot_pages = '{:ptg:}';
  473. /**
  474. * String alias for group page number.
  475. * @protected
  476. */
  477. protected $alias_group_num_page = '{:png:}';
  478. /**
  479. * String alias for right shift compensation used to correctly align page numbers on the right.
  480. * @protected
  481. */
  482. protected $alias_right_shift = '{rsc:';
  483. /**
  484. * The right-bottom (or left-bottom for RTL) corner X coordinate of last inserted image.
  485. * @since 2002-07-31
  486. * @author Nicola Asuni
  487. * @protected
  488. */
  489. protected $img_rb_x;
  490. /**
  491. * The right-bottom corner Y coordinate of last inserted image.
  492. * @since 2002-07-31
  493. * @author Nicola Asuni
  494. * @protected
  495. */
  496. protected $img_rb_y;
  497. /**
  498. * Adjusting factor to convert pixels to user units.
  499. * @since 2004-06-14
  500. * @author Nicola Asuni
  501. * @protected
  502. */
  503. protected $imgscale = 1;
  504. /**
  505. * Boolean flag set to true when the input text is unicode (require unicode fonts).
  506. * @since 2005-01-02
  507. * @author Nicola Asuni
  508. * @protected
  509. */
  510. protected $isunicode = false;
  511. /**
  512. * Object containing unicode data.
  513. * @since 5.9.004 (2010-10-18)
  514. * @author Nicola Asuni
  515. * @protected
  516. */
  517. protected $unicode;
  518. /**
  519. * PDF version.
  520. * @since 1.5.3
  521. * @protected
  522. */
  523. protected $PDFVersion = '1.7';
  524. /**
  525. * ID of the stored default header template (-1 = not set).
  526. * @protected
  527. */
  528. protected $header_xobjid = -1;
  529. /**
  530. * If true reset the Header Xobject template at each page
  531. * @protected
  532. */
  533. protected $header_xobj_autoreset = false;
  534. /**
  535. * Minimum distance between header and top page margin.
  536. * @protected
  537. */
  538. protected $header_margin;
  539. /**
  540. * Minimum distance between footer and bottom page margin.
  541. * @protected
  542. */
  543. protected $footer_margin;
  544. /**
  545. * Original left margin value.
  546. * @protected
  547. * @since 1.53.0.TC013
  548. */
  549. protected $original_lMargin;
  550. /**
  551. * Original right margin value.
  552. * @protected
  553. * @since 1.53.0.TC013
  554. */
  555. protected $original_rMargin;
  556. /**
  557. * Default font used on page header.
  558. * @protected
  559. */
  560. protected $header_font;
  561. /**
  562. * Default font used on page footer.
  563. * @protected
  564. */
  565. protected $footer_font;
  566. /**
  567. * Language templates.
  568. * @protected
  569. */
  570. protected $l;
  571. /**
  572. * Barcode to print on page footer (only if set).
  573. * @protected
  574. */
  575. protected $barcode = false;
  576. /**
  577. * Boolean flag to print/hide page header.
  578. * @protected
  579. */
  580. protected $print_header = true;
  581. /**
  582. * Boolean flag to print/hide page footer.
  583. * @protected
  584. */
  585. protected $print_footer = true;
  586. /**
  587. * Header image logo.
  588. * @protected
  589. */
  590. protected $header_logo = '';
  591. /**
  592. * Width of header image logo in user units.
  593. * @protected
  594. */
  595. protected $header_logo_width = 30;
  596. /**
  597. * Title to be printed on default page header.
  598. * @protected
  599. */
  600. protected $header_title = '';
  601. /**
  602. * String to pring on page header after title.
  603. * @protected
  604. */
  605. protected $header_string = '';
  606. /**
  607. * Default number of columns for html table.
  608. * @protected
  609. */
  610. protected $default_table_columns = 4;
  611. // variables for html parser
  612. /**
  613. * HTML PARSER: array to store current link and rendering styles.
  614. * @protected
  615. */
  616. protected $HREF = array();
  617. /**
  618. * List of available fonts on filesystem.
  619. * @protected
  620. */
  621. protected $fontlist = array();
  622. /**
  623. * Current foreground color.
  624. * @protected
  625. */
  626. protected $fgcolor;
  627. /**
  628. * HTML PARSER: array of boolean values, true in case of ordered list (OL), false otherwise.
  629. * @protected
  630. */
  631. protected $listordered = array();
  632. /**
  633. * HTML PARSER: array count list items on nested lists.
  634. * @protected
  635. */
  636. protected $listcount = array();
  637. /**
  638. * HTML PARSER: current list nesting level.
  639. * @protected
  640. */
  641. protected $listnum = 0;
  642. /**
  643. * HTML PARSER: indent amount for lists.
  644. * @protected
  645. */
  646. protected $listindent = 0;
  647. /**
  648. * HTML PARSER: current list indententation level.
  649. * @protected
  650. */
  651. protected $listindentlevel = 0;
  652. /**
  653. * Current background color.
  654. * @protected
  655. */
  656. protected $bgcolor;
  657. /**
  658. * Temporary font size in points.
  659. * @protected
  660. */
  661. protected $tempfontsize = 10;
  662. /**
  663. * Spacer string for LI tags.
  664. * @protected
  665. */
  666. protected $lispacer = '';
  667. /**
  668. * Default encoding.
  669. * @protected
  670. * @since 1.53.0.TC010
  671. */
  672. protected $encoding = 'UTF-8';
  673. /**
  674. * PHP internal encoding.
  675. * @protected
  676. * @since 1.53.0.TC016
  677. */
  678. protected $internal_encoding;
  679. /**
  680. * Boolean flag to indicate if the document language is Right-To-Left.
  681. * @protected
  682. * @since 2.0.000
  683. */
  684. protected $rtl = false;
  685. /**
  686. * Boolean flag used to force RTL or LTR string direction.
  687. * @protected
  688. * @since 2.0.000
  689. */
  690. protected $tmprtl = false;
  691. // --- Variables used for document encryption:
  692. /**
  693. * IBoolean flag indicating whether document is protected.
  694. * @protected
  695. * @since 2.0.000 (2008-01-02)
  696. */
  697. protected $encrypted;
  698. /**
  699. * Array containing encryption settings.
  700. * @protected
  701. * @since 5.0.005 (2010-05-11)
  702. */
  703. protected $encryptdata = array();
  704. /**
  705. * Last RC4 key encrypted (cached for optimisation).
  706. * @protected
  707. * @since 2.0.000 (2008-01-02)
  708. */
  709. protected $last_enc_key;
  710. /**
  711. * Last RC4 computed key.
  712. * @protected
  713. * @since 2.0.000 (2008-01-02)
  714. */
  715. protected $last_enc_key_c;
  716. /**
  717. * Encryption padding string.
  718. * @protected
  719. */
  720. 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";
  721. /**
  722. * File ID (used on document trailer).
  723. * @protected
  724. * @since 5.0.005 (2010-05-12)
  725. */
  726. protected $file_id;
  727. // --- bookmark ---
  728. /**
  729. * Outlines for bookmark.
  730. * @protected
  731. * @since 2.1.002 (2008-02-12)
  732. */
  733. protected $outlines = array();
  734. /**
  735. * Outline root for bookmark.
  736. * @protected
  737. * @since 2.1.002 (2008-02-12)
  738. */
  739. protected $OutlineRoot;
  740. // --- javascript and form ---
  741. /**
  742. * Javascript code.
  743. * @protected
  744. * @since 2.1.002 (2008-02-12)
  745. */
  746. protected $javascript = '';
  747. /**
  748. * Javascript counter.
  749. * @protected
  750. * @since 2.1.002 (2008-02-12)
  751. */
  752. protected $n_js;
  753. /**
  754. * line trough state
  755. * @protected
  756. * @since 2.8.000 (2008-03-19)
  757. */
  758. protected $linethrough;
  759. /**
  760. * Array with additional document-wide usage rights for the document.
  761. * @protected
  762. * @since 5.8.014 (2010-08-23)
  763. */
  764. protected $ur = array();
  765. /**
  766. * DPI (Dot Per Inch) Document Resolution (do not change).
  767. * @protected
  768. * @since 3.0.000 (2008-03-27)
  769. */
  770. protected $dpi = 72;
  771. /**
  772. * Array of page numbers were a new page group was started (the page numbers are the keys of the array).
  773. * @protected
  774. * @since 3.0.000 (2008-03-27)
  775. */
  776. protected $newpagegroup = array();
  777. /**
  778. * Array that contains the number of pages in each page group.
  779. * @protected
  780. * @since 3.0.000 (2008-03-27)
  781. */
  782. protected $pagegroups = array();
  783. /**
  784. * Current page group number.
  785. * @protected
  786. * @since 3.0.000 (2008-03-27)
  787. */
  788. protected $currpagegroup = 0;
  789. /**
  790. * Array of transparency objects and parameters.
  791. * @protected
  792. * @since 3.0.000 (2008-03-27)
  793. */
  794. protected $extgstates;
  795. /**
  796. * Set the default JPEG compression quality (1-100).
  797. * @protected
  798. * @since 3.0.000 (2008-03-27)
  799. */
  800. protected $jpeg_quality;
  801. /**
  802. * Default cell height ratio.
  803. * @protected
  804. * @since 3.0.014 (2008-05-23)
  805. */
  806. protected $cell_height_ratio = K_CELL_HEIGHT_RATIO;
  807. /**
  808. * PDF viewer preferences.
  809. * @protected
  810. * @since 3.1.000 (2008-06-09)
  811. */
  812. protected $viewer_preferences;
  813. /**
  814. * A name object specifying how the document should be displayed when opened.
  815. * @protected
  816. * @since 3.1.000 (2008-06-09)
  817. */
  818. protected $PageMode;
  819. /**
  820. * Array for storing gradient information.
  821. * @protected
  822. * @since 3.1.000 (2008-06-09)
  823. */
  824. protected $gradients = array();
  825. /**
  826. * Array used to store positions inside the pages buffer (keys are the page numbers).
  827. * @protected
  828. * @since 3.2.000 (2008-06-26)
  829. */
  830. protected $intmrk = array();
  831. /**
  832. * Array used to store positions inside the pages buffer (keys are the page numbers).
  833. * @protected
  834. * @since 5.7.000 (2010-08-03)
  835. */
  836. protected $bordermrk = array();
  837. /**
  838. * Array used to store page positions to track empty pages (keys are the page numbers).
  839. * @protected
  840. * @since 5.8.007 (2010-08-18)
  841. */
  842. protected $emptypagemrk = array();
  843. /**
  844. * Array used to store content positions inside the pages buffer (keys are the page numbers).
  845. * @protected
  846. * @since 4.6.021 (2009-07-20)
  847. */
  848. protected $cntmrk = array();
  849. /**
  850. * Array used to store footer positions of each page.
  851. * @protected
  852. * @since 3.2.000 (2008-07-01)
  853. */
  854. protected $footerpos = array();
  855. /**
  856. * Array used to store footer length of each page.
  857. * @protected
  858. * @since 4.0.014 (2008-07-29)
  859. */
  860. protected $footerlen = array();
  861. /**
  862. * Boolean flag to indicate if a new line is created.
  863. * @protected
  864. * @since 3.2.000 (2008-07-01)
  865. */
  866. protected $newline = true;
  867. /**
  868. * End position of the latest inserted line.
  869. * @protected
  870. * @since 3.2.000 (2008-07-01)
  871. */
  872. protected $endlinex = 0;
  873. /**
  874. * PDF string for width value of the last line.
  875. * @protected
  876. * @since 4.0.006 (2008-07-16)
  877. */
  878. protected $linestyleWidth = '';
  879. /**
  880. * PDF string for CAP value of the last line.
  881. * @protected
  882. * @since 4.0.006 (2008-07-16)
  883. */
  884. protected $linestyleCap = '0 J';
  885. /**
  886. * PDF string for join value of the last line.
  887. * @protected
  888. * @since 4.0.006 (2008-07-16)
  889. */
  890. protected $linestyleJoin = '0 j';
  891. /**
  892. * PDF string for dash value of the last line.
  893. * @protected
  894. * @since 4.0.006 (2008-07-16)
  895. */
  896. protected $linestyleDash = '[] 0 d';
  897. /**
  898. * Boolean flag to indicate if marked-content sequence is open.
  899. * @protected
  900. * @since 4.0.013 (2008-07-28)
  901. */
  902. protected $openMarkedContent = false;
  903. /**
  904. * Count the latest inserted vertical spaces on HTML.
  905. * @protected
  906. * @since 4.0.021 (2008-08-24)
  907. */
  908. protected $htmlvspace = 0;
  909. /**
  910. * Array of Spot colors.
  911. * @protected
  912. * @since 4.0.024 (2008-09-12)
  913. */
  914. protected $spot_colors = array();
  915. /**
  916. * Symbol used for HTML unordered list items.
  917. * @protected
  918. * @since 4.0.028 (2008-09-26)
  919. */
  920. protected $lisymbol = '';
  921. /**
  922. * String used to mark the beginning and end of EPS image blocks.
  923. * @protected
  924. * @since 4.1.000 (2008-10-18)
  925. */
  926. protected $epsmarker = 'x#!#EPS#!#x';
  927. /**
  928. * Array of transformation matrix.
  929. * @protected
  930. * @since 4.2.000 (2008-10-29)
  931. */
  932. protected $transfmatrix = array();
  933. /**
  934. * Current key for transformation matrix.
  935. * @protected
  936. * @since 4.8.005 (2009-09-17)
  937. */
  938. protected $transfmatrix_key = 0;
  939. /**
  940. * Booklet mode for double-sided pages.
  941. * @protected
  942. * @since 4.2.000 (2008-10-29)
  943. */
  944. protected $booklet = false;
  945. /**
  946. * Epsilon value used for float calculations.
  947. * @protected
  948. * @since 4.2.000 (2008-10-29)
  949. */
  950. protected $feps = 0.005;
  951. /**
  952. * Array used for custom vertical spaces for HTML tags.
  953. * @protected
  954. * @since 4.2.001 (2008-10-30)
  955. */
  956. protected $tagvspaces = array();
  957. /**
  958. * HTML PARSER: custom indent amount for lists. Negative value means disabled.
  959. * @protected
  960. * @since 4.2.007 (2008-11-12)
  961. */
  962. protected $customlistindent = -1;
  963. /**
  964. * Boolean flag to indicate if the border of the cell sides that cross the page should be removed.
  965. * @protected
  966. * @since 4.2.010 (2008-11-14)
  967. */
  968. protected $opencell = true;
  969. /**
  970. * Array of files to embedd.
  971. * @protected
  972. * @since 4.4.000 (2008-12-07)
  973. */
  974. protected $embeddedfiles = array();
  975. /**
  976. * Boolean flag to indicate if we are inside a PRE tag.
  977. * @protected
  978. * @since 4.4.001 (2008-12-08)
  979. */
  980. protected $premode = false;
  981. /**
  982. * Array used to store positions of graphics transformation blocks inside the page buffer.
  983. * keys are the page numbers
  984. * @protected
  985. * @since 4.4.002 (2008-12-09)
  986. */
  987. protected $transfmrk = array();
  988. /**
  989. * Default color for html links.
  990. * @protected
  991. * @since 4.4.003 (2008-12-09)
  992. */
  993. protected $htmlLinkColorArray = array(0, 0, 255);
  994. /**
  995. * Default font style to add to html links.
  996. * @protected
  997. * @since 4.4.003 (2008-12-09)
  998. */
  999. protected $htmlLinkFontStyle = 'U';
  1000. /**
  1001. * Counts the number of pages.
  1002. * @protected
  1003. * @since 4.5.000 (2008-12-31)
  1004. */
  1005. protected $numpages = 0;
  1006. /**
  1007. * Array containing page lengths in bytes.
  1008. * @protected
  1009. * @since 4.5.000 (2008-12-31)
  1010. */
  1011. protected $pagelen = array();
  1012. /**
  1013. * Counts the number of pages.
  1014. * @protected
  1015. * @since 4.5.000 (2008-12-31)
  1016. */
  1017. protected $numimages = 0;
  1018. /**
  1019. * Store the image keys.
  1020. * @protected
  1021. * @since 4.5.000 (2008-12-31)
  1022. */
  1023. protected $imagekeys = array();
  1024. /**
  1025. * Length of the buffer in bytes.
  1026. * @protected
  1027. * @since 4.5.000 (2008-12-31)
  1028. */
  1029. protected $bufferlen = 0;
  1030. /**
  1031. * If true enables disk caching.
  1032. * @protected
  1033. * @since 4.5.000 (2008-12-31)
  1034. */
  1035. protected $diskcache = false;
  1036. /**
  1037. * Counts the number of fonts.
  1038. * @protected
  1039. * @since 4.5.000 (2009-01-02)
  1040. */
  1041. protected $numfonts = 0;
  1042. /**
  1043. * Store the font keys.
  1044. * @protected
  1045. * @since 4.5.000 (2009-01-02)
  1046. */
  1047. protected $fontkeys = array();
  1048. /**
  1049. * Store the font object IDs.
  1050. * @protected
  1051. * @since 4.8.001 (2009-09-09)
  1052. */
  1053. protected $font_obj_ids = array();
  1054. /**
  1055. * Store the fage status (true when opened, false when closed).
  1056. * @protected
  1057. * @since 4.5.000 (2009-01-02)
  1058. */
  1059. protected $pageopen = array();
  1060. /**
  1061. * Default monospace font.
  1062. * @protected
  1063. * @since 4.5.025 (2009-03-10)
  1064. */
  1065. protected $default_monospaced_font = 'courier';
  1066. /**
  1067. * Cloned copy of the current class object.
  1068. * @protected
  1069. * @since 4.5.029 (2009-03-19)
  1070. */
  1071. protected $objcopy;
  1072. /**
  1073. * Array used to store the lengths of cache files.
  1074. * @protected
  1075. * @since 4.5.029 (2009-03-19)
  1076. */
  1077. protected $cache_file_length = array();
  1078. /**
  1079. * Table header content to be repeated on each new page.
  1080. * @protected
  1081. * @since 4.5.030 (2009-03-20)
  1082. */
  1083. protected $thead = '';
  1084. /**
  1085. * Margins used for table header.
  1086. * @protected
  1087. * @since 4.5.030 (2009-03-20)
  1088. */
  1089. protected $theadMargins = array();
  1090. /**
  1091. * Cache array for UTF8StringToArray() method.
  1092. * @protected
  1093. * @since 4.5.037 (2009-04-07)
  1094. */
  1095. protected $cache_UTF8StringToArray = array();
  1096. /**
  1097. * Maximum size of cache array used for UTF8StringToArray() method.
  1098. * @protected
  1099. * @since 4.5.037 (2009-04-07)
  1100. */
  1101. protected $cache_maxsize_UTF8StringToArray = 8;
  1102. /**
  1103. * Current size of cache array used for UTF8StringToArray() method.
  1104. * @protected
  1105. * @since 4.5.037 (2009-04-07)
  1106. */
  1107. protected $cache_size_UTF8StringToArray = 0;
  1108. /**
  1109. * Boolean flag to enable document digital signature.
  1110. * @protected
  1111. * @since 4.6.005 (2009-04-24)
  1112. */
  1113. protected $sign = false;
  1114. /**
  1115. * Digital signature data.
  1116. * @protected
  1117. * @since 4.6.005 (2009-04-24)
  1118. */
  1119. protected $signature_data = array();
  1120. /**
  1121. * Digital signature max length.
  1122. * @protected
  1123. * @since 4.6.005 (2009-04-24)
  1124. */
  1125. protected $signature_max_length = 11742;
  1126. /**
  1127. * Data for digital signature appearance.
  1128. * @protected
  1129. * @since 5.3.011 (2010-06-16)
  1130. */
  1131. protected $signature_appearance = array('page' => 1, 'rect' => '0 0 0 0');
  1132. /**
  1133. * Array of empty digital signature appearances.
  1134. * @protected
  1135. * @since 5.9.101 (2011-07-06)
  1136. */
  1137. protected $empty_signature_appearance = array();
  1138. /**
  1139. * Regular expression used to find blank characters (required for word-wrapping).
  1140. * @protected
  1141. * @since 4.6.006 (2009-04-28)
  1142. */
  1143. protected $re_spaces = '/[^\S\xa0]/';
  1144. /**
  1145. * Array of $re_spaces parts.
  1146. * @protected
  1147. * @since 5.5.011 (2010-07-09)
  1148. */
  1149. protected $re_space = array('p' => '[^\S\xa0]', 'm' => '');
  1150. /**
  1151. * Digital signature object ID.
  1152. * @protected
  1153. * @since 4.6.022 (2009-06-23)
  1154. */
  1155. protected $sig_obj_id = 0;
  1156. /**
  1157. * ByteRange placemark used during digital signature process.
  1158. * @protected
  1159. * @since 4.6.028 (2009-08-25)
  1160. */
  1161. protected $byterange_string = '/ByteRange[0 ********** ********** **********]';
  1162. /**
  1163. * Placemark used during digital signature process.
  1164. * @protected
  1165. * @since 4.6.028 (2009-08-25)
  1166. */
  1167. protected $sig_annot_ref = '***SIGANNREF*** 0 R';
  1168. /**
  1169. * ID of page objects.
  1170. * @protected
  1171. * @since 4.7.000 (2009-08-29)
  1172. */
  1173. protected $page_obj_id = array();
  1174. /**
  1175. * List of form annotations IDs.
  1176. * @protected
  1177. * @since 4.8.000 (2009-09-07)
  1178. */
  1179. protected $form_obj_id = array();
  1180. /**
  1181. * 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.
  1182. * @protected
  1183. * @since 4.8.000 (2009-09-07)
  1184. */
  1185. protected $default_form_prop = array('lineWidth'=>1, 'borderStyle'=>'solid', 'fillColor'=>array(255, 255, 255), 'strokeColor'=>array(128, 128, 128));
  1186. /**
  1187. * Javascript objects array.
  1188. * @protected
  1189. * @since 4.8.000 (2009-09-07)
  1190. */
  1191. protected $js_objects = array();
  1192. /**
  1193. * Current form action (used during XHTML rendering).
  1194. * @protected
  1195. * @since 4.8.000 (2009-09-07)
  1196. */
  1197. protected $form_action = '';
  1198. /**
  1199. * Current form encryption type (used during XHTML rendering).
  1200. * @protected
  1201. * @since 4.8.000 (2009-09-07)
  1202. */
  1203. protected $form_enctype = 'application/x-www-form-urlencoded';
  1204. /**
  1205. * Current method to submit forms.
  1206. * @protected
  1207. * @since 4.8.000 (2009-09-07)
  1208. */
  1209. protected $form_mode = 'post';
  1210. /**
  1211. * List of fonts used on form fields (fontname => fontkey).
  1212. * @protected
  1213. * @since 4.8.001 (2009-09-09)
  1214. */
  1215. protected $annotation_fonts = array();
  1216. /**
  1217. * List of radio buttons parent objects.
  1218. * @protected
  1219. * @since 4.8.001 (2009-09-09)
  1220. */
  1221. protected $radiobutton_groups = array();
  1222. /**
  1223. * List of radio group objects IDs.
  1224. * @protected
  1225. * @since 4.8.001 (2009-09-09)
  1226. */
  1227. protected $radio_groups = array();
  1228. /**
  1229. * Text indentation value (used for text-indent CSS attribute).
  1230. * @protected
  1231. * @since 4.8.006 (2009-09-23)
  1232. */
  1233. protected $textindent = 0;
  1234. /**
  1235. * Store page number when startTransaction() is called.
  1236. * @protected
  1237. * @since 4.8.006 (2009-09-23)
  1238. */
  1239. protected $start_transaction_page = 0;
  1240. /**
  1241. * Store Y position when startTransaction() is called.
  1242. * @protected
  1243. * @since 4.9.001 (2010-03-28)
  1244. */
  1245. protected $start_transaction_y = 0;
  1246. /**
  1247. * True when we are printing the thead section on a new page.
  1248. * @protected
  1249. * @since 4.8.027 (2010-01-25)
  1250. */
  1251. protected $inthead = false;
  1252. /**
  1253. * Array of column measures (width, space, starting Y position).
  1254. * @protected
  1255. * @since 4.9.001 (2010-03-28)
  1256. */
  1257. protected $columns = array();
  1258. /**
  1259. * Number of colums.
  1260. * @protected
  1261. * @since 4.9.001 (2010-03-28)
  1262. */
  1263. protected $num_columns = 1;
  1264. /**
  1265. * Current column number.
  1266. * @protected
  1267. * @since 4.9.001 (2010-03-28)
  1268. */
  1269. protected $current_column = 0;
  1270. /**
  1271. * Starting page for columns.
  1272. * @protected
  1273. * @since 4.9.001 (2010-03-28)
  1274. */
  1275. protected $column_start_page = 0;
  1276. /**
  1277. * Maximum page and column selected.
  1278. * @protected
  1279. * @since 5.8.000 (2010-08-11)
  1280. */
  1281. protected $maxselcol = array('page' => 0, 'column' => 0);
  1282. /**
  1283. * Array of: X difference between table cell x start and starting page margin, cellspacing, cellpadding.
  1284. * @protected
  1285. * @since 5.8.000 (2010-08-11)
  1286. */
  1287. protected $colxshift = array('x' => 0, 's' => array('H' => 0, 'V' => 0), 'p' => array('L' => 0, 'T' => 0, 'R' => 0, 'B' => 0));
  1288. /**
  1289. * 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.
  1290. * @protected
  1291. * @since 4.9.008 (2010-04-03)
  1292. */
  1293. protected $textrendermode = 0;
  1294. /**
  1295. * Text stroke width in doc units.
  1296. * @protected
  1297. * @since 4.9.008 (2010-04-03)
  1298. */
  1299. protected $textstrokewidth = 0;
  1300. /**
  1301. * Current stroke color.
  1302. * @protected
  1303. * @since 4.9.008 (2010-04-03)
  1304. */
  1305. protected $strokecolor;
  1306. /**
  1307. * Default unit of measure for document.
  1308. * @protected
  1309. * @since 5.0.000 (2010-04-22)
  1310. */
  1311. protected $pdfunit = 'mm';
  1312. /**
  1313. * Boolean flag true when we are on TOC (Table Of Content) page.
  1314. * @protected
  1315. */
  1316. protected $tocpage = false;
  1317. /**
  1318. * Boolean flag: if true convert vector images (SVG, EPS) to raster image using GD or ImageMagick library.
  1319. * @protected
  1320. * @since 5.0.000 (2010-04-26)
  1321. */
  1322. protected $rasterize_vector_images = false;
  1323. /**
  1324. * Boolean flag: if true enables font subsetting by default.
  1325. * @protected
  1326. * @since 5.3.002 (2010-06-07)
  1327. */
  1328. protected $font_subsetting = true;
  1329. /**
  1330. * Array of default graphic settings.
  1331. * @protected
  1332. * @since 5.5.008 (2010-07-02)
  1333. */
  1334. protected $default_graphic_vars = array();
  1335. /**
  1336. * Array of XObjects.
  1337. * @protected
  1338. * @since 5.8.014 (2010-08-23)
  1339. */
  1340. protected $xobjects = array();
  1341. /**
  1342. * Boolean value true when we are inside an XObject.
  1343. * @protected
  1344. * @since 5.8.017 (2010-08-24)
  1345. */
  1346. protected $inxobj = false;
  1347. /**
  1348. * Current XObject ID.
  1349. * @protected
  1350. * @since 5.8.017 (2010-08-24)
  1351. */
  1352. protected $xobjid = '';
  1353. /**
  1354. * Percentage of character stretching.
  1355. * @protected
  1356. * @since 5.9.000 (2010-09-29)
  1357. */
  1358. protected $font_stretching = 100;
  1359. /**
  1360. * Increases or decreases the space between characters in a text by the specified amount (tracking/kerning).
  1361. * @protected
  1362. * @since 5.9.000 (2010-09-29)
  1363. */
  1364. protected $font_spacing = 0;
  1365. /**
  1366. * Array of no-write regions.
  1367. * ('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)
  1368. * @protected
  1369. * @since 5.9.003 (2010-10-14)
  1370. */
  1371. protected $page_regions = array();
  1372. /**
  1373. * Array containing HTML color names and values.
  1374. * @protected
  1375. * @since 5.9.004 (2010-10-18)
  1376. */
  1377. protected $webcolor = array();
  1378. /**
  1379. * Array containing spot color names and values.
  1380. * @protected
  1381. * @since 5.9.012 (2010-11-11)
  1382. */
  1383. protected $spotcolor = array();
  1384. /**
  1385. * Array of PDF layers data.
  1386. * @protected
  1387. * @since 5.9.102 (2011-07-13)
  1388. */
  1389. protected $pdflayers = array();
  1390. /**
  1391. * A dictionary of names and corresponding destinations (Dests key on document Catalog).
  1392. * @protected
  1393. * @since 5.9.097 (2011-06-23)
  1394. */
  1395. protected $dests = array();
  1396. /**
  1397. * Object ID for Named Destinations
  1398. * @protected
  1399. * @since 5.9.097 (2011-06-23)
  1400. */
  1401. protected $n_dests;
  1402. /**
  1403. * Directory used for the last SVG image.
  1404. * @protected
  1405. * @since 5.0.000 (2010-05-05)
  1406. */
  1407. protected $svgdir = '';
  1408. /**
  1409. * Deafult unit of measure for SVG.
  1410. * @protected
  1411. * @since 5.0.000 (2010-05-02)
  1412. */
  1413. protected $svgunit = 'px';
  1414. /**
  1415. * Array of SVG gradients.
  1416. * @protected
  1417. * @since 5.0.000 (2010-05-02)
  1418. */
  1419. protected $svggradients = array();
  1420. /**
  1421. * ID of last SVG gradient.
  1422. * @protected
  1423. * @since 5.0.000 (2010-05-02)
  1424. */
  1425. protected $svggradientid = 0;
  1426. /**
  1427. * Boolean value true when in SVG defs group.
  1428. * @protected
  1429. * @since 5.0.000 (2010-05-02)
  1430. */
  1431. protected $svgdefsmode = false;
  1432. /**
  1433. * Array of SVG defs.
  1434. * @protected
  1435. * @since 5.0.000 (2010-05-02)
  1436. */
  1437. protected $svgdefs = array();
  1438. /**
  1439. * Boolean value true when in SVG clipPath tag.
  1440. * @protected
  1441. * @since 5.0.000 (2010-04-26)
  1442. */
  1443. protected $svgclipmode = false;
  1444. /**
  1445. * Array of SVG clipPath commands.
  1446. * @protected
  1447. * @since 5.0.000 (2010-05-02)
  1448. */
  1449. protected $svgclippaths = array();
  1450. /**
  1451. * Array of SVG clipPath tranformation matrix.
  1452. * @protected
  1453. * @since 5.8.022 (2010-08-31)
  1454. */
  1455. protected $svgcliptm = array();
  1456. /**
  1457. * ID of last SVG clipPath.
  1458. * @protected
  1459. * @since 5.0.000 (2010-05-02)
  1460. */
  1461. protected $svgclipid = 0;
  1462. /**
  1463. * SVG text.
  1464. * @protected
  1465. * @since 5.0.000 (2010-05-02)
  1466. */
  1467. protected $svgtext = '';
  1468. /**
  1469. * SVG text properties.
  1470. * @protected
  1471. * @since 5.8.013 (2010-08-23)
  1472. */
  1473. protected $svgtextmode = array();
  1474. /**
  1475. * Array of hinheritable SVG properties.
  1476. * @protected
  1477. * @since 5.0.000 (2010-05-02)
  1478. */
  1479. 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');
  1480. /**
  1481. * Array of SVG properties.
  1482. * @protected
  1483. * @since 5.0.000 (2010-05-02)
  1484. */
  1485. protected $svgstyles = array(array(
  1486. 'alignment-baseline' => 'auto',
  1487. 'baseline-shift' => 'baseline',
  1488. 'clip' => 'auto',
  1489. 'clip-path' => 'none',
  1490. 'clip-rule' => 'nonzero',
  1491. 'color' => 'black',
  1492. 'color-interpolation' => 'sRGB',
  1493. 'color-interpolation-filters' => 'linearRGB',
  1494. 'color-profile' => 'auto',
  1495. 'color-rendering' => 'auto',
  1496. 'cursor' => 'auto',
  1497. 'direction' => 'ltr',
  1498. 'display' => 'inline',
  1499. 'dominant-baseline' => 'auto',
  1500. 'enable-background' => 'accumulate',
  1501. 'fill' => 'black',
  1502. 'fill-opacity' => 1,
  1503. 'fill-rule' => 'nonzero',
  1504. 'filter' => 'none',
  1505. 'flood-color' => 'black',
  1506. 'flood-opacity' => 1,
  1507. 'font' => '',
  1508. 'font-family' => 'helvetica',
  1509. 'font-size' => 'medium',
  1510. 'font-size-adjust' => 'none',
  1511. 'font-stretch' => 'normal',
  1512. 'font-style' => 'normal',
  1513. 'font-variant' => 'normal',
  1514. 'font-weight' => 'normal',
  1515. 'glyph-orientation-horizontal' => '0deg',
  1516. 'glyph-orientation-vertical' => 'auto',
  1517. 'image-rendering' => 'auto',
  1518. 'kerning' => 'auto',
  1519. 'letter-spacing' => 'normal',
  1520. 'lighting-color' => 'white',
  1521. 'marker' => '',
  1522. 'marker-end' => 'none',
  1523. 'marker-mid' => 'none',
  1524. 'marker-start' => 'none',
  1525. 'mask' => 'none',
  1526. 'opacity' => 1,
  1527. 'overflow' => 'auto',
  1528. 'pointer-events' => 'visiblePainted',
  1529. 'shape-rendering' => 'auto',
  1530. 'stop-color' => 'black',
  1531. 'stop-opacity' => 1,
  1532. 'stroke' => 'none',
  1533. 'stroke-dasharray' => 'none',
  1534. 'stroke-dashoffset' => 0,
  1535. 'stroke-linecap' => 'butt',
  1536. 'stroke-linejoin' => 'miter',
  1537. 'stroke-miterlimit' => 4,
  1538. 'stroke-opacity' => 1,
  1539. 'stroke-width' => 1,
  1540. 'text-anchor' => 'start',
  1541. 'text-decoration' => 'none',
  1542. 'text-rendering' => 'auto',
  1543. 'unicode-bidi' => 'normal',
  1544. 'visibility' => 'visible',
  1545. 'word-spacing' => 'normal',
  1546. 'writing-mode' => 'lr-tb',
  1547. 'text-color' => 'black',
  1548. 'transfmatrix' => array(1, 0, 0, 1, 0, 0)
  1549. ));
  1550. //------------------------------------------------------------
  1551. // METHODS
  1552. //------------------------------------------------------------
  1553. /**
  1554. * This is the class constructor.
  1555. * It allows to set up the page format, the orientation and the measure unit used in all the methods (except for the font sizes).
  1556. * @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>
  1557. * @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.
  1558. * @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().
  1559. * @param $unicode (boolean) TRUE means that the input text is unicode (default = true)
  1560. * @param $diskcache (boolean) if TRUE reduce the RAM memory usage by caching temporary data on filesystem (slower).
  1561. * @param $encoding (string) charset encoding; default is UTF-8
  1562. * @public
  1563. * @see getPageSizeFromFormat(), setPageFormat()
  1564. */
  1565. public function __construct($orientation='P', $unit='mm', $format='A4', $unicode=true, $encoding='UTF-8', $diskcache=false) {
  1566. /* Set internal character encoding to ASCII */
  1567. if (function_exists('mb_internal_encoding') AND mb_internal_encoding()) {
  1568. $this->internal_encoding = mb_internal_encoding();
  1569. mb_internal_encoding('ASCII');
  1570. }
  1571. // get array of HTML colors
  1572. require(dirname(__FILE__).'/htmlcolors.php');
  1573. $this->webcolor = $webcolor;
  1574. // get array of custom spot colors
  1575. if (file_exists(dirname(__FILE__).'/spotcolors.php')) {
  1576. require(dirname(__FILE__).'/spotcolors.php');
  1577. $this->spotcolor = $spotcolor;
  1578. } else {
  1579. $this->spotcolor = array();
  1580. }
  1581. require_once(dirname(__FILE__).'/unicode_data.php');
  1582. $this->unicode = new TCPDF_UNICODE_DATA();
  1583. $this->font_obj_ids = array();
  1584. $this->page_obj_id = array();
  1585. $this->form_obj_id = array();
  1586. // set disk caching
  1587. $this->diskcache = $diskcache ? true : false;
  1588. // set language direction
  1589. $this->rtl = false;
  1590. $this->tmprtl = false;
  1591. // some checks
  1592. $this->_dochecks();
  1593. // initialization of properties
  1594. $this->isunicode = $unicode;
  1595. $this->page = 0;
  1596. $this->transfmrk[0] = array();
  1597. $this->pagedim = array();
  1598. $this->n = 2;
  1599. $this->buffer = '';
  1600. $this->pages = array();
  1601. $this->state = 0;
  1602. $this->fonts = array();
  1603. $this->FontFiles = array();
  1604. $this->diffs = array();
  1605. $this->images = array();
  1606. $this->links = array();
  1607. $this->gradients = array();
  1608. $this->InFooter = false;
  1609. $this->lasth = 0;
  1610. $this->FontFamily = 'helvetica';
  1611. $this->FontStyle = '';
  1612. $this->FontSizePt = 12;
  1613. $this->underline = false;
  1614. $this->overline = false;
  1615. $this->linethrough = false;
  1616. $this->DrawColor = '0 G';
  1617. $this->FillColor = '0 g';
  1618. $this->TextColor = '0 g';
  1619. $this->ColorFlag = false;
  1620. $this->pdflayers = array();
  1621. // encryption values
  1622. $this->encrypted = false;
  1623. $this->last_enc_key = '';
  1624. // standard Unicode fonts
  1625. $this->CoreFonts = array(
  1626. 'courier'=>'Courier',
  1627. 'courierB'=>'Courier-Bold',
  1628. 'courierI'=>'Courier-Oblique',
  1629. 'courierBI'=>'Courier-BoldOblique',
  1630. 'helvetica'=>'Helvetica',
  1631. 'helveticaB'=>'Helvetica-Bold',
  1632. 'helveticaI'=>'Helvetica-Oblique',
  1633. 'helveticaBI'=>'Helvetica-BoldOblique',
  1634. 'times'=>'Times-Roman',
  1635. 'timesB'=>'Times-Bold',
  1636. 'timesI'=>'Times-Italic',
  1637. 'timesBI'=>'Times-BoldItalic',
  1638. 'symbol'=>'Symbol',
  1639. 'zapfdingbats'=>'ZapfDingbats'
  1640. );
  1641. // set scale factor
  1642. $this->setPageUnit($unit);
  1643. // set page format and orientation
  1644. $this->setPageFormat($format, $orientation);
  1645. // page margins (1 cm)
  1646. $margin = 28.35 / $this->k;
  1647. $this->SetMargins($margin, $margin);
  1648. // internal cell padding
  1649. $cpadding = $margin / 10;
  1650. $this->setCellPaddings($cpadding, 0, $cpadding, 0);
  1651. // cell margins
  1652. $this->setCellMargins(0, 0, 0, 0);
  1653. // line width (0.2 mm)
  1654. $this->LineWidth = 0.57 / $this->k;
  1655. $this->linestyleWidth = sprintf('%.2F w', ($this->LineWidth * $this->k));
  1656. $this->linestyleCap = '0 J';
  1657. $this->linestyleJoin = '0 j';
  1658. $this->linestyleDash = '[] 0 d';
  1659. // automatic page break
  1660. $this->SetAutoPageBreak(true, (2 * $margin));
  1661. // full width display mode
  1662. $this->SetDisplayMode('fullwidth');
  1663. // compression
  1664. $this->SetCompression(true);
  1665. // set default PDF version number
  1666. $this->PDFVersion = '1.7';
  1667. $this->encoding = $encoding;
  1668. $this->HREF = array();
  1669. $this->getFontsList();
  1670. $this->fgcolor = array('R' => 0, 'G' => 0, 'B' => 0);
  1671. $this->strokecolor = array('R' => 0, 'G' => 0, 'B' => 0);
  1672. $this->bgcolor = array('R' => 255, 'G' => 255, 'B' => 255);
  1673. $this->extgstates = array();
  1674. // user's rights
  1675. $this->sign = false;
  1676. $this->ur['enabled'] = false;
  1677. $this->ur['document'] = '/FullSave';
  1678. $this->ur['annots'] = '/Create/Delete/Modify/Copy/Import/Export';
  1679. $this->ur['form'] = '/Add/Delete/FillIn/Import/Export/SubmitStandalone/SpawnTemplate';
  1680. $this->ur['signature'] = '/Modify';
  1681. $this->ur['ef'] = '/Create/Delete/Modify/Import';
  1682. $this->ur['formex'] = '';
  1683. $this->signature_appearance = array('page' => 1, 'rect' => '0 0 0 0');
  1684. $this->empty_signature_appearance = array();
  1685. // set default JPEG quality
  1686. $this->jpeg_quality = 75;
  1687. // initialize some settings
  1688. $this->utf8Bidi(array(''), '');
  1689. // set default font
  1690. $this->SetFont($this->FontFamily, $this->FontStyle, $this->FontSizePt);
  1691. // check if PCRE Unicode support is enabled
  1692. if ($this->isunicode AND (@preg_match('/\pL/u', 'a') == 1)) {
  1693. // PCRE unicode support is turned ON
  1694. // \p{Z} or \p{Separator}: any kind of Unicode whitespace or invisible separator.
  1695. // \p{Lo} or \p{Other_Letter}: a Unicode letter or ideograph that does not have lowercase and uppercase variants.
  1696. // \p{Lo} is needed because Chinese characters are packed next to each other without spaces in between.
  1697. //$this->setSpacesRE('/[^\S\P{Z}\P{Lo}\xa0]/u');
  1698. $this->setSpacesRE('/[^\S\P{Z}\xa0]/u');
  1699. } else {
  1700. // PCRE unicode support is turned OFF
  1701. $this->setSpacesRE('/[^\S\xa0]/');
  1702. }
  1703. $this->default_form_prop = array('lineWidth'=>1, 'borderStyle'=>'solid', 'fillColor'=>array(255, 255, 255), 'strokeColor'=>array(128, 128, 128));
  1704. // set file ID for trailer
  1705. $this->file_id = md5($this->getRandomSeed('TCPDF'.$orientation.$unit.$format.$encoding));
  1706. // get default graphic vars
  1707. $this->default_graphic_vars = $this->getGraphicVars();
  1708. $this->header_xobj_autoreset = false;
  1709. }
  1710. /**
  1711. * Default destructor.
  1712. * @public
  1713. * @since 1.53.0.TC016
  1714. */
  1715. public function __destruct() {
  1716. // restore internal encoding
  1717. if (isset($this->internal_encoding) AND !empty($this->internal_encoding)) {
  1718. mb_internal_encoding($this->internal_encoding);
  1719. }
  1720. // unset all class variables
  1721. $this->_destroy(true);
  1722. }
  1723. /**
  1724. * Return the current TCPDF version.
  1725. * @return TCPDF version string
  1726. * @public
  1727. * @since 5.9.012 (2010-11-10)
  1728. */
  1729. public function getTCPDFVersion() {
  1730. return $this->tcpdf_version;
  1731. }
  1732. /**
  1733. * Set the units of measure for the document.
  1734. * @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.
  1735. * @public
  1736. * @since 3.0.015 (2008-06-06)
  1737. */
  1738. public function setPageUnit($unit) {
  1739. $unit = strtolower($unit);
  1740. //Set scale factor
  1741. switch ($unit) {
  1742. // points
  1743. case 'px':
  1744. case 'pt': {
  1745. $this->k = 1;
  1746. break;
  1747. }
  1748. // millimeters
  1749. case 'mm': {
  1750. $this->k = $this->dpi / 25.4;
  1751. break;
  1752. }
  1753. // centimeters
  1754. case 'cm': {
  1755. $this->k = $this->dpi / 2.54;
  1756. break;
  1757. }
  1758. // inches
  1759. case 'in': {
  1760. $this->k = $this->dpi;
  1761. break;
  1762. }
  1763. // unsupported unit
  1764. default : {
  1765. $this->Error('Incorrect unit: '.$unit);
  1766. break;
  1767. }
  1768. }
  1769. $this->pdfunit = $unit;
  1770. if (isset($this->CurOrientation)) {
  1771. $this->setPageOrientation($this->CurOrientation);
  1772. }
  1773. }
  1774. /**
  1775. * Get page dimensions from format name.
  1776. * @param $format (mixed) The format name. It can be: <ul>
  1777. * <li><b>ISO 216 A Series + 2 SIS 014711 extensions</b></li>
  1778. * <li>A0 (841x1189 mm ; 33.11x46.81 in)</li>
  1779. * <li>A1 (594x841 mm ; 23.39x33.11 in)</li>
  1780. * <li>A2 (420x594 mm ; 16.54x23.39 in)</li>
  1781. * <li>A3 (297x420 mm ; 11.69x16.54 in)</li>
  1782. * <li>A4 (210x297 mm ; 8.27x11.69 in)</li>
  1783. * <li>A5 (148x210 mm ; 5.83x8.27 in)</li>
  1784. * <li>A6 (105x148 mm ; 4.13x5.83 in)</li>
  1785. * <li>A7 (74x105 mm ; 2.91x4.13 in)</li>
  1786. * <li>A8 (52x74 mm ; 2.05x2.91 in)</li>
  1787. * <li>A9 (37x52 mm ; 1.46x2.05 in)</li>
  1788. * <li>A10 (26x37 mm ; 1.02x1.46 in)</li>
  1789. * <li>A11 (18x26 mm ; 0.71x1.02 in)</li>
  1790. * <li>A12 (13x18 mm ; 0.51x0.71 in)</li>
  1791. * <li><b>ISO 216 B Series + 2 SIS 014711 extensions</b></li>
  1792. * <li>B0 (1000x1414 mm ; 39.37x55.67 in)</li>
  1793. * <li>B1 (707x1000 mm ; 27.83x39.37 in)</li>
  1794. * <li>B2 (500x707 mm ; 19.69x27.83 in)</li>
  1795. * <li>B3 (353x500 mm ; 13.90x19.69 in)</li>
  1796. * <li>B4 (250x353 mm ; 9.84x13.90 in)</li>
  1797. * <li>B5 (176x250 mm ; 6.93x9.84 in)</li>
  1798. * <li>B6 (125x176 mm ; 4.92x6.93 in)</li>
  1799. * <li>B7 (88x125 mm ; 3.46x4.92 in)</li>
  1800. * <li>B8 (62x88 mm ; 2.44x3.46 in)</li>
  1801. * <li>B9 (44x62 mm ; 1.73x2.44 in)</li>
  1802. * <li>B10 (31x44 mm ; 1.22x1.73 in)</li>
  1803. * <li>B11 (22x31 mm ; 0.87x1.22 in)</li>
  1804. * <li>B12 (15x22 mm ; 0.59x0.87 in)</li>
  1805. * <li><b>ISO 216 C Series + 2 SIS 014711 extensions + 2 EXTENSION</b></li>
  1806. * <li>C0 (917x1297 mm ; 36.10x51.06 in)</li>
  1807. * <li>C1 (648x917 mm ; 25.51x36.10 in)</li>
  1808. * <li>C2 (458x648 mm ; 18.03x25.51 in)</li>
  1809. * <li>C3 (324x458 mm ; 12.76x18.03 in)</li>
  1810. * <li>C4 (229x324 mm ; 9.02x12.76 in)</li>
  1811. * <li>C5 (162x229 mm ; 6.38x9.02 in)</li>
  1812. * <li>C6 (114x162 mm ; 4.49x6.38 in)</li>
  1813. * <li>C7 (81x114 mm ; 3.19x4.49 in)</li>
  1814. * <li>C8 (57x81 mm ; 2.24x3.19 in)</li>
  1815. * <li>C9 (40x57 mm ; 1.57x2.24 in)</li>
  1816. * <li>C10 (28x40 mm ; 1.10x1.57 in)</li>
  1817. * <li>C11 (20x28 mm ; 0.79x1.10 in)</li>
  1818. * <li>C12 (14x20 mm ; 0.55x0.79 in)</li>
  1819. * <li>C76 (81x162 mm ; 3.19x6.38 in)</li>
  1820. * <li>DL (110x220 mm ; 4.33x8.66 in)</li>
  1821. * <li><b>SIS 014711 E Series</b></li>
  1822. * <li>E0 (879x1241 mm ; 34.61x48.86 in)</li>
  1823. * <li>E1 (620x879 mm ; 24.41x34.61 in)</li>
  1824. * <li>E2 (440x620 mm ; 17.32x24.41 in)</li>
  1825. * <li>E3 (310x440 mm ; 12.20x17.32 in)</li>
  1826. * <li>E4 (220x310 mm ; 8.66x12.20 in)</li>
  1827. * <li>E5 (155x220 mm ; 6.10x8.66 in)</li>
  1828. * <li>E6 (110x155 mm ; 4.33x6.10 in)</li>
  1829. * <li>E7 (78x110 mm ; 3.07x4.33 in)</li>
  1830. * <li>E8 (55x78 mm ; 2.17x3.07 in)</li>
  1831. * <li>E9 (39x55 mm ; 1.54x2.17 in)</li>
  1832. * <li>E10 (27x39 mm ; 1.06x1.54 in)</li>
  1833. * <li>E11 (19x27 mm ; 0.75x1.06 in)</li>
  1834. * <li>E12 (13x19 mm ; 0.51x0.75 in)</li>
  1835. * <li><b>SIS 014711 G Series</b></li>
  1836. * <li>G0 (958x1354 mm ; 37.72x53.31 in)</li>
  1837. * <li>G1 (677x958 mm ; 26.65x37.72 in)</li>
  1838. * <li>G2 (479x677 mm ; 18.86x26.65 in)</li>
  1839. * <li>G3 (338x479 mm ; 13.31x18.86 in)</li>
  1840. * <li>G4 (239x338 mm ; 9.41x13.31 in)</li>
  1841. * <li>G5 (169x239 mm ; 6.65x9.41 in)</li>
  1842. * <li>G6 (119x169 mm ; 4.69x6.65 in)</li>
  1843. * <li>G7 (84x119 mm ; 3.31x4.69 in)</li>
  1844. * <li>G8 (59x84 mm ; 2.32x3.31 in)</li>
  1845. * <li>G9 (42x59 mm ; 1.65x2.32 in)</li>
  1846. * <li>G10 (29x42 mm ; 1.14x1.65 in)</li>
  1847. * <li>G11 (21x29 mm ; 0.83x1.14 in)</li>
  1848. * <li>G12 (14x21 mm ; 0.55x0.83 in)</li>
  1849. * <li><b>ISO Press</b></li>
  1850. * <li>RA0 (860x1220 mm ; 33.86x48.03 in)</li>
  1851. * <li>RA1 (610x860 mm ; 24.02x33.86 in)</li>
  1852. * <li>RA2 (430x610 mm ; 16.93x24.02 in)</li>
  1853. * <li>RA3 (305x430 mm ; 12.01x16.93 in)</li>
  1854. * <li>RA4 (215x305 mm ; 8.46x12.01 in)</li>
  1855. * <li>SRA0 (900x1280 mm ; 35.43x50.39 in)</li>
  1856. * <li>SRA1 (640x900 mm ; 25.20x35.43 in)</li>
  1857. * <li>SRA2 (450x640 mm ; 17.72x25.20 in)</li>
  1858. * <li>SRA3 (320x450 mm ; 12.60x17.72 in)</li>
  1859. * <li>SRA4 (225x320 mm ; 8.86x12.60 in)</li>
  1860. * <li><b>German DIN 476</b></li>
  1861. * <li>4A0 (1682x2378 mm ; 66.22x93.62 in)</li>
  1862. * <li>2A0 (1189x1682 mm ; 46.81x66.22 in)</li>
  1863. * <li><b>Variations on the ISO Standard</b></li>
  1864. * <li>A2_EXTRA (445x619 mm ; 17.52x24.37 in)</li>
  1865. * <li>A3+ (329x483 mm ; 12.95x19.02 in)</li>
  1866. * <li>A3_EXTRA (322x445 mm ; 12.68x17.52 in)</li>
  1867. * <li>A3_SUPER (305x508 mm ; 12.01x20.00 in)</li>
  1868. * <li>SUPER_A3 (305x487 mm ; 12.01x19.17 in)</li>
  1869. * <li>A4_EXTRA (235x322 mm ; 9.25x12.68 in)</li>
  1870. * <li>A4_SUPER (229x322 mm ; 9.02x12.68 in)</li>
  1871. * <li>SUPER_A4 (227x356 mm ; 8.94x14.02 in)</li>
  1872. * <li>A4_LONG (210x348 mm ; 8.27x13.70 in)</li>
  1873. * <li>F4 (210x330 mm ; 8.27x12.99 in)</li>
  1874. * <li>SO_B5_EXTRA (202x276 mm ; 7.95x10.87 in)</li>
  1875. * <li>A5_EXTRA (173x235 mm ; 6.81x9.25 in)</li>
  1876. * <li><b>ANSI Series</b></li>
  1877. * <li>ANSI_E (864x1118 mm ; 34.00x44.00 in)</li>
  1878. * <li>ANSI_D (559x864 mm ; 22.00x34.00 in)</li>
  1879. * <li>ANSI_C (432x559 mm ; 17.00x22.00 in)</li>
  1880. * <li>ANSI_B (279x432 mm ; 11.00x17.00 in)</li>
  1881. * <li>ANSI_A (216x279 mm ; 8.50x11.00 in)</li>
  1882. * <li><b>Traditional 'Loose' North American Paper Sizes</b></li>
  1883. * <li>LEDGER, USLEDGER (432x279 mm ; 17.00x11.00 in)</li>
  1884. * <li>TABLOID, USTABLOID, BIBLE, ORGANIZERK (279x432 mm ; 11.00x17.00 in)</li>
  1885. * <li>LETTER, USLETTER, ORGANIZERM (216x279 mm ; 8.50x11.00 in)</li>
  1886. * <li>LEGAL, USLEGAL (216x356 mm ; 8.50x14.00 in)</li>
  1887. * <li>GLETTER, GOVERNMENTLETTER (203x267 mm ; 8.00x10.50 in)</li>
  1888. * <li>JLEGAL, JUNIORLEGAL (203x127 mm ; 8.00x5.00 in)</li>
  1889. * <li><b>Other North American Paper Sizes</b></li>
  1890. * <li>QUADDEMY (889x1143 mm ; 35.00x45.00 in)</li>
  1891. * <li>SUPER_B (330x483 mm ; 13.00x19.00 in)</li>
  1892. * <li>QUARTO (229x279 mm ; 9.00x11.00 in)</li>
  1893. * <li>FOLIO, GOVERNMENTLEGAL (216x330 mm ; 8.50x13.00 in)</li>
  1894. * <li>EXECUTIVE, MONARCH (184x267 mm ; 7.25x10.50 in)</li>
  1895. * <li>MEMO, STATEMENT, ORGANIZERL (140x216 mm ; 5.50x8.50 in)</li>
  1896. * <li>FOOLSCAP (210x330 mm ; 8.27x13.00 in)</li>
  1897. * <li>COMPACT (108x171 mm ; 4.25x6.75 in)</li>
  1898. * <li>ORGANIZERJ (70x127 mm ; 2.75x5.00 in)</li>
  1899. * <li><b>Canadian standard CAN 2-9.60M</b></li>
  1900. * <li>P1 (560x860 mm ; 22.05x33.86 in)</li>
  1901. * <li>P2 (430x560 mm ; 16.93x22.05 in)</li>
  1902. * <li>P3 (280x430 mm ; 11.02x16.93 in)</li>
  1903. * <li>P4 (215x280 mm ; 8.46x11.02 in)</li>
  1904. * <li>P5 (140x215 mm ; 5.51x8.46 in)</li>
  1905. * <li>P6 (107x140 mm ; 4.21x5.51 in)</li>
  1906. * <li><b>North American Architectural Sizes</b></li>
  1907. * <li>ARCH_E (914x1219 mm ; 36.00x48.00 in)</li>
  1908. * <li>ARCH_E1 (762x1067 mm ; 30.00x42.00 in)</li>
  1909. * <li>ARCH_D (610x914 mm ; 24.00x36.00 in)</li>
  1910. * <li>ARCH_C, BROADSHEET (457x610 mm ; 18.00x24.00 in)</li>
  1911. * <li>ARCH_B (305x457 mm ; 12.00x18.00 in)</li>
  1912. * <li>ARCH_A (229x305 mm ; 9.00x12.00 in)</li>
  1913. * <li><b>Announcement Envelopes</b></li>
  1914. * <li>ANNENV_A2 (111x146 mm ; 4.37x5.75 in)</li>
  1915. * <li>ANNENV_A6 (121x165 mm ; 4.75x6.50 in)</li>
  1916. * <li>ANNENV_A7 (133x184 mm ; 5.25x7.25 in)</li>
  1917. * <li>ANNENV_A8 (140x206 mm ; 5.50x8.12 in)</li>
  1918. * <li>ANNENV_A10 (159x244 mm ; 6.25x9.62 in)</li>
  1919. * <li>ANNENV_SLIM (98x225 mm ; 3.87x8.87 in)</li>
  1920. * <li><b>Commercial Envelopes</b></li>
  1921. * <li>COMMENV_N6_1/4 (89x152 mm ; 3.50x6.00 in)</li>
  1922. * <li>COMMENV_N6_3/4 (92x165 mm ; 3.62x6.50 in)</li>
  1923. * <li>COMMENV_N8 (98x191 mm ; 3.87x7.50 in)</li>
  1924. * <li>COMMENV_N9 (98x225 mm ; 3.87x8.87 in)</li>
  1925. * <li>COMMENV_N10 (105x241 mm ; 4.12x9.50 in)</li>
  1926. * <li>COMMENV_N11 (114x263 mm ; 4.50x10.37 in)</li>
  1927. * <li>COMMENV_N12 (121x279 mm ; 4.75x11.00 in)</li>
  1928. * <li>COMMENV_N14 (127x292 mm ; 5.00x11.50 in)</li>
  1929. * <li><b>Catalogue Envelopes</b></li>
  1930. * <li>CATENV_N1 (152x229 mm ; 6.00x9.00 in)</li>
  1931. * <li>CATENV_N1_3/4 (165x241 mm ; 6.50x9.50 in)</li>
  1932. * <li>CATENV_N2 (165x254 mm ; 6.50x10.00 in)</li>
  1933. * <li>CATENV_N3 (178x254 mm ; 7.00x10.00 in)</li>
  1934. * <li>CATENV_N6 (191x267 mm ; 7.50x10.50 in)</li>
  1935. * <li>CATENV_N7 (203x279 mm ; 8.00x11.00 in)</li>
  1936. * <li>CATENV_N8 (210x286 mm ; 8.25x11.25 in)</li>
  1937. * <li>CATENV_N9_1/2 (216x267 mm ; 8.50x10.50 in)</li>
  1938. * <li>CATENV_N9_3/4 (222x286 mm ; 8.75x11.25 in)</li>
  1939. * <li>CATENV_N10_1/2 (229x305 mm ; 9.00x12.00 in)</li>
  1940. * <li>CATENV_N12_1/2 (241x318 mm ; 9.50x12.50 in)</li>
  1941. * <li>CATENV_N13_1/2 (254x330 mm ; 10.00x13.00 in)</li>
  1942. * <li>CATENV_N14_1/4 (286x311 mm ; 11.25x12.25 in)</li>
  1943. * <li>CATENV_N14_1/2 (292x368 mm ; 11.50x14.50 in)</li>
  1944. * <li><b>Japanese (JIS P 0138-61) Standard B-Series</b></li>
  1945. * <li>JIS_B0 (1030x1456 mm ; 40.55x57.32 in)</li>
  1946. * <li>JIS_B1 (728x1030 mm ; 28.66x40.55 in)</li>
  1947. * <li>JIS_B2 (515x728 mm ; 20.28x28.66 in)</li>
  1948. * <li>JIS_B3 (364x515 mm ; 14.33x20.28 in)</li>
  1949. * <li>JIS_B4 (257x364 mm ; 10.12x14.33 in)</li>
  1950. * <li>JIS_B5 (182x257 mm ; 7.17x10.12 in)</li>
  1951. * <li>JIS_B6 (128x182 mm ; 5.04x7.17 in)</li>
  1952. * <li>JIS_B7 (91x128 mm ; 3.58x5.04 in)</li>
  1953. * <li>JIS_B8 (64x91 mm ; 2.52x3.58 in)</li>
  1954. * <li>JIS_B9 (45x64 mm ; 1.77x2.52 in)</li>
  1955. * <li>JIS_B10 (32x45 mm ; 1.26x1.77 in)</li>
  1956. * <li>JIS_B11 (22x32 mm ; 0.87x1.26 in)</li>
  1957. * <li>JIS_B12 (16x22 mm ; 0.63x0.87 in)</li>
  1958. * <li><b>PA Series</b></li>
  1959. * <li>PA0 (840x1120 mm ; 33.07x44.09 in)</li>
  1960. * <li>PA1 (560x840 mm ; 22.05x33.07 in)</li>
  1961. * <li>PA2 (420x560 mm ; 16.54x22.05 in)</li>
  1962. * <li>PA3 (280x420 mm ; 11.02x16.54 in)</li>
  1963. * <li>PA4 (210x280 mm ; 8.27x11.02 in)</li>
  1964. * <li>PA5 (140x210 mm ; 5.51x8.27 in)</li>
  1965. * <li>PA6 (105x140 mm ; 4.13x5.51 in)</li>
  1966. * <li>PA7 (70x105 mm ; 2.76x4.13 in)</li>
  1967. * <li>PA8 (52x70 mm ; 2.05x2.76 in)</li>
  1968. * <li>PA9 (35x52 mm ; 1.38x2.05 in)</li>
  1969. * <li>PA10 (26x35 mm ; 1.02x1.38 in)</li>
  1970. * <li><b>Standard Photographic Print Sizes</b></li>
  1971. * <li>PASSPORT_PHOTO (35x45 mm ; 1.38x1.77 in)</li>
  1972. * <li>E (82x120 mm ; 3.25x4.72 in)</li>
  1973. * <li>3R, L (89x127 mm ; 3.50x5.00 in)</li>
  1974. * <li>4R, KG (102x152 mm ; 4.02x5.98 in)</li>
  1975. * <li>4D (120x152 mm ; 4.72x5.98 in)</li>
  1976. * <li>5R, 2L (127x178 mm ; 5.00x7.01 in)</li>
  1977. * <li>6R, 8P (152x203 mm ; 5.98x7.99 in)</li>
  1978. * <li>8R, 6P (203x254 mm ; 7.99x10.00 in)</li>
  1979. * <li>S8R, 6PW (203x305 mm ; 7.99x12.01 in)</li>
  1980. * <li>10R, 4P (254x305 mm ; 10.00x12.01 in)</li>
  1981. * <li>S10R, 4PW (254x381 mm ; 10.00x15.00 in)</li>
  1982. * <li>11R (279x356 mm ; 10.98x14.02 in)</li>
  1983. * <li>S11R (279x432 mm ; 10.98x17.01 in)</li>
  1984. * <li>12R (305x381 mm ; 12.01x15.00 in)</li>
  1985. * <li>S12R (305x456 mm ; 12.01x17.95 in)</li>
  1986. * <li><b>Common Newspaper Sizes</b></li>
  1987. * <li>NEWSPAPER_BROADSHEET (750x600 mm ; 29.53x23.62 in)</li>
  1988. * <li>NEWSPAPER_BERLINER (470x315 mm ; 18.50x12.40 in)</li>
  1989. * <li>NEWSPAPER_COMPACT, NEWSPAPER_TABLOID (430x280 mm ; 16.93x11.02 in)</li>
  1990. * <li><b>Business Cards</b></li>
  1991. * <li>CREDIT_CARD, BUSINESS_CARD, BUSINESS_CARD_ISO7810 (54x86 mm ; 2.13x3.37 in)</li>
  1992. * <li>BUSINESS_CARD_ISO216 (52x74 mm ; 2.05x2.91 in)</li>
  1993. * <li>BUSINESS_CARD_IT, BUSINESS_CARD_UK, BUSINESS_CARD_FR, BUSINESS_CARD_DE, BUSINESS_CARD_ES (55x85 mm ; 2.17x3.35 in)</li>
  1994. * <li>BUSINESS_CARD_US, BUSINESS_CARD_CA (51x89 mm ; 2.01x3.50 in)</li>
  1995. * <li>BUSINESS_CARD_JP (55x91 mm ; 2.17x3.58 in)</li>
  1996. * <li>BUSINESS_CARD_HK (54x90 mm ; 2.13x3.54 in)</li>
  1997. * <li>BUSINESS_CARD_AU, BUSINESS_CARD_DK, BUSINESS_CARD_SE (55x90 mm ; 2.17x3.54 in)</li>
  1998. * <li>BUSINESS_CARD_RU, BUSINESS_CARD_CZ, BUSINESS_CARD_FI, BUSINESS_CARD_HU, BUSINESS_CARD_IL (50x90 mm ; 1.97x3.54 in)</li>
  1999. * <li><b>Billboards</b></li>
  2000. * <li>4SHEET (1016x1524 mm ; 40.00x60.00 in)</li>
  2001. * <li>6SHEET (1200x1800 mm ; 47.24x70.87 in)</li>
  2002. * <li>12SHEET (3048x1524 mm ; 120.00x60.00 in)</li>
  2003. * <li>16SHEET (2032x3048 mm ; 80.00x120.00 in)</li>
  2004. * <li>32SHEET (4064x3048 mm ; 160.00x120.00 in)</li>
  2005. * <li>48SHEET (6096x3048 mm ; 240.00x120.00 in)</li>
  2006. * <li>64SHEET (8128x3048 mm ; 320.00x120.00 in)</li>
  2007. * <li>96SHEET (12192x3048 mm ; 480.00x120.00 in)</li>
  2008. * <li><b>Old Imperial English (some are still used in USA)</b></li>
  2009. * <li>EN_EMPEROR (1219x1829 mm ; 48.00x72.00 in)</li>
  2010. * <li>EN_ANTIQUARIAN (787x1346 mm ; 31.00x53.00 in)</li>
  2011. * <li>EN_GRAND_EAGLE (730x1067 mm ; 28.75x42.00 in)</li>
  2012. * <li>EN_DOUBLE_ELEPHANT (679x1016 mm ; 26.75x40.00 in)</li>
  2013. * <li>EN_ATLAS (660x864 mm ; 26.00x34.00 in)</li>
  2014. * <li>EN_COLOMBIER (597x876 mm ; 23.50x34.50 in)</li>
  2015. * <li>EN_ELEPHANT (584x711 mm ; 23.00x28.00 in)</li>
  2016. * <li>EN_DOUBLE_DEMY (572x902 mm ; 22.50x35.50 in)</li>
  2017. * <li>EN_IMPERIAL (559x762 mm ; 22.00x30.00 in)</li>
  2018. * <li>EN_PRINCESS (546x711 mm ; 21.50x28.00 in)</li>
  2019. * <li>EN_CARTRIDGE (533x660 mm ; 21.00x26.00 in)</li>
  2020. * <li>EN_DOUBLE_LARGE_POST (533x838 mm ; 21.00x33.00 in)</li>
  2021. * <li>EN_ROYAL (508x635 mm ; 20.00x25.00 in)</li>
  2022. * <li>EN_SHEET, EN_HALF_POST (495x597 mm ; 19.50x23.50 in)</li>
  2023. * <li>EN_SUPER_ROYAL (483x686 mm ; 19.00x27.00 in)</li>
  2024. * <li>EN_DOUBLE_POST (483x775 mm ; 19.00x30.50 in)</li>
  2025. * <li>EN_MEDIUM (445x584 mm ; 17.50x23.00 in)</li>
  2026. * <li>EN_DEMY (445x572 mm ; 17.50x22.50 in)</li>
  2027. * <li>EN_LARGE_POST (419x533 mm ; 16.50x21.00 in)</li>
  2028. * <li>EN_COPY_DRAUGHT (406x508 mm ; 16.00x20.00 in)</li>
  2029. * <li>EN_POST (394x489 mm ; 15.50x19.25 in)</li>
  2030. * <li>EN_CROWN (381x508 mm ; 15.00x20.00 in)</li>
  2031. * <li>EN_PINCHED_POST (375x470 mm ; 14.75x18.50 in)</li>
  2032. * <li>EN_BRIEF (343x406 mm ; 13.50x16.00 in)</li>
  2033. * <li>EN_FOOLSCAP (343x432 mm ; 13.50x17.00 in)</li>
  2034. * <li>EN_SMALL_FOOLSCAP (337x419 mm ; 13.25x16.50 in)</li>
  2035. * <li>EN_POTT (318x381 mm ; 12.50x15.00 in)</li>
  2036. * <li><b>Old Imperial Belgian</b></li>
  2037. * <li>BE_GRAND_AIGLE (700x1040 mm ; 27.56x40.94 in)</li>
  2038. * <li>BE_COLOMBIER (620x850 mm ; 24.41x33.46 in)</li>
  2039. * <li>BE_DOUBLE_CARRE (620x920 mm ; 24.41x36.22 in)</li>
  2040. * <li>BE_ELEPHANT (616x770 mm ; 24.25x30.31 in)</li>
  2041. * <li>BE_PETIT_AIGLE (600x840 mm ; 23.62x33.07 in)</li>
  2042. * <li>BE_GRAND_JESUS (550x730 mm ; 21.65x28.74 in)</li>
  2043. * <li>BE_JESUS (540x730 mm ; 21.26x28.74 in)</li>
  2044. * <li>BE_RAISIN (500x650 mm ; 19.69x25.59 in)</li>
  2045. * <li>BE_GRAND_MEDIAN (460x605 mm ; 18.11x23.82 in)</li>
  2046. * <li>BE_DOUBLE_POSTE (435x565 mm ; 17.13x22.24 in)</li>
  2047. * <li>BE_COQUILLE (430x560 mm ; 16.93x22.05 in)</li>
  2048. * <li>BE_PETIT_MEDIAN (415x530 mm ; 16.34x20.87 in)</li>
  2049. * <li>BE_RUCHE (360x460 mm ; 14.17x18.11 in)</li>
  2050. * <li>BE_PROPATRIA (345x430 mm ; 13.58x16.93 in)</li>
  2051. * <li>BE_LYS (317x397 mm ; 12.48x15.63 in)</li>
  2052. * <li>BE_POT (307x384 mm ; 12.09x15.12 in)</li>
  2053. * <li>BE_ROSETTE (270x347 mm ; 10.63x13.66 in)</li>
  2054. * <li><b>Old Imperial French</b></li>
  2055. * <li>FR_UNIVERS (1000x1300 mm ; 39.37x51.18 in)</li>
  2056. * <li>FR_DOUBLE_COLOMBIER (900x1260 mm ; 35.43x49.61 in)</li>
  2057. * <li>FR_GRANDE_MONDE (900x1260 mm ; 35.43x49.61 in)</li>
  2058. * <li>FR_DOUBLE_SOLEIL (800x1200 mm ; 31.50x47.24 in)</li>
  2059. * <li>FR_DOUBLE_JESUS (760x1120 mm ; 29.92x44.09 in)</li>
  2060. * <li>FR_GRAND_AIGLE (750x1060 mm ; 29.53x41.73 in)</li>
  2061. * <li>FR_PETIT_AIGLE (700x940 mm ; 27.56x37.01 in)</li>
  2062. * <li>FR_DOUBLE_RAISIN (650x1000 mm ; 25.59x39.37 in)</li>
  2063. * <li>FR_JOURNAL (650x940 mm ; 25.59x37.01 in)</li>
  2064. * <li>FR_COLOMBIER_AFFICHE (630x900 mm ; 24.80x35.43 in)</li>
  2065. * <li>FR_DOUBLE_CAVALIER (620x920 mm ; 24.41x36.22 in)</li>
  2066. * <li>FR_CLOCHE (600x800 mm ; 23.62x31.50 in)</li>
  2067. * <li>FR_SOLEIL (600x800 mm ; 23.62x31.50 in)</li>
  2068. * <li>FR_DOUBLE_CARRE (560x900 mm ; 22.05x35.43 in)</li>
  2069. * <li>FR_DOUBLE_COQUILLE (560x880 mm ; 22.05x34.65 in)</li>
  2070. * <li>FR_JESUS (560x760 mm ; 22.05x29.92 in)</li>
  2071. * <li>FR_RAISIN (500x650 mm ; 19.69x25.59 in)</li>
  2072. * <li>FR_CAVALIER (460x620 mm ; 18.11x24.41 in)</li>
  2073. * <li>FR_DOUBLE_COURONNE (460x720 mm ; 18.11x28.35 in)</li>
  2074. * <li>FR_CARRE (450x560 mm ; 17.72x22.05 in)</li>
  2075. * <li>FR_COQUILLE (440x560 mm ; 17.32x22.05 in)</li>
  2076. * <li>FR_DOUBLE_TELLIERE (440x680 mm ; 17.32x26.77 in)</li>
  2077. * <li>FR_DOUBLE_CLOCHE (400x600 mm ; 15.75x23.62 in)</li>
  2078. * <li>FR_DOUBLE_POT (400x620 mm ; 15.75x24.41 in)</li>
  2079. * <li>FR_ECU (400x520 mm ; 15.75x20.47 in)</li>
  2080. * <li>FR_COURONNE (360x460 mm ; 14.17x18.11 in)</li>
  2081. * <li>FR_TELLIERE (340x440 mm ; 13.39x17.32 in)</li>
  2082. * <li>FR_POT (310x400 mm ; 12.20x15.75 in)</li>
  2083. * </ul>
  2084. * @return array containing page width and height in points
  2085. * @public
  2086. * @since 5.0.010 (2010-05-17)
  2087. */
  2088. public function getPageSizeFromFormat($format) {
  2089. // Paper cordinates are calculated in this way: (inches * 72) where (1 inch = 25.4 mm)
  2090. switch (strtoupper($format)) {
  2091. // ISO 216 A Series + 2 SIS 014711 extensions
  2092. case 'A0' : {$pf = array( 2383.937, 3370.394); break;}
  2093. case 'A1' : {$pf = array( 1683.780, 2383.937); break;}
  2094. case 'A2' : {$pf = array( 1190.551, 1683.780); break;}
  2095. case 'A3' : {$pf = array( 841.890, 1190.551); break;}
  2096. case 'A4' : {$pf = array( 595.276, 841.890); break;}
  2097. case 'A5' : {$pf = array( 419.528, 595.276); break;}
  2098. case 'A6' : {$pf = array( 297.638, 419.528); break;}
  2099. case 'A7' : {$pf = array( 209.764, 297.638); break;}
  2100. case 'A8' : {$pf = array( 147.402, 209.764); break;}
  2101. case 'A9' : {$pf = array( 104.882, 147.402); break;}
  2102. case 'A10': {$pf = array( 73.701, 104.882); break;}
  2103. case 'A11': {$pf = array( 51.024, 73.701); break;}
  2104. case 'A12': {$pf = array( 36.850, 51.024); break;}
  2105. // ISO 216 B Series + 2 SIS 014711 extensions
  2106. case 'B0' : {$pf = array( 2834.646, 4008.189); break;}
  2107. case 'B1' : {$pf = array( 2004.094, 2834.646); break;}
  2108. case 'B2' : {$pf = array( 1417.323, 2004.094); break;}
  2109. case 'B3' : {$pf = array( 1000.630, 1417.323); break;}
  2110. case 'B4' : {$pf = array( 708.661, 1000.630); break;}
  2111. case 'B5' : {$pf = array( 498.898, 708.661); break;}
  2112. case 'B6' : {$pf = array( 354.331, 498.898); break;}
  2113. case 'B7' : {$pf = array( 249.449, 354.331); break;}
  2114. case 'B8' : {$pf = array( 175.748, 249.449); break;}
  2115. case 'B9' : {$pf = array( 124.724, 175.748); break;}
  2116. case 'B10': {$pf = array( 87.874, 124.724); break;}
  2117. case 'B11': {$pf = array( 62.362, 87.874); break;}
  2118. case 'B12': {$pf = array( 42.520, 62.362); break;}
  2119. // ISO 216 C Series + 2 SIS 014711 extensions + 2 EXTENSION
  2120. case 'C0' : {$pf = array( 2599.370, 3676.535); break;}
  2121. case 'C1' : {$pf = array( 1836.850, 2599.370); break;}
  2122. case 'C2' : {$pf = array( 1298.268, 1836.850); break;}
  2123. case 'C3' : {$pf = array( 918.425, 1298.268); break;}
  2124. case 'C4' : {$pf = array( 649.134, 918.425); break;}
  2125. case 'C5' : {$pf = array( 459.213, 649.134); break;}
  2126. case 'C6' : {$pf = array( 323.150, 459.213); break;}
  2127. case 'C7' : {$pf = array( 229.606, 323.150); break;}
  2128. case 'C8' : {$pf = array( 161.575, 229.606); break;}
  2129. case 'C9' : {$pf = array( 113.386, 161.575); break;}
  2130. case 'C10': {$pf = array( 79.370, 113.386); break;}
  2131. case 'C11': {$pf = array( 56.693, 79.370); break;}
  2132. case 'C12': {$pf = array( 39.685, 56.693); break;}
  2133. case 'C76': {$pf = array( 229.606, 459.213); break;}
  2134. case 'DL' : {$pf = array( 311.811, 623.622); break;}
  2135. // SIS 014711 E Series
  2136. case 'E0' : {$pf = array( 2491.654, 3517.795); break;}
  2137. case 'E1' : {$pf = array( 1757.480, 2491.654); break;}
  2138. case 'E2' : {$pf = array( 1247.244, 1757.480); break;}
  2139. case 'E3' : {$pf = array( 878.740, 1247.244); break;}
  2140. case 'E4' : {$pf = array( 623.622, 878.740); break;}
  2141. case 'E5' : {$pf = array( 439.370, 623.622); break;}
  2142. case 'E6' : {$pf = array( 311.811, 439.370); break;}
  2143. case 'E7' : {$pf = array( 221.102, 311.811); break;}
  2144. case 'E8' : {$pf = array( 155.906, 221.102); break;}
  2145. case 'E9' : {$pf = array( 110.551, 155.906); break;}
  2146. case 'E10': {$pf = array( 76.535, 110.551); break;}
  2147. case 'E11': {$pf = array( 53.858, 76.535); break;}
  2148. case 'E12': {$pf = array( 36.850, 53.858); break;}
  2149. // SIS 014711 G Series
  2150. case 'G0' : {$pf = array( 2715.591, 3838.110); break;}
  2151. case 'G1' : {$pf = array( 1919.055, 2715.591); break;}
  2152. case 'G2' : {$pf = array( 1357.795, 1919.055); break;}
  2153. case 'G3' : {$pf = array( 958.110, 1357.795); break;}
  2154. case 'G4' : {$pf = array( 677.480, 958.110); break;}
  2155. case 'G5' : {$pf = array( 479.055, 677.480); break;}
  2156. case 'G6' : {$pf = array( 337.323, 479.055); break;}
  2157. case 'G7' : {$pf = array( 238.110, 337.323); break;}
  2158. case 'G8' : {$pf = array( 167.244, 238.110); break;}
  2159. case 'G9' : {$pf = array( 119.055, 167.244); break;}
  2160. case 'G10': {$pf = array( 82.205, 119.055); break;}
  2161. case 'G11': {$pf = array( 59.528, 82.205); break;}
  2162. case 'G12': {$pf = array( 39.685, 59.528); break;}
  2163. // ISO Press
  2164. case 'RA0': {$pf = array( 2437.795, 3458.268); break;}
  2165. case 'RA1': {$pf = array( 1729.134, 2437.795); break;}
  2166. case 'RA2': {$pf = array( 1218.898, 1729.134); break;}
  2167. case 'RA3': {$pf = array( 864.567, 1218.898); break;}
  2168. case 'RA4': {$pf = array( 609.449, 864.567); break;}
  2169. case 'SRA0': {$pf = array( 2551.181, 3628.346); break;}
  2170. case 'SRA1': {$pf = array( 1814.173, 2551.181); break;}
  2171. case 'SRA2': {$pf = array( 1275.591, 1814.173); break;}
  2172. case 'SRA3': {$pf = array( 907.087, 1275.591); break;}
  2173. case 'SRA4': {$pf = array( 637.795, 907.087); break;}
  2174. // German DIN 476
  2175. case '4A0': {$pf = array( 4767.874, 6740.787); break;}
  2176. case '2A0': {$pf = array( 3370.394, 4767.874); break;}
  2177. // Variations on the ISO Standard
  2178. case 'A2_EXTRA' : {$pf = array( 1261.417, 1754.646); break;}
  2179. case 'A3+' : {$pf = array( 932.598, 1369.134); break;}
  2180. case 'A3_EXTRA' : {$pf = array( 912.756, 1261.417); break;}
  2181. case 'A3_SUPER' : {$pf = array( 864.567, 1440.000); break;}
  2182. case 'SUPER_A3' : {$pf = array( 864.567, 1380.472); break;}
  2183. case 'A4_EXTRA' : {$pf = array( 666.142, 912.756); break;}
  2184. case 'A4_SUPER' : {$pf = array( 649.134, 912.756); break;}
  2185. case 'SUPER_A4' : {$pf = array( 643.465, 1009.134); break;}
  2186. case 'A4_LONG' : {$pf = array( 595.276, 986.457); break;}
  2187. case 'F4' : {$pf = array( 595.276, 935.433); break;}
  2188. case 'SO_B5_EXTRA': {$pf = array( 572.598, 782.362); break;}
  2189. case 'A5_EXTRA' : {$pf = array( 490.394, 666.142); break;}
  2190. // ANSI Series
  2191. case 'ANSI_E': {$pf = array( 2448.000, 3168.000); break;}
  2192. case 'ANSI_D': {$pf = array( 1584.000, 2448.000); break;}
  2193. case 'ANSI_C': {$pf = array( 1224.000, 1584.000); break;}
  2194. case 'ANSI_B': {$pf = array( 792.000, 1224.000); break;}
  2195. case 'ANSI_A': {$pf = array( 612.000, 792.000); break;}
  2196. // Traditional 'Loose' North American Paper Sizes
  2197. case 'USLEDGER':
  2198. case 'LEDGER' : {$pf = array( 1224.000, 792.000); break;}
  2199. case 'ORGANIZERK':
  2200. case 'BIBLE':
  2201. case 'USTABLOID':
  2202. case 'TABLOID': {$pf = array( 792.000, 1224.000); break;}
  2203. case 'ORGANIZERM':
  2204. case 'USLETTER':
  2205. case 'LETTER' : {$pf = array( 612.000, 792.000); break;}
  2206. case 'USLEGAL':
  2207. case 'LEGAL' : {$pf = array( 612.000, 1008.000); break;}
  2208. case 'GOVERNMENTLETTER':
  2209. case 'GLETTER': {$pf = array( 576.000, 756.000); break;}
  2210. case 'JUNIORLEGAL':
  2211. case 'JLEGAL' : {$pf = array( 576.000, 360.000); break;}
  2212. // Other North American Paper Sizes
  2213. case 'QUADDEMY': {$pf = array( 2520.000, 3240.000); break;}
  2214. case 'SUPER_B': {$pf = array( 936.000, 1368.000); break;}
  2215. case 'QUARTO': {$pf = array( 648.000, 792.000); break;}
  2216. case 'GOVERNMENTLEGAL':
  2217. case 'FOLIO': {$pf = array( 612.000, 936.000); break;}
  2218. case 'MONARCH':
  2219. case 'EXECUTIVE': {$pf = array( 522.000, 756.000); break;}
  2220. case 'ORGANIZERL':
  2221. case 'STATEMENT':
  2222. case 'MEMO': {$pf = array( 396.000, 612.000); break;}
  2223. case 'FOOLSCAP': {$pf = array( 595.440, 936.000); break;}
  2224. case 'COMPACT': {$pf = array( 306.000, 486.000); break;}
  2225. case 'ORGANIZERJ': {$pf = array( 198.000, 360.000); break;}
  2226. // Canadian standard CAN 2-9.60M
  2227. case 'P1': {$pf = array( 1587.402, 2437.795); break;}
  2228. case 'P2': {$pf = array( 1218.898, 1587.402); break;}
  2229. case 'P3': {$pf = array( 793.701, 1218.898); break;}
  2230. case 'P4': {$pf = array( 609.449, 793.701); break;}
  2231. case 'P5': {$pf = array( 396.850, 609.449); break;}
  2232. case 'P6': {$pf = array( 303.307, 396.850); break;}
  2233. // North American Architectural Sizes
  2234. case 'ARCH_E' : {$pf = array( 2592.000, 3456.000); break;}
  2235. case 'ARCH_E1': {$pf = array( 2160.000, 3024.000); break;}
  2236. case 'ARCH_D' : {$pf = array( 1728.000, 2592.000); break;}
  2237. case 'BROADSHEET':
  2238. case 'ARCH_C' : {$pf = array( 1296.000, 1728.000); break;}
  2239. case 'ARCH_B' : {$pf = array( 864.000, 1296.000); break;}
  2240. case 'ARCH_A' : {$pf = array( 648.000, 864.000); break;}
  2241. // --- North American Envelope Sizes ---
  2242. // - Announcement Envelopes
  2243. case 'ANNENV_A2' : {$pf = array( 314.640, 414.000); break;}
  2244. case 'ANNENV_A6' : {$pf = array( 342.000, 468.000); break;}
  2245. case 'ANNENV_A7' : {$pf = array( 378.000, 522.000); break;}
  2246. case 'ANNENV_A8' : {$pf = array( 396.000, 584.640); break;}
  2247. case 'ANNENV_A10' : {$pf = array( 450.000, 692.640); break;}
  2248. case 'ANNENV_SLIM': {$pf = array( 278.640, 638.640); break;}
  2249. // - Commercial Envelopes
  2250. case 'COMMENV_N6_1/4': {$pf = array( 252.000, 432.000); break;}
  2251. case 'COMMENV_N6_3/4': {$pf = array( 260.640, 468.000); break;}
  2252. case 'COMMENV_N8' : {$pf = array( 278.640, 540.000); break;}
  2253. case 'COMMENV_N9' : {$pf = array( 278.640, 638.640); break;}
  2254. case 'COMMENV_N10' : {$pf = array( 296.640, 684.000); break;}
  2255. case 'COMMENV_N11' : {$pf = array( 324.000, 746.640); break;}
  2256. case 'COMMENV_N12' : {$pf = array( 342.000, 792.000); break;}
  2257. case 'COMMENV_N14' : {$pf = array( 360.000, 828.000); break;}
  2258. // - Catalogue Envelopes
  2259. case 'CATENV_N1' : {$pf = array( 432.000, 648.000); break;}
  2260. case 'CATENV_N1_3/4' : {$pf = array( 468.000, 684.000); break;}
  2261. case 'CATENV_N2' : {$pf = array( 468.000, 720.000); break;}
  2262. case 'CATENV_N3' : {$pf = array( 504.000, 720.000); break;}
  2263. case 'CATENV_N6' : {$pf = array( 540.000, 756.000); break;}
  2264. case 'CATENV_N7' : {$pf = array( 576.000, 792.000); break;}
  2265. case 'CATENV_N8' : {$pf = array( 594.000, 810.000); break;}
  2266. case 'CATENV_N9_1/2' : {$pf = array( 612.000, 756.000); break;}
  2267. case 'CATENV_N9_3/4' : {$pf = array( 630.000, 810.000); break;}
  2268. case 'CATENV_N10_1/2': {$pf = array( 648.000, 864.000); break;}
  2269. case 'CATENV_N12_1/2': {$pf = array( 684.000, 900.000); break;}
  2270. case 'CATENV_N13_1/2': {$pf = array( 720.000, 936.000); break;}
  2271. case 'CATENV_N14_1/4': {$pf = array( 810.000, 882.000); break;}
  2272. case 'CATENV_N14_1/2': {$pf = array( 828.000, 1044.000); break;}
  2273. // Japanese (JIS P 0138-61) Standard B-Series
  2274. case 'JIS_B0' : {$pf = array( 2919.685, 4127.244); break;}
  2275. case 'JIS_B1' : {$pf = array( 2063.622, 2919.685); break;}
  2276. case 'JIS_B2' : {$pf = array( 1459.843, 2063.622); break;}
  2277. case 'JIS_B3' : {$pf = array( 1031.811, 1459.843); break;}
  2278. case 'JIS_B4' : {$pf = array( 728.504, 1031.811); break;}
  2279. case 'JIS_B5' : {$pf = array( 515.906, 728.504); break;}
  2280. case 'JIS_B6' : {$pf = array( 362.835, 515.906); break;}
  2281. case 'JIS_B7' : {$pf = array( 257.953, 362.835); break;}
  2282. case 'JIS_B8' : {$pf = array( 181.417, 257.953); break;}
  2283. case 'JIS_B9' : {$pf = array( 127.559, 181.417); break;}
  2284. case 'JIS_B10': {$pf = array( 90.709, 127.559); break;}
  2285. case 'JIS_B11': {$pf = array( 62.362, 90.709); break;}
  2286. case 'JIS_B12': {$pf = array( 45.354, 62.362); break;}
  2287. // PA Series
  2288. case 'PA0' : {$pf = array( 2381.102, 3174.803,); break;}
  2289. case 'PA1' : {$pf = array( 1587.402, 2381.102); break;}
  2290. case 'PA2' : {$pf = array( 1190.551, 1587.402); break;}
  2291. case 'PA3' : {$pf = array( 793.701, 1190.551); break;}
  2292. case 'PA4' : {$pf = array( 595.276, 793.701); break;}
  2293. case 'PA5' : {$pf = array( 396.850, 595.276); break;}
  2294. case 'PA6' : {$pf = array( 297.638, 396.850); break;}
  2295. case 'PA7' : {$pf = array( 198.425, 297.638); break;}
  2296. case 'PA8' : {$pf = array( 147.402, 198.425); break;}
  2297. case 'PA9' : {$pf = array( 99.213, 147.402); break;}
  2298. case 'PA10': {$pf = array( 73.701, 99.213); break;}
  2299. // Standard Photographic Print Sizes
  2300. case 'PASSPORT_PHOTO': {$pf = array( 99.213, 127.559); break;}
  2301. case 'E' : {$pf = array( 233.858, 340.157); break;}
  2302. case 'L':
  2303. case '3R' : {$pf = array( 252.283, 360.000); break;}
  2304. case 'KG':
  2305. case '4R' : {$pf = array( 289.134, 430.866); break;}
  2306. case '4D' : {$pf = array( 340.157, 430.866); break;}
  2307. case '2L':
  2308. case '5R' : {$pf = array( 360.000, 504.567); break;}
  2309. case '8P':
  2310. case '6R' : {$pf = array( 430.866, 575.433); break;}
  2311. case '6P':
  2312. case '8R' : {$pf = array( 575.433, 720.000); break;}
  2313. case '6PW':
  2314. case 'S8R' : {$pf = array( 575.433, 864.567); break;}
  2315. case '4P':
  2316. case '10R' : {$pf = array( 720.000, 864.567); break;}
  2317. case '4PW':
  2318. case 'S10R': {$pf = array( 720.000, 1080.000); break;}
  2319. case '11R' : {$pf = array( 790.866, 1009.134); break;}
  2320. case 'S11R': {$pf = array( 790.866, 1224.567); break;}
  2321. case '12R' : {$pf = array( 864.567, 1080.000); break;}
  2322. case 'S12R': {$pf = array( 864.567, 1292.598); break;}
  2323. // Common Newspaper Sizes
  2324. case 'NEWSPAPER_BROADSHEET': {$pf = array( 2125.984, 1700.787); break;}
  2325. case 'NEWSPAPER_BERLINER' : {$pf = array( 1332.283, 892.913); break;}
  2326. case 'NEWSPAPER_TABLOID':
  2327. case 'NEWSPAPER_COMPACT' : {$pf = array( 1218.898, 793.701); break;}
  2328. // Business Cards
  2329. case 'CREDIT_CARD':
  2330. case 'BUSINESS_CARD':
  2331. case 'BUSINESS_CARD_ISO7810': {$pf = array( 153.014, 242.646); break;}
  2332. case 'BUSINESS_CARD_ISO216' : {$pf = array( 147.402, 209.764); break;}
  2333. case 'BUSINESS_CARD_IT':
  2334. case 'BUSINESS_CARD_UK':
  2335. case 'BUSINESS_CARD_FR':
  2336. case 'BUSINESS_CARD_DE':
  2337. case 'BUSINESS_CARD_ES' : {$pf = array( 155.906, 240.945); break;}
  2338. case 'BUSINESS_CARD_CA':
  2339. case 'BUSINESS_CARD_US' : {$pf = array( 144.567, 252.283); break;}
  2340. case 'BUSINESS_CARD_JP' : {$pf = array( 155.906, 257.953); break;}
  2341. case 'BUSINESS_CARD_HK' : {$pf = array( 153.071, 255.118); break;}
  2342. case 'BUSINESS_CARD_AU':
  2343. case 'BUSINESS_CARD_DK':
  2344. case 'BUSINESS_CARD_SE' : {$pf = array( 155.906, 255.118); break;}
  2345. case 'BUSINESS_CARD_RU':
  2346. case 'BUSINESS_CARD_CZ':
  2347. case 'BUSINESS_CARD_FI':
  2348. case 'BUSINESS_CARD_HU':
  2349. case 'BUSINESS_CARD_IL' : {$pf = array( 141.732, 255.118); break;}
  2350. // Billboards
  2351. case '4SHEET' : {$pf = array( 2880.000, 4320.000); break;}
  2352. case '6SHEET' : {$pf = array( 3401.575, 5102.362); break;}
  2353. case '12SHEET': {$pf = array( 8640.000, 4320.000); break;}
  2354. case '16SHEET': {$pf = array( 5760.000, 8640.000); break;}
  2355. case '32SHEET': {$pf = array(11520.000, 8640.000); break;}
  2356. case '48SHEET': {$pf = array(17280.000, 8640.000); break;}
  2357. case '64SHEET': {$pf = array(23040.000, 8640.000); break;}
  2358. case '96SHEET': {$pf = array(34560.000, 8640.000); break;}
  2359. // Old European Sizes
  2360. // - Old Imperial English Sizes
  2361. case 'EN_EMPEROR' : {$pf = array( 3456.000, 5184.000); break;}
  2362. case 'EN_ANTIQUARIAN' : {$pf = array( 2232.000, 3816.000); break;}
  2363. case 'EN_GRAND_EAGLE' : {$pf = array( 2070.000, 3024.000); break;}
  2364. case 'EN_DOUBLE_ELEPHANT' : {$pf = array( 1926.000, 2880.000); break;}
  2365. case 'EN_ATLAS' : {$pf = array( 1872.000, 2448.000); break;}
  2366. case 'EN_COLOMBIER' : {$pf = array( 1692.000, 2484.000); break;}
  2367. case 'EN_ELEPHANT' : {$pf = array( 1656.000, 2016.000); break;}
  2368. case 'EN_DOUBLE_DEMY' : {$pf = array( 1620.000, 2556.000); break;}
  2369. case 'EN_IMPERIAL' : {$pf = array( 1584.000, 2160.000); break;}
  2370. case 'EN_PRINCESS' : {$pf = array( 1548.000, 2016.000); break;}
  2371. case 'EN_CARTRIDGE' : {$pf = array( 1512.000, 1872.000); break;}
  2372. case 'EN_DOUBLE_LARGE_POST': {$pf = array( 1512.000, 2376.000); break;}
  2373. case 'EN_ROYAL' : {$pf = array( 1440.000, 1800.000); break;}
  2374. case 'EN_SHEET':
  2375. case 'EN_HALF_POST' : {$pf = array( 1404.000, 1692.000); break;}
  2376. case 'EN_SUPER_ROYAL' : {$pf = array( 1368.000, 1944.000); break;}
  2377. case 'EN_DOUBLE_POST' : {$pf = array( 1368.000, 2196.000); break;}
  2378. case 'EN_MEDIUM' : {$pf = array( 1260.000, 1656.000); break;}
  2379. case 'EN_DEMY' : {$pf = array( 1260.000, 1620.000); break;}
  2380. case 'EN_LARGE_POST' : {$pf = array( 1188.000, 1512.000); break;}
  2381. case 'EN_COPY_DRAUGHT' : {$pf = array( 1152.000, 1440.000); break;}
  2382. case 'EN_POST' : {$pf = array( 1116.000, 1386.000); break;}
  2383. case 'EN_CROWN' : {$pf = array( 1080.000, 1440.000); break;}
  2384. case 'EN_PINCHED_POST' : {$pf = array( 1062.000, 1332.000); break;}
  2385. case 'EN_BRIEF' : {$pf = array( 972.000, 1152.000); break;}
  2386. case 'EN_FOOLSCAP' : {$pf = array( 972.000, 1224.000); break;}
  2387. case 'EN_SMALL_FOOLSCAP' : {$pf = array( 954.000, 1188.000); break;}
  2388. case 'EN_POTT' : {$pf = array( 900.000, 1080.000); break;}
  2389. // - Old Imperial Belgian Sizes
  2390. case 'BE_GRAND_AIGLE' : {$pf = array( 1984.252, 2948.031); break;}
  2391. case 'BE_COLOMBIER' : {$pf = array( 1757.480, 2409.449); break;}
  2392. case 'BE_DOUBLE_CARRE': {$pf = array( 1757.480, 2607.874); break;}
  2393. case 'BE_ELEPHANT' : {$pf = array( 1746.142, 2182.677); break;}
  2394. case 'BE_PETIT_AIGLE' : {$pf = array( 1700.787, 2381.102); break;}
  2395. case 'BE_GRAND_JESUS' : {$pf = array( 1559.055, 2069.291); break;}
  2396. case 'BE_JESUS' : {$pf = array( 1530.709, 2069.291); break;}
  2397. case 'BE_RAISIN' : {$pf = array( 1417.323, 1842.520); break;}
  2398. case 'BE_GRAND_MEDIAN': {$pf = array( 1303.937, 1714.961); break;}
  2399. case 'BE_DOUBLE_POSTE': {$pf = array( 1233.071, 1601.575); break;}
  2400. case 'BE_COQUILLE' : {$pf = array( 1218.898, 1587.402); break;}
  2401. case 'BE_PETIT_MEDIAN': {$pf = array( 1176.378, 1502.362); break;}
  2402. case 'BE_RUCHE' : {$pf = array( 1020.472, 1303.937); break;}
  2403. case 'BE_PROPATRIA' : {$pf = array( 977.953, 1218.898); break;}
  2404. case 'BE_LYS' : {$pf = array( 898.583, 1125.354); break;}
  2405. case 'BE_POT' : {$pf = array( 870.236, 1088.504); break;}
  2406. case 'BE_ROSETTE' : {$pf = array( 765.354, 983.622); break;}
  2407. // - Old Imperial French Sizes
  2408. case 'FR_UNIVERS' : {$pf = array( 2834.646, 3685.039); break;}
  2409. case 'FR_DOUBLE_COLOMBIER' : {$pf = array( 2551.181, 3571.654); break;}
  2410. case 'FR_GRANDE_MONDE' : {$pf = array( 2551.181, 3571.654); break;}
  2411. case 'FR_DOUBLE_SOLEIL' : {$pf = array( 2267.717, 3401.575); break;}
  2412. case 'FR_DOUBLE_JESUS' : {$pf = array( 2154.331, 3174.803); break;}
  2413. case 'FR_GRAND_AIGLE' : {$pf = array( 2125.984, 3004.724); break;}
  2414. case 'FR_PETIT_AIGLE' : {$pf = array( 1984.252, 2664.567); break;}
  2415. case 'FR_DOUBLE_RAISIN' : {$pf = array( 1842.520, 2834.646); break;}
  2416. case 'FR_JOURNAL' : {$pf = array( 1842.520, 2664.567); break;}
  2417. case 'FR_COLOMBIER_AFFICHE': {$pf = array( 1785.827, 2551.181); break;}
  2418. case 'FR_DOUBLE_CAVALIER' : {$pf = array( 1757.480, 2607.874); break;}
  2419. case 'FR_CLOCHE' : {$pf = array( 1700.787, 2267.717); break;}
  2420. case 'FR_SOLEIL' : {$pf = array( 1700.787, 2267.717); break;}
  2421. case 'FR_DOUBLE_CARRE' : {$pf = array( 1587.402, 2551.181); break;}
  2422. case 'FR_DOUBLE_COQUILLE' : {$pf = array( 1587.402, 2494.488); break;}
  2423. case 'FR_JESUS' : {$pf = array( 1587.402, 2154.331); break;}
  2424. case 'FR_RAISIN' : {$pf = array( 1417.323, 1842.520); break;}
  2425. case 'FR_CAVALIER' : {$pf = array( 1303.937, 1757.480); break;}
  2426. case 'FR_DOUBLE_COURONNE' : {$pf = array( 1303.937, 2040.945); break;}
  2427. case 'FR_CARRE' : {$pf = array( 1275.591, 1587.402); break;}
  2428. case 'FR_COQUILLE' : {$pf = array( 1247.244, 1587.402); break;}
  2429. case 'FR_DOUBLE_TELLIERE' : {$pf = array( 1247.244, 1927.559); break;}
  2430. case 'FR_DOUBLE_CLOCHE' : {$pf = array( 1133.858, 1700.787); break;}
  2431. case 'FR_DOUBLE_POT' : {$pf = array( 1133.858, 1757.480); break;}
  2432. case 'FR_ECU' : {$pf = array( 1133.858, 1474.016); break;}
  2433. case 'FR_COURONNE' : {$pf = array( 1020.472, 1303.937); break;}
  2434. case 'FR_TELLIERE' : {$pf = array( 963.780, 1247.244); break;}
  2435. case 'FR_POT' : {$pf = array( 878.740, 1133.858); break;}
  2436. // DEFAULT ISO A4
  2437. default: {$pf = array( 595.276, 841.890); break;}
  2438. }
  2439. return $pf;
  2440. }
  2441. /**
  2442. * Change the format of the current page
  2443. * @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>
  2444. * <li>['format'] = page format name (one of the above);</li>
  2445. * <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>
  2446. * <li>['PZ'] : The page's preferred zoom (magnification) factor.</li>
  2447. * <li>['MediaBox'] : the boundaries of the physical medium on which the page shall be displayed or printed:</li>
  2448. * <li>['MediaBox']['llx'] : lower-left x coordinate in points</li>
  2449. * <li>['MediaBox']['lly'] : lower-left y coordinate in points</li>
  2450. * <li>['MediaBox']['urx'] : upper-right x coordinate in points</li>
  2451. * <li>['MediaBox']['ury'] : upper-right y coordinate in points</li>
  2452. * <li>['CropBox'] : the visible region of default user space:</li>
  2453. * <li>['CropBox']['llx'] : lower-left x coordinate in points</li>
  2454. * <li>['CropBox']['lly'] : lower-left y coordinate in points</li>
  2455. * <li>['CropBox']['urx'] : upper-right x coordinate in points</li>
  2456. * <li>['CropBox']['ury'] : upper-right y coordinate in points</li>
  2457. * <li>['BleedBox'] : the region to which the contents of the page shall be clipped when output in a production environment:</li>
  2458. * <li>['BleedBox']['llx'] : lower-left x coordinate in points</li>
  2459. * <li>['BleedBox']['lly'] : lower-left y coordinate in points</li>
  2460. * <li>['BleedBox']['urx'] : upper-right x coordinate in points</li>
  2461. * <li>['BleedBox']['ury'] : upper-right y coordinate in points</li>
  2462. * <li>['TrimBox'] : the intended dimensions of the finished page after trimming:</li>
  2463. * <li>['TrimBox']['llx'] : lower-left x coordinate in points</li>
  2464. * <li>['TrimBox']['lly'] : lower-left y coordinate in points</li>
  2465. * <li>['TrimBox']['urx'] : upper-right x coordinate in points</li>
  2466. * <li>['TrimBox']['ury'] : upper-right y coordinate in points</li>
  2467. * <li>['ArtBox'] : the extent of the page's meaningful content:</li>
  2468. * <li>['ArtBox']['llx'] : lower-left x coordinate in points</li>
  2469. * <li>['ArtBox']['lly'] : lower-left y coordinate in points</li>
  2470. * <li>['ArtBox']['urx'] : upper-right x coordinate in points</li>
  2471. * <li>['ArtBox']['ury'] : upper-right y coordinate in points</li>
  2472. * <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>
  2473. * <li>['BoxColorInfo'][BOXTYPE]['C'] : an array of three numbers in the range 0-255, representing the components in the DeviceRGB colour space.</li>
  2474. * <li>['BoxColorInfo'][BOXTYPE]['W'] : the guideline width in default user units</li>
  2475. * <li>['BoxColorInfo'][BOXTYPE]['S'] : the guideline style: S = Solid; D = Dashed</li>
  2476. * <li>['BoxColorInfo'][BOXTYPE]['D'] : dash array defining a pattern of dashes and gaps to be used in drawing dashed guidelines</li>
  2477. * <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>
  2478. * <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>
  2479. * <li>['trans']['S'] : transition style : Split, Blinds, Box, Wipe, Dissolve, Glitter, R, Fly, Push, Cover, Uncover, Fade</li>
  2480. * <li>['trans']['D'] : The duration of the transition effect, in seconds.</li>
  2481. * <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>
  2482. * <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>
  2483. * <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>
  2484. * <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>
  2485. * <li>['trans']['B'] : (Fly transition style only) If true, the area that shall be flown in is rectangular and opaque. Default: false.</li>
  2486. * </ul>
  2487. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul>
  2488. * <li>P or Portrait (default)</li>
  2489. * <li>L or Landscape</li>
  2490. * <li>'' (empty string) for automatic orientation</li>
  2491. * </ul>
  2492. * @protected
  2493. * @since 3.0.015 (2008-06-06)
  2494. * @see getPageSizeFromFormat()
  2495. */
  2496. protected function setPageFormat($format, $orientation='P') {
  2497. if (!empty($format) AND isset($this->pagedim[$this->page])) {
  2498. // remove inherited values
  2499. unset($this->pagedim[$this->page]);
  2500. }
  2501. if (is_string($format)) {
  2502. // get page measures from format name
  2503. $pf = $this->getPageSizeFromFormat($format);
  2504. $this->fwPt = $pf[0];
  2505. $this->fhPt = $pf[1];
  2506. } else {
  2507. // the boundaries of the physical medium on which the page shall be displayed or printed
  2508. if (isset($format['MediaBox'])) {
  2509. $this->setPageBoxes($this->page, 'MediaBox', $format['MediaBox']['llx'], $format['MediaBox']['lly'], $format['MediaBox']['urx'], $format['MediaBox']['ury'], false);
  2510. $this->fwPt = (($format['MediaBox']['urx'] - $format['MediaBox']['llx']) * $this->k);
  2511. $this->fhPt = (($format['MediaBox']['ury'] - $format['MediaBox']['lly']) * $this->k);
  2512. } else {
  2513. if (isset($format[0]) AND is_numeric($format[0]) AND isset($format[1]) AND is_numeric($format[1])) {
  2514. $pf = array(($format[0] * $this->k), ($format[1] * $this->k));
  2515. } else {
  2516. if (!isset($format['format'])) {
  2517. // default value
  2518. $format['format'] = 'A4';
  2519. }
  2520. $pf = $this->getPageSizeFromFormat($format['format']);
  2521. }
  2522. $this->fwPt = $pf[0];
  2523. $this->fhPt = $pf[1];
  2524. $this->setPageBoxes($this->page, 'MediaBox', 0, 0, $this->fwPt, $this->fhPt, true);
  2525. }
  2526. // the visible region of default user space
  2527. if (isset($format['CropBox'])) {
  2528. $this->setPageBoxes($this->page, 'CropBox', $format['CropBox']['llx'], $format['CropBox']['lly'], $format['CropBox']['urx'], $format['CropBox']['ury'], false);
  2529. }
  2530. // the region to which the contents of the page shall be clipped when output in a production environment
  2531. if (isset($format['BleedBox'])) {
  2532. $this->setPageBoxes($this->page, 'BleedBox', $format['BleedBox']['llx'], $format['BleedBox']['lly'], $format['BleedBox']['urx'], $format['BleedBox']['ury'], false);
  2533. }
  2534. // the intended dimensions of the finished page after trimming
  2535. if (isset($format['TrimBox'])) {
  2536. $this->setPageBoxes($this->page, 'TrimBox', $format['TrimBox']['llx'], $format['TrimBox']['lly'], $format['TrimBox']['urx'], $format['TrimBox']['ury'], false);
  2537. }
  2538. // the page's meaningful content (including potential white space)
  2539. if (isset($format['ArtBox'])) {
  2540. $this->setPageBoxes($this->page, 'ArtBox', $format['ArtBox']['llx'], $format['ArtBox']['lly'], $format['ArtBox']['urx'], $format['ArtBox']['ury'], false);
  2541. }
  2542. // specify the colours and other visual characteristics that should be used in displaying guidelines on the screen for the various page boundaries
  2543. if (isset($format['BoxColorInfo'])) {
  2544. $this->pagedim[$this->page]['BoxColorInfo'] = $format['BoxColorInfo'];
  2545. }
  2546. if (isset($format['Rotate']) AND (($format['Rotate'] % 90) == 0)) {
  2547. // The number of degrees by which the page shall be rotated clockwise when displayed or printed. The value shall be a multiple of 90.
  2548. $this->pagedim[$this->page]['Rotate'] = intval($format['Rotate']);
  2549. }
  2550. if (isset($format['PZ'])) {
  2551. // The page's preferred zoom (magnification) factor
  2552. $this->pagedim[$this->page]['PZ'] = floatval($format['PZ']);
  2553. }
  2554. if (isset($format['trans'])) {
  2555. // The style and duration of the visual transition to use when moving from another page to the given page during a presentation
  2556. if (isset($format['trans']['Dur'])) {
  2557. // The page's display duration
  2558. $this->pagedim[$this->page]['trans']['Dur'] = floatval($format['trans']['Dur']);
  2559. }
  2560. $stansition_styles = array('Split', 'Blinds', 'Box', 'Wipe', 'Dissolve', 'Glitter', 'R', 'Fly', 'Push', 'Cover', 'Uncover', 'Fade');
  2561. if (isset($format['trans']['S']) AND in_array($format['trans']['S'], $stansition_styles)) {
  2562. // The transition style that shall be used when moving to this page from another during a presentation
  2563. $this->pagedim[$this->page]['trans']['S'] = $format['trans']['S'];
  2564. $valid_effect = array('Split', 'Blinds');
  2565. $valid_vals = array('H', 'V');
  2566. if (isset($format['trans']['Dm']) AND in_array($format['trans']['S'], $valid_effect) AND in_array($format['trans']['Dm'], $valid_vals)) {
  2567. $this->pagedim[$this->page]['trans']['Dm'] = $format['trans']['Dm'];
  2568. }
  2569. $valid_effect = array('Split', 'Box', 'Fly');
  2570. $valid_vals = array('I', 'O');
  2571. if (isset($format['trans']['M']) AND in_array($format['trans']['S'], $valid_effect) AND in_array($format['trans']['M'], $valid_vals)) {
  2572. $this->pagedim[$this->page]['trans']['M'] = $format['trans']['M'];
  2573. }
  2574. $valid_effect = array('Wipe', 'Glitter', 'Fly', 'Cover', 'Uncover', 'Push');
  2575. if (isset($format['trans']['Di']) AND in_array($format['trans']['S'], $valid_effect)) {
  2576. if (((($format['trans']['Di'] == 90) OR ($format['trans']['Di'] == 180)) AND ($format['trans']['S'] == 'Wipe'))
  2577. OR (($format['trans']['Di'] == 315) AND ($format['trans']['S'] == 'Glitter'))
  2578. OR (($format['trans']['Di'] == 0) OR ($format['trans']['Di'] == 270))) {
  2579. $this->pagedim[$this->page]['trans']['Di'] = intval($format['trans']['Di']);
  2580. }
  2581. }
  2582. if (isset($format['trans']['SS']) AND ($format['trans']['S'] == 'Fly')) {
  2583. $this->pagedim[$this->page]['trans']['SS'] = floatval($format['trans']['SS']);
  2584. }
  2585. if (isset($format['trans']['B']) AND ($format['trans']['B'] === true) AND ($format['trans']['S'] == 'Fly')) {
  2586. $this->pagedim[$this->page]['trans']['B'] = 'true';
  2587. }
  2588. } else {
  2589. $this->pagedim[$this->page]['trans']['S'] = 'R';
  2590. }
  2591. if (isset($format['trans']['D'])) {
  2592. // The duration of the transition effect, in seconds
  2593. $this->pagedim[$this->page]['trans']['D'] = floatval($format['trans']['D']);
  2594. } else {
  2595. $this->pagedim[$this->page]['trans']['D'] = 1;
  2596. }
  2597. }
  2598. }
  2599. $this->setPageOrientation($orientation);
  2600. }
  2601. /**
  2602. * Set page boundaries.
  2603. * @param $page (int) page number
  2604. * @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>
  2605. * @param $llx (float) lower-left x coordinate in user units
  2606. * @param $lly (float) lower-left y coordinate in user units
  2607. * @param $urx (float) upper-right x coordinate in user units
  2608. * @param $ury (float) upper-right y coordinate in user units
  2609. * @param $points (boolean) if true uses user units as unit of measure, otherwise uses PDF points
  2610. * @public
  2611. * @since 5.0.010 (2010-05-17)
  2612. */
  2613. public function setPageBoxes($page, $type, $llx, $lly, $urx, $ury, $points=false) {
  2614. if (!isset($this->pagedim[$page])) {
  2615. // initialize array
  2616. $this->pagedim[$page] = array();
  2617. }
  2618. $pageboxes = array('MediaBox', 'CropBox', 'BleedBox', 'TrimBox', 'ArtBox');
  2619. if (!in_array($type, $pageboxes)) {
  2620. return;
  2621. }
  2622. if ($points) {
  2623. $k = 1;
  2624. } else {
  2625. $k = $this->k;
  2626. }
  2627. $this->pagedim[$page][$type]['llx'] = ($llx * $k);
  2628. $this->pagedim[$page][$type]['lly'] = ($lly * $k);
  2629. $this->pagedim[$page][$type]['urx'] = ($urx * $k);
  2630. $this->pagedim[$page][$type]['ury'] = ($ury * $k);
  2631. }
  2632. /**
  2633. * Swap X and Y coordinates of page boxes (change page boxes orientation).
  2634. * @param $page (int) page number
  2635. * @protected
  2636. * @since 5.0.010 (2010-05-17)
  2637. */
  2638. protected function swapPageBoxCoordinates($page) {
  2639. $pageboxes = array('MediaBox', 'CropBox', 'BleedBox', 'TrimBox', 'ArtBox');
  2640. foreach ($pageboxes as $type) {
  2641. // swap X and Y coordinates
  2642. if (isset($this->pagedim[$page][$type])) {
  2643. $tmp = $this->pagedim[$page][$type]['llx'];
  2644. $this->pagedim[$page][$type]['llx'] = $this->pagedim[$page][$type]['lly'];
  2645. $this->pagedim[$page][$type]['lly'] = $tmp;
  2646. $tmp = $this->pagedim[$page][$type]['urx'];
  2647. $this->pagedim[$page][$type]['urx'] = $this->pagedim[$page][$type]['ury'];
  2648. $this->pagedim[$page][$type]['ury'] = $tmp;
  2649. }
  2650. }
  2651. }
  2652. /**
  2653. * Set page orientation.
  2654. * @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>
  2655. * @param $autopagebreak (boolean) Boolean indicating if auto-page-break mode should be on or off.
  2656. * @param $bottommargin (float) bottom margin of the page.
  2657. * @public
  2658. * @since 3.0.015 (2008-06-06)
  2659. */
  2660. public function setPageOrientation($orientation, $autopagebreak='', $bottommargin='') {
  2661. if (!isset($this->pagedim[$this->page]['MediaBox'])) {
  2662. // the boundaries of the physical medium on which the page shall be displayed or printed
  2663. $this->setPageBoxes($this->page, 'MediaBox', 0, 0, $this->fwPt, $this->fhPt, true);
  2664. }
  2665. if (!isset($this->pagedim[$this->page]['CropBox'])) {
  2666. // the visible region of default user space
  2667. $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);
  2668. }
  2669. if (!isset($this->pagedim[$this->page]['BleedBox'])) {
  2670. // the region to which the contents of the page shall be clipped when output in a production environment
  2671. $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);
  2672. }
  2673. if (!isset($this->pagedim[$this->page]['TrimBox'])) {
  2674. // the intended dimensions of the finished page after trimming
  2675. $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);
  2676. }
  2677. if (!isset($this->pagedim[$this->page]['ArtBox'])) {
  2678. // the page's meaningful content (including potential white space)
  2679. $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);
  2680. }
  2681. if (!isset($this->pagedim[$this->page]['Rotate'])) {
  2682. // The number of degrees by which the page shall be rotated clockwise when displayed or printed. The value shall be a multiple of 90.
  2683. $this->pagedim[$this->page]['Rotate'] = 0;
  2684. }
  2685. if (!isset($this->pagedim[$this->page]['PZ'])) {
  2686. // The page's preferred zoom (magnification) factor
  2687. $this->pagedim[$this->page]['PZ'] = 1;
  2688. }
  2689. if ($this->fwPt > $this->fhPt) {
  2690. // landscape
  2691. $default_orientation = 'L';
  2692. } else {
  2693. // portrait
  2694. $default_orientation = 'P';
  2695. }
  2696. $valid_orientations = array('P', 'L');
  2697. if (empty($orientation)) {
  2698. $orientation = $default_orientation;
  2699. } else {
  2700. $orientation = strtoupper($orientation{0});
  2701. }
  2702. if (in_array($orientation, $valid_orientations) AND ($orientation != $default_orientation)) {
  2703. $this->CurOrientation = $orientation;
  2704. $this->wPt = $this->fhPt;
  2705. $this->hPt = $this->fwPt;
  2706. } else {
  2707. $this->CurOrientation = $default_orientation;
  2708. $this->wPt = $this->fwPt;
  2709. $this->hPt = $this->fhPt;
  2710. }
  2711. if ((abs($this->pagedim[$this->page]['MediaBox']['urx'] - $this->hPt) < $this->feps) AND (abs($this->pagedim[$this->page]['MediaBox']['ury'] - $this->wPt) < $this->feps)){
  2712. // swap X and Y coordinates (change page orientation)
  2713. $this->swapPageBoxCoordinates($this->page);
  2714. }
  2715. $this->w = $this->wPt / $this->k;
  2716. $this->h = $this->hPt / $this->k;
  2717. if ($this->empty_string($autopagebreak)) {
  2718. if (isset($this->AutoPageBreak)) {
  2719. $autopagebreak = $this->AutoPageBreak;
  2720. } else {
  2721. $autopagebreak = true;
  2722. }
  2723. }
  2724. if ($this->empty_string($bottommargin)) {
  2725. if (isset($this->bMargin)) {
  2726. $bottommargin = $this->bMargin;
  2727. } else {
  2728. // default value = 2 cm
  2729. $bottommargin = 2 * 28.35 / $this->k;
  2730. }
  2731. }
  2732. $this->SetAutoPageBreak($autopagebreak, $bottommargin);
  2733. // store page dimensions
  2734. $this->pagedim[$this->page]['w'] = $this->wPt;
  2735. $this->pagedim[$this->page]['h'] = $this->hPt;
  2736. $this->pagedim[$this->page]['wk'] = $this->w;
  2737. $this->pagedim[$this->page]['hk'] = $this->h;
  2738. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  2739. $this->pagedim[$this->page]['bm'] = $bottommargin;
  2740. $this->pagedim[$this->page]['lm'] = $this->lMargin;
  2741. $this->pagedim[$this->page]['rm'] = $this->rMargin;
  2742. $this->pagedim[$this->page]['pb'] = $autopagebreak;
  2743. $this->pagedim[$this->page]['or'] = $this->CurOrientation;
  2744. $this->pagedim[$this->page]['olm'] = $this->original_lMargin;
  2745. $this->pagedim[$this->page]['orm'] = $this->original_rMargin;
  2746. }
  2747. /**
  2748. * Set regular expression to detect withespaces or word separators.
  2749. * The pattern delimiter must be the forward-slash character "/".
  2750. * Some example patterns are:
  2751. * <pre>
  2752. * Non-Unicode or missing PCRE unicode support: "/[^\S\xa0]/"
  2753. * Unicode and PCRE unicode support: "/[^\S\P{Z}\xa0]/u"
  2754. * Unicode and PCRE unicode support in Chinese mode: "/[^\S\P{Z}\P{Lo}\xa0]/u"
  2755. * if PCRE unicode support is turned ON ("\P" is the negate class of "\p"):
  2756. * "\p{Z}" or "\p{Separator}": any kind of Unicode whitespace or invisible separator.
  2757. * "\p{Lo}" or "\p{Other_Letter}": a Unicode letter or ideograph that does not have lowercase and uppercase variants.
  2758. * "\p{Lo}" is needed for Chinese characters because are packed next to each other without spaces in between.
  2759. * </pre>
  2760. * @param $re (string) regular expression (leave empty for default).
  2761. * @public
  2762. * @since 4.6.016 (2009-06-15)
  2763. */
  2764. public function setSpacesRE($re='/[^\S\xa0]/') {
  2765. $this->re_spaces = $re;
  2766. $re_parts = explode('/', $re);
  2767. // get pattern parts
  2768. $this->re_space = array();
  2769. if (isset($re_parts[1]) AND !empty($re_parts[1])) {
  2770. $this->re_space['p'] = $re_parts[1];
  2771. } else {
  2772. $this->re_space['p'] = '[\s]';
  2773. }
  2774. // set pattern modifiers
  2775. if (isset($re_parts[2]) AND !empty($re_parts[2])) {
  2776. $this->re_space['m'] = $re_parts[2];
  2777. } else {
  2778. $this->re_space['m'] = '';
  2779. }
  2780. }
  2781. /**
  2782. * Enable or disable Right-To-Left language mode
  2783. * @param $enable (Boolean) if true enable Right-To-Left language mode.
  2784. * @param $resetx (Boolean) if true reset the X position on direction change.
  2785. * @public
  2786. * @since 2.0.000 (2008-01-03)
  2787. */
  2788. public function setRTL($enable, $resetx=true) {
  2789. $enable = $enable ? true : false;
  2790. $resetx = ($resetx AND ($enable != $this->rtl));
  2791. $this->rtl = $enable;
  2792. $this->tmprtl = false;
  2793. if ($resetx) {
  2794. $this->Ln(0);
  2795. }
  2796. }
  2797. /**
  2798. * Return the RTL status
  2799. * @return boolean
  2800. * @public
  2801. * @since 4.0.012 (2008-07-24)
  2802. */
  2803. public function getRTL() {
  2804. return $this->rtl;
  2805. }
  2806. /**
  2807. * Force temporary RTL language direction
  2808. * @param $mode (mixed) can be false, 'L' for LTR or 'R' for RTL
  2809. * @public
  2810. * @since 2.1.000 (2008-01-09)
  2811. */
  2812. public function setTempRTL($mode) {
  2813. $newmode = false;
  2814. switch (strtoupper($mode)) {
  2815. case 'LTR':
  2816. case 'L': {
  2817. if ($this->rtl) {
  2818. $newmode = 'L';
  2819. }
  2820. break;
  2821. }
  2822. case 'RTL':
  2823. case 'R': {
  2824. if (!$this->rtl) {
  2825. $newmode = 'R';
  2826. }
  2827. break;
  2828. }
  2829. case false:
  2830. default: {
  2831. $newmode = false;
  2832. break;
  2833. }
  2834. }
  2835. $this->tmprtl = $newmode;
  2836. }
  2837. /**
  2838. * Return the current temporary RTL status
  2839. * @return boolean
  2840. * @public
  2841. * @since 4.8.014 (2009-11-04)
  2842. */
  2843. public function isRTLTextDir() {
  2844. return ($this->rtl OR ($this->tmprtl == 'R'));
  2845. }
  2846. /**
  2847. * Set the last cell height.
  2848. * @param $h (float) cell height.
  2849. * @author Nicola Asuni
  2850. * @public
  2851. * @since 1.53.0.TC034
  2852. */
  2853. public function setLastH($h) {
  2854. $this->lasth = $h;
  2855. }
  2856. /**
  2857. * Reset the last cell height.
  2858. * @public
  2859. * @since 5.9.000 (2010-10-03)
  2860. */
  2861. public function resetLastH() {
  2862. $this->lasth = ($this->FontSize * $this->cell_height_ratio) + $this->cell_padding['T'] + $this->cell_padding['B'];
  2863. }
  2864. /**
  2865. * Get the last cell height.
  2866. * @return last cell height
  2867. * @public
  2868. * @since 4.0.017 (2008-08-05)
  2869. */
  2870. public function getLastH() {
  2871. return $this->lasth;
  2872. }
  2873. /**
  2874. * Set the adjusting factor to convert pixels to user units.
  2875. * @param $scale (float) adjusting factor to convert pixels to user units.
  2876. * @author Nicola Asuni
  2877. * @public
  2878. * @since 1.5.2
  2879. */
  2880. public function setImageScale($scale) {
  2881. $this->imgscale = $scale;
  2882. }
  2883. /**
  2884. * Returns the adjusting factor to convert pixels to user units.
  2885. * @return float adjusting factor to convert pixels to user units.
  2886. * @author Nicola Asuni
  2887. * @public
  2888. * @since 1.5.2
  2889. */
  2890. public function getImageScale() {
  2891. return $this->imgscale;
  2892. }
  2893. /**
  2894. * Returns an array of page dimensions:
  2895. * <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>
  2896. * @param $pagenum (int) page number (empty = current page)
  2897. * @return array of page dimensions.
  2898. * @author Nicola Asuni
  2899. * @public
  2900. * @since 4.5.027 (2009-03-16)
  2901. */
  2902. public function getPageDimensions($pagenum='') {
  2903. if (empty($pagenum)) {
  2904. $pagenum = $this->page;
  2905. }
  2906. return $this->pagedim[$pagenum];
  2907. }
  2908. /**
  2909. * Returns the page width in units.
  2910. * @param $pagenum (int) page number (empty = current page)
  2911. * @return int page width.
  2912. * @author Nicola Asuni
  2913. * @public
  2914. * @since 1.5.2
  2915. * @see getPageDimensions()
  2916. */
  2917. public function getPageWidth($pagenum='') {
  2918. if (empty($pagenum)) {
  2919. return $this->w;
  2920. }
  2921. return $this->pagedim[$pagenum]['w'];
  2922. }
  2923. /**
  2924. * Returns the page height in units.
  2925. * @param $pagenum (int) page number (empty = current page)
  2926. * @return int page height.
  2927. * @author Nicola Asuni
  2928. * @public
  2929. * @since 1.5.2
  2930. * @see getPageDimensions()
  2931. */
  2932. public function getPageHeight($pagenum='') {
  2933. if (empty($pagenum)) {
  2934. return $this->h;
  2935. }
  2936. return $this->pagedim[$pagenum]['h'];
  2937. }
  2938. /**
  2939. * Returns the page break margin.
  2940. * @param $pagenum (int) page number (empty = current page)
  2941. * @return int page break margin.
  2942. * @author Nicola Asuni
  2943. * @public
  2944. * @since 1.5.2
  2945. * @see getPageDimensions()
  2946. */
  2947. public function getBreakMargin($pagenum='') {
  2948. if (empty($pagenum)) {
  2949. return $this->bMargin;
  2950. }
  2951. return $this->pagedim[$pagenum]['bm'];
  2952. }
  2953. /**
  2954. * Returns the scale factor (number of points in user unit).
  2955. * @return int scale factor.
  2956. * @author Nicola Asuni
  2957. * @public
  2958. * @since 1.5.2
  2959. */
  2960. public function getScaleFactor() {
  2961. return $this->k;
  2962. }
  2963. /**
  2964. * Defines the left, top and right margins.
  2965. * @param $left (float) Left margin.
  2966. * @param $top (float) Top margin.
  2967. * @param $right (float) Right margin. Default value is the left one.
  2968. * @param $keepmargins (boolean) if true overwrites the default page margins
  2969. * @public
  2970. * @since 1.0
  2971. * @see SetLeftMargin(), SetTopMargin(), SetRightMargin(), SetAutoPageBreak()
  2972. */
  2973. public function SetMargins($left, $top, $right=-1, $keepmargins=false) {
  2974. //Set left, top and right margins
  2975. $this->lMargin = $left;
  2976. $this->tMargin = $top;
  2977. if ($right == -1) {
  2978. $right = $left;
  2979. }
  2980. $this->rMargin = $right;
  2981. if ($keepmargins) {
  2982. // overwrite original values
  2983. $this->original_lMargin = $this->lMargin;
  2984. $this->original_rMargin = $this->rMargin;
  2985. }
  2986. }
  2987. /**
  2988. * 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.
  2989. * @param $margin (float) The margin.
  2990. * @public
  2991. * @since 1.4
  2992. * @see SetTopMargin(), SetRightMargin(), SetAutoPageBreak(), SetMargins()
  2993. */
  2994. public function SetLeftMargin($margin) {
  2995. //Set left margin
  2996. $this->lMargin = $margin;
  2997. if (($this->page > 0) AND ($this->x < $margin)) {
  2998. $this->x = $margin;
  2999. }
  3000. }
  3001. /**
  3002. * Defines the top margin. The method can be called before creating the first page.
  3003. * @param $margin (float) The margin.
  3004. * @public
  3005. * @since 1.5
  3006. * @see SetLeftMargin(), SetRightMargin(), SetAutoPageBreak(), SetMargins()
  3007. */
  3008. public function SetTopMargin($margin) {
  3009. //Set top margin
  3010. $this->tMargin = $margin;
  3011. if (($this->page > 0) AND ($this->y < $margin)) {
  3012. $this->y = $margin;
  3013. }
  3014. }
  3015. /**
  3016. * Defines the right margin. The method can be called before creating the first page.
  3017. * @param $margin (float) The margin.
  3018. * @public
  3019. * @since 1.5
  3020. * @see SetLeftMargin(), SetTopMargin(), SetAutoPageBreak(), SetMargins()
  3021. */
  3022. public function SetRightMargin($margin) {
  3023. $this->rMargin = $margin;
  3024. if (($this->page > 0) AND ($this->x > ($this->w - $margin))) {
  3025. $this->x = $this->w - $margin;
  3026. }
  3027. }
  3028. /**
  3029. * Set the same internal Cell padding for top, right, bottom, left-
  3030. * @param $pad (float) internal padding.
  3031. * @public
  3032. * @since 2.1.000 (2008-01-09)
  3033. * @see getCellPaddings(), setCellPaddings()
  3034. */
  3035. public function SetCellPadding($pad) {
  3036. if ($pad >= 0) {
  3037. $this->cell_padding['L'] = $pad;
  3038. $this->cell_padding['T'] = $pad;
  3039. $this->cell_padding['R'] = $pad;
  3040. $this->cell_padding['B'] = $pad;
  3041. }
  3042. }
  3043. /**
  3044. * Set the internal Cell paddings.
  3045. * @param $left (float) left padding
  3046. * @param $top (float) top padding
  3047. * @param $right (float) right padding
  3048. * @param $bottom (float) bottom padding
  3049. * @public
  3050. * @since 5.9.000 (2010-10-03)
  3051. * @see getCellPaddings(), SetCellPadding()
  3052. */
  3053. public function setCellPaddings($left='', $top='', $right='', $bottom='') {
  3054. if (($left !== '') AND ($left >= 0)) {
  3055. $this->cell_padding['L'] = $left;
  3056. }
  3057. if (($top !== '') AND ($top >= 0)) {
  3058. $this->cell_padding['T'] = $top;
  3059. }
  3060. if (($right !== '') AND ($right >= 0)) {
  3061. $this->cell_padding['R'] = $right;
  3062. }
  3063. if (($bottom !== '') AND ($bottom >= 0)) {
  3064. $this->cell_padding['B'] = $bottom;
  3065. }
  3066. }
  3067. /**
  3068. * Get the internal Cell padding array.
  3069. * @return array of padding values
  3070. * @public
  3071. * @since 5.9.000 (2010-10-03)
  3072. * @see setCellPaddings(), SetCellPadding()
  3073. */
  3074. public function getCellPaddings() {
  3075. return $this->cell_padding;
  3076. }
  3077. /**
  3078. * Set the internal Cell margins.
  3079. * @param $left (float) left margin
  3080. * @param $top (float) top margin
  3081. * @param $right (float) right margin
  3082. * @param $bottom (float) bottom margin
  3083. * @public
  3084. * @since 5.9.000 (2010-10-03)
  3085. * @see getCellMargins()
  3086. */
  3087. public function setCellMargins($left='', $top='', $right='', $bottom='') {
  3088. if (($left !== '') AND ($left >= 0)) {
  3089. $this->cell_margin['L'] = $left;
  3090. }
  3091. if (($top !== '') AND ($top >= 0)) {
  3092. $this->cell_margin['T'] = $top;
  3093. }
  3094. if (($right !== '') AND ($right >= 0)) {
  3095. $this->cell_margin['R'] = $right;
  3096. }
  3097. if (($bottom !== '') AND ($bottom >= 0)) {
  3098. $this->cell_margin['B'] = $bottom;
  3099. }
  3100. }
  3101. /**
  3102. * Get the internal Cell margin array.
  3103. * @return array of margin values
  3104. * @public
  3105. * @since 5.9.000 (2010-10-03)
  3106. * @see setCellMargins()
  3107. */
  3108. public function getCellMargins() {
  3109. return $this->cell_margin;
  3110. }
  3111. /**
  3112. * Adjust the internal Cell padding array to take account of the line width.
  3113. * @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)))
  3114. * @return array of adjustments
  3115. * @public
  3116. * @since 5.9.000 (2010-10-03)
  3117. */
  3118. protected function adjustCellPadding($brd=0) {
  3119. if (empty($brd)) {
  3120. return;
  3121. }
  3122. if (is_string($brd)) {
  3123. // convert string to array
  3124. $slen = strlen($brd);
  3125. $newbrd = array();
  3126. for ($i = 0; $i < $slen; ++$i) {
  3127. $newbrd[$brd{$i}] = true;
  3128. }
  3129. $brd = $newbrd;
  3130. } elseif (($brd === 1) OR ($brd === true) OR (is_numeric($brd) AND (intval($brd) > 0))) {
  3131. $brd = array('LRTB' => true);
  3132. }
  3133. if (!is_array($brd)) {
  3134. return;
  3135. }
  3136. // store current cell padding
  3137. $cp = $this->cell_padding;
  3138. // select border mode
  3139. if (isset($brd['mode'])) {
  3140. $mode = $brd['mode'];
  3141. unset($brd['mode']);
  3142. } else {
  3143. $mode = 'normal';
  3144. }
  3145. // process borders
  3146. foreach ($brd as $border => $style) {
  3147. $line_width = $this->LineWidth;
  3148. if (is_array($style) AND isset($style['width'])) {
  3149. // get border width
  3150. $line_width = $style['width'];
  3151. }
  3152. $adj = 0; // line width inside the cell
  3153. switch ($mode) {
  3154. case 'ext': {
  3155. $adj = 0;
  3156. break;
  3157. }
  3158. case 'int': {
  3159. $adj = $line_width;
  3160. break;
  3161. }
  3162. case 'normal':
  3163. default: {
  3164. $adj = ($line_width / 2);
  3165. break;
  3166. }
  3167. }
  3168. // correct internal cell padding if required to avoid overlap between text and lines
  3169. if ((strpos($border,'T') !== false) AND ($this->cell_padding['T'] < $adj)) {
  3170. $this->cell_padding['T'] = $adj;
  3171. }
  3172. if ((strpos($border,'R') !== false) AND ($this->cell_padding['R'] < $adj)) {
  3173. $this->cell_padding['R'] = $adj;
  3174. }
  3175. if ((strpos($border,'B') !== false) AND ($this->cell_padding['B'] < $adj)) {
  3176. $this->cell_padding['B'] = $adj;
  3177. }
  3178. if ((strpos($border,'L') !== false) AND ($this->cell_padding['L'] < $adj)) {
  3179. $this->cell_padding['L'] = $adj;
  3180. }
  3181. }
  3182. 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']));
  3183. }
  3184. /**
  3185. * 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.
  3186. * @param $auto (boolean) Boolean indicating if mode should be on or off.
  3187. * @param $margin (float) Distance from the bottom of the page.
  3188. * @public
  3189. * @since 1.0
  3190. * @see Cell(), MultiCell(), AcceptPageBreak()
  3191. */
  3192. public function SetAutoPageBreak($auto, $margin=0) {
  3193. $this->AutoPageBreak = $auto;
  3194. $this->bMargin = $margin;
  3195. $this->PageBreakTrigger = $this->h - $margin;
  3196. }
  3197. /**
  3198. * Return the auto-page-break mode (true or false).
  3199. * @return boolean auto-page-break mode
  3200. * @public
  3201. * @since 5.9.088
  3202. */
  3203. public function getAutoPageBreak() {
  3204. return $this->AutoPageBreak;
  3205. }
  3206. /**
  3207. * Defines the way the document is to be displayed by the viewer.
  3208. * @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>
  3209. * @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>
  3210. * @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>
  3211. * @public
  3212. * @since 1.2
  3213. */
  3214. public function SetDisplayMode($zoom, $layout='SinglePage', $mode='UseNone') {
  3215. if (($zoom == 'fullpage') OR ($zoom == 'fullwidth') OR ($zoom == 'real') OR ($zoom == 'default') OR (!is_string($zoom))) {
  3216. $this->ZoomMode = $zoom;
  3217. } else {
  3218. $this->Error('Incorrect zoom display mode: '.$zoom);
  3219. }
  3220. switch ($layout) {
  3221. case 'default':
  3222. case 'single':
  3223. case 'SinglePage': {
  3224. $this->LayoutMode = 'SinglePage';
  3225. break;
  3226. }
  3227. case 'continuous':
  3228. case 'OneColumn': {
  3229. $this->LayoutMode = 'OneColumn';
  3230. break;
  3231. }
  3232. case 'two':
  3233. case 'TwoColumnLeft': {
  3234. $this->LayoutMode = 'TwoColumnLeft';
  3235. break;
  3236. }
  3237. case 'TwoColumnRight': {
  3238. $this->LayoutMode = 'TwoColumnRight';
  3239. break;
  3240. }
  3241. case 'TwoPageLeft': {
  3242. $this->LayoutMode = 'TwoPageLeft';
  3243. break;
  3244. }
  3245. case 'TwoPageRight': {
  3246. $this->LayoutMode = 'TwoPageRight';
  3247. break;
  3248. }
  3249. default: {
  3250. $this->LayoutMode = 'SinglePage';
  3251. }
  3252. }
  3253. switch ($mode) {
  3254. case 'UseNone': {
  3255. $this->PageMode = 'UseNone';
  3256. break;
  3257. }
  3258. case 'UseOutlines': {
  3259. $this->PageMode = 'UseOutlines';
  3260. break;
  3261. }
  3262. case 'UseThumbs': {
  3263. $this->PageMode = 'UseThumbs';
  3264. break;
  3265. }
  3266. case 'FullScreen': {
  3267. $this->PageMode = 'FullScreen';
  3268. break;
  3269. }
  3270. case 'UseOC': {
  3271. $this->PageMode = 'UseOC';
  3272. break;
  3273. }
  3274. case '': {
  3275. $this->PageMode = 'UseAttachments';
  3276. break;
  3277. }
  3278. default: {
  3279. $this->PageMode = 'UseNone';
  3280. }
  3281. }
  3282. }
  3283. /**
  3284. * 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.
  3285. * Note: the Zlib extension is required for this feature. If not present, compression will be turned off.
  3286. * @param $compress (boolean) Boolean indicating if compression must be enabled.
  3287. * @public
  3288. * @since 1.4
  3289. */
  3290. public function SetCompression($compress) {
  3291. if (function_exists('gzcompress')) {
  3292. $this->compress = $compress ? true : false;
  3293. } else {
  3294. $this->compress = false;
  3295. }
  3296. }
  3297. /**
  3298. * Turn on/off Unicode mode for document information dictionary (meta tags).
  3299. * This has effect only when unicode mode is set to false.
  3300. * @param $unicode (boolean) if true set the meta information in Unicode
  3301. * @since 5.9.027 (2010-12-01)
  3302. * @public
  3303. */
  3304. public function SetDocInfoUnicode($unicode=true) {
  3305. $this->docinfounicode = $unicode ? true : false;
  3306. }
  3307. /**
  3308. * Defines the title of the document.
  3309. * @param $title (string) The title.
  3310. * @public
  3311. * @since 1.2
  3312. * @see SetAuthor(), SetCreator(), SetKeywords(), SetSubject()
  3313. */
  3314. public function SetTitle($title) {
  3315. $this->title = $title;
  3316. }
  3317. /**
  3318. * Defines the subject of the document.
  3319. * @param $subject (string) The subject.
  3320. * @public
  3321. * @since 1.2
  3322. * @see SetAuthor(), SetCreator(), SetKeywords(), SetTitle()
  3323. */
  3324. public function SetSubject($subject) {
  3325. $this->subject = $subject;
  3326. }
  3327. /**
  3328. * Defines the author of the document.
  3329. * @param $author (string) The name of the author.
  3330. * @public
  3331. * @since 1.2
  3332. * @see SetCreator(), SetKeywords(), SetSubject(), SetTitle()
  3333. */
  3334. public function SetAuthor($author) {
  3335. $this->author = $author;
  3336. }
  3337. /**
  3338. * Associates keywords with the document, generally in the form 'keyword1 keyword2 ...'.
  3339. * @param $keywords (string) The list of keywords.
  3340. * @public
  3341. * @since 1.2
  3342. * @see SetAuthor(), SetCreator(), SetSubject(), SetTitle()
  3343. */
  3344. public function SetKeywords($keywords) {
  3345. $this->keywords = $keywords;
  3346. }
  3347. /**
  3348. * Defines the creator of the document. This is typically the name of the application that generates the PDF.
  3349. * @param $creator (string) The name of the creator.
  3350. * @public
  3351. * @since 1.2
  3352. * @see SetAuthor(), SetKeywords(), SetSubject(), SetTitle()
  3353. */
  3354. public function SetCreator($creator) {
  3355. $this->creator = $creator;
  3356. }
  3357. /**
  3358. * 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.
  3359. * 2004-06-11 :: Nicola Asuni : changed bold tag with strong
  3360. * @param $msg (string) The error message
  3361. * @public
  3362. * @since 1.0
  3363. */
  3364. public function Error($msg) {
  3365. // unset all class variables
  3366. $this->_destroy(true);
  3367. // exit program and print error
  3368. die('<strong>TCPDF ERROR: </strong>'.$msg);
  3369. }
  3370. /**
  3371. * This method begins the generation of the PDF document.
  3372. * It is not necessary to call it explicitly because AddPage() does it automatically.
  3373. * Note: no page is created by this method
  3374. * @public
  3375. * @since 1.0
  3376. * @see AddPage(), Close()
  3377. */
  3378. public function Open() {
  3379. $this->state = 1;
  3380. }
  3381. /**
  3382. * Terminates the PDF document.
  3383. * It is not necessary to call this method explicitly because Output() does it automatically.
  3384. * If the document contains no page, AddPage() is called to prevent from getting an invalid document.
  3385. * @public
  3386. * @since 1.0
  3387. * @see Open(), Output()
  3388. */
  3389. public function Close() {
  3390. if ($this->state == 3) {
  3391. return;
  3392. }
  3393. if ($this->page == 0) {
  3394. $this->AddPage();
  3395. }
  3396. $this->endLayer();
  3397. // save current graphic settings
  3398. $gvars = $this->getGraphicVars();
  3399. $this->setEqualColumns();
  3400. $this->lastpage(true);
  3401. $this->SetAutoPageBreak(false);
  3402. $this->x = 0;
  3403. $this->y = $this->h - (1 / $this->k);
  3404. $this->lMargin = 0;
  3405. $this->_out('q');
  3406. $this->SetFont('helvetica', '', 1);
  3407. $this->setTextRenderingMode(0, false, false);
  3408. $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";
  3409. $lnk = "\x68\x74\x74\x70\x3a\x2f\x2f\x77\x77\x77\x2e\x74\x63\x70\x64\x66\x2e\x6f\x72\x67";
  3410. $this->Cell(0, 0, $msg, 0, 0, 'L', 0, $lnk, 0, false, 'D', 'B');
  3411. $this->_out('Q');
  3412. // restore graphic settings
  3413. $this->setGraphicVars($gvars);
  3414. // close page
  3415. $this->endPage();
  3416. // close document
  3417. $this->_enddoc();
  3418. // unset all class variables (except critical ones)
  3419. $this->_destroy(false);
  3420. }
  3421. /**
  3422. * Move pointer at the specified document page and update page dimensions.
  3423. * @param $pnum (int) page number (1 ... numpages)
  3424. * @param $resetmargins (boolean) if true reset left, right, top margins and Y position.
  3425. * @public
  3426. * @since 2.1.000 (2008-01-07)
  3427. * @see getPage(), lastpage(), getNumPages()
  3428. */
  3429. public function setPage($pnum, $resetmargins=false) {
  3430. if (($pnum == $this->page) AND ($this->state == 2)) {
  3431. return;
  3432. }
  3433. if (($pnum > 0) AND ($pnum <= $this->numpages)) {
  3434. $this->state = 2;
  3435. // save current graphic settings
  3436. //$gvars = $this->getGraphicVars();
  3437. $oldpage = $this->page;
  3438. $this->page = $pnum;
  3439. $this->wPt = $this->pagedim[$this->page]['w'];
  3440. $this->hPt = $this->pagedim[$this->page]['h'];
  3441. $this->w = $this->pagedim[$this->page]['wk'];
  3442. $this->h = $this->pagedim[$this->page]['hk'];
  3443. $this->tMargin = $this->pagedim[$this->page]['tm'];
  3444. $this->bMargin = $this->pagedim[$this->page]['bm'];
  3445. $this->original_lMargin = $this->pagedim[$this->page]['olm'];
  3446. $this->original_rMargin = $this->pagedim[$this->page]['orm'];
  3447. $this->AutoPageBreak = $this->pagedim[$this->page]['pb'];
  3448. $this->CurOrientation = $this->pagedim[$this->page]['or'];
  3449. $this->SetAutoPageBreak($this->AutoPageBreak, $this->bMargin);
  3450. // restore graphic settings
  3451. //$this->setGraphicVars($gvars);
  3452. if ($resetmargins) {
  3453. $this->lMargin = $this->pagedim[$this->page]['olm'];
  3454. $this->rMargin = $this->pagedim[$this->page]['orm'];
  3455. $this->SetY($this->tMargin);
  3456. } else {
  3457. // account for booklet mode
  3458. if ($this->pagedim[$this->page]['olm'] != $this->pagedim[$oldpage]['olm']) {
  3459. $deltam = $this->pagedim[$this->page]['olm'] - $this->pagedim[$this->page]['orm'];
  3460. $this->lMargin += $deltam;
  3461. $this->rMargin -= $deltam;
  3462. }
  3463. }
  3464. } else {
  3465. $this->Error('Wrong page number on setPage() function: '.$pnum);
  3466. }
  3467. }
  3468. /**
  3469. * Reset pointer to the last document page.
  3470. * @param $resetmargins (boolean) if true reset left, right, top margins and Y position.
  3471. * @public
  3472. * @since 2.0.000 (2008-01-04)
  3473. * @see setPage(), getPage(), getNumPages()
  3474. */
  3475. public function lastPage($resetmargins=false) {
  3476. $this->setPage($this->getNumPages(), $resetmargins);
  3477. }
  3478. /**
  3479. * Get current document page number.
  3480. * @return int page number
  3481. * @public
  3482. * @since 2.1.000 (2008-01-07)
  3483. * @see setPage(), lastpage(), getNumPages()
  3484. */
  3485. public function getPage() {
  3486. return $this->page;
  3487. }
  3488. /**
  3489. * Get the total number of insered pages.
  3490. * @return int number of pages
  3491. * @public
  3492. * @since 2.1.000 (2008-01-07)
  3493. * @see setPage(), getPage(), lastpage()
  3494. */
  3495. public function getNumPages() {
  3496. return $this->numpages;
  3497. }
  3498. /**
  3499. * Adds a new TOC (Table Of Content) page to the document.
  3500. * @param $orientation (string) page orientation.
  3501. * @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().
  3502. * @param $keepmargins (boolean) if true overwrites the default page margins with the current margins
  3503. * @public
  3504. * @since 5.0.001 (2010-05-06)
  3505. * @see AddPage(), startPage(), endPage(), endTOCPage()
  3506. */
  3507. public function addTOCPage($orientation='', $format='', $keepmargins=false) {
  3508. $this->AddPage($orientation, $format, $keepmargins, true);
  3509. }
  3510. /**
  3511. * Terminate the current TOC (Table Of Content) page
  3512. * @public
  3513. * @since 5.0.001 (2010-05-06)
  3514. * @see AddPage(), startPage(), endPage(), addTOCPage()
  3515. */
  3516. public function endTOCPage() {
  3517. $this->endPage(true);
  3518. }
  3519. /**
  3520. * 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).
  3521. * The origin of the coordinate system is at the top-left corner (or top-right for RTL) and increasing ordinates go downwards.
  3522. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  3523. * @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().
  3524. * @param $keepmargins (boolean) if true overwrites the default page margins with the current margins
  3525. * @param $tocpage (boolean) if true set the tocpage state to true (the added page will be used to display Table Of Content).
  3526. * @public
  3527. * @since 1.0
  3528. * @see startPage(), endPage(), addTOCPage(), endTOCPage(), getPageSizeFromFormat(), setPageFormat()
  3529. */
  3530. public function AddPage($orientation='', $format='', $keepmargins=false, $tocpage=false) {
  3531. if ($this->inxobj) {
  3532. // we are inside an XObject template
  3533. return;
  3534. }
  3535. if (!isset($this->original_lMargin) OR $keepmargins) {
  3536. $this->original_lMargin = $this->lMargin;
  3537. }
  3538. if (!isset($this->original_rMargin) OR $keepmargins) {
  3539. $this->original_rMargin = $this->rMargin;
  3540. }
  3541. // terminate previous page
  3542. $this->endPage();
  3543. // start new page
  3544. $this->startPage($orientation, $format, $tocpage);
  3545. }
  3546. /**
  3547. * Terminate the current page
  3548. * @param $tocpage (boolean) if true set the tocpage state to false (end the page used to display Table Of Content).
  3549. * @public
  3550. * @since 4.2.010 (2008-11-14)
  3551. * @see AddPage(), startPage(), addTOCPage(), endTOCPage()
  3552. */
  3553. public function endPage($tocpage=false) {
  3554. // check if page is already closed
  3555. if (($this->page == 0) OR ($this->numpages > $this->page) OR (!$this->pageopen[$this->page])) {
  3556. return;
  3557. }
  3558. // print page footer
  3559. $this->setFooter();
  3560. // close page
  3561. $this->_endpage();
  3562. // mark page as closed
  3563. $this->pageopen[$this->page] = false;
  3564. if ($tocpage) {
  3565. $this->tocpage = false;
  3566. }
  3567. }
  3568. /**
  3569. * Starts a new page to the document. The page must be closed using the endPage() function.
  3570. * The origin of the coordinate system is at the top-left corner and increasing ordinates go downwards.
  3571. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  3572. * @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().
  3573. * @param $tocpage (boolean) if true the page is designated to contain the Table-Of-Content.
  3574. * @since 4.2.010 (2008-11-14)
  3575. * @see AddPage(), endPage(), addTOCPage(), endTOCPage(), getPageSizeFromFormat(), setPageFormat()
  3576. * @public
  3577. */
  3578. public function startPage($orientation='', $format='', $tocpage=false) {
  3579. if ($tocpage) {
  3580. $this->tocpage = true;
  3581. }
  3582. if ($this->numpages > $this->page) {
  3583. // this page has been already added
  3584. $this->setPage($this->page + 1);
  3585. $this->SetY($this->tMargin);
  3586. return;
  3587. }
  3588. // start a new page
  3589. if ($this->state == 0) {
  3590. $this->Open();
  3591. }
  3592. ++$this->numpages;
  3593. $this->swapMargins($this->booklet);
  3594. // save current graphic settings
  3595. $gvars = $this->getGraphicVars();
  3596. // start new page
  3597. $this->_beginpage($orientation, $format);
  3598. // mark page as open
  3599. $this->pageopen[$this->page] = true;
  3600. // restore graphic settings
  3601. $this->setGraphicVars($gvars);
  3602. // mark this point
  3603. $this->setPageMark();
  3604. // print page header
  3605. $this->setHeader();
  3606. // restore graphic settings
  3607. $this->setGraphicVars($gvars);
  3608. // mark this point
  3609. $this->setPageMark();
  3610. // print table header (if any)
  3611. $this->setTableHeader();
  3612. // set mark for empty page check
  3613. $this->emptypagemrk[$this->page]= $this->pagelen[$this->page];
  3614. }
  3615. /**
  3616. * Set start-writing mark on current page stream used to put borders and fills.
  3617. * Borders and fills are always created after content and inserted on the position marked by this method.
  3618. * This function must be called after calling Image() function for a background image.
  3619. * Background images must be always inserted before calling Multicell() or WriteHTMLCell() or WriteHTML() functions.
  3620. * @public
  3621. * @since 4.0.016 (2008-07-30)
  3622. */
  3623. public function setPageMark() {
  3624. $this->intmrk[$this->page] = $this->pagelen[$this->page];
  3625. $this->bordermrk[$this->page] = $this->intmrk[$this->page];
  3626. $this->setContentMark();
  3627. }
  3628. /**
  3629. * Set start-writing mark on selected page.
  3630. * Borders and fills are always created after content and inserted on the position marked by this method.
  3631. * @param $page (int) page number (default is the current page)
  3632. * @protected
  3633. * @since 4.6.021 (2009-07-20)
  3634. */
  3635. protected function setContentMark($page=0) {
  3636. if ($page <= 0) {
  3637. $page = $this->page;
  3638. }
  3639. if (isset($this->footerlen[$page])) {
  3640. $this->cntmrk[$page] = $this->pagelen[$page] - $this->footerlen[$page];
  3641. } else {
  3642. $this->cntmrk[$page] = $this->pagelen[$page];
  3643. }
  3644. }
  3645. /**
  3646. * Set header data.
  3647. * @param $ln (string) header image logo
  3648. * @param $lw (string) header image logo width in mm
  3649. * @param $ht (string) string to print as title on document header
  3650. * @param $hs (string) string to print on document header
  3651. * @public
  3652. */
  3653. public function setHeaderData($ln='', $lw=0, $ht='', $hs='') {
  3654. $this->header_logo = $ln;
  3655. $this->header_logo_width = $lw;
  3656. $this->header_title = $ht;
  3657. $this->header_string = $hs;
  3658. }
  3659. /**
  3660. * Returns header data:
  3661. * <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>
  3662. * @return array()
  3663. * @public
  3664. * @since 4.0.012 (2008-07-24)
  3665. */
  3666. public function getHeaderData() {
  3667. $ret = array();
  3668. $ret['logo'] = $this->header_logo;
  3669. $ret['logo_width'] = $this->header_logo_width;
  3670. $ret['title'] = $this->header_title;
  3671. $ret['string'] = $this->header_string;
  3672. return $ret;
  3673. }
  3674. /**
  3675. * Set header margin.
  3676. * (minimum distance between header and top page margin)
  3677. * @param $hm (int) distance in user units
  3678. * @public
  3679. */
  3680. public function setHeaderMargin($hm=10) {
  3681. $this->header_margin = $hm;
  3682. }
  3683. /**
  3684. * Returns header margin in user units.
  3685. * @return float
  3686. * @since 4.0.012 (2008-07-24)
  3687. * @public
  3688. */
  3689. public function getHeaderMargin() {
  3690. return $this->header_margin;
  3691. }
  3692. /**
  3693. * Set footer margin.
  3694. * (minimum distance between footer and bottom page margin)
  3695. * @param $fm (int) distance in user units
  3696. * @public
  3697. */
  3698. public function setFooterMargin($fm=10) {
  3699. $this->footer_margin = $fm;
  3700. }
  3701. /**
  3702. * Returns footer margin in user units.
  3703. * @return float
  3704. * @since 4.0.012 (2008-07-24)
  3705. * @public
  3706. */
  3707. public function getFooterMargin() {
  3708. return $this->footer_margin;
  3709. }
  3710. /**
  3711. * Set a flag to print page header.
  3712. * @param $val (boolean) set to true to print the page header (default), false otherwise.
  3713. * @public
  3714. */
  3715. public function setPrintHeader($val=true) {
  3716. $this->print_header = $val;
  3717. }
  3718. /**
  3719. * Set a flag to print page footer.
  3720. * @param $val (boolean) set to true to print the page footer (default), false otherwise.
  3721. * @public
  3722. */
  3723. public function setPrintFooter($val=true) {
  3724. $this->print_footer = $val;
  3725. }
  3726. /**
  3727. * Return the right-bottom (or left-bottom for RTL) corner X coordinate of last inserted image
  3728. * @return float
  3729. * @public
  3730. */
  3731. public function getImageRBX() {
  3732. return $this->img_rb_x;
  3733. }
  3734. /**
  3735. * Return the right-bottom (or left-bottom for RTL) corner Y coordinate of last inserted image
  3736. * @return float
  3737. * @public
  3738. */
  3739. public function getImageRBY() {
  3740. return $this->img_rb_y;
  3741. }
  3742. /**
  3743. * Reset the xobject template used by Header() method.
  3744. * @public
  3745. */
  3746. public function resetHeaderTemplate() {
  3747. $this->header_xobjid = -1;
  3748. }
  3749. /**
  3750. * Set a flag to automatically reset the xobject template used by Header() method at each page.
  3751. * @param $val (boolean) set to true to reset Header xobject template at each page, false otherwise.
  3752. * @public
  3753. */
  3754. public function setHeaderTemplateAutoreset($val=true) {
  3755. $this->header_xobj_autoreset = $val;
  3756. }
  3757. /**
  3758. * This method is used to render the page header.
  3759. * It is automatically called by AddPage() and could be overwritten in your own inherited class.
  3760. * @public
  3761. */
  3762. public function Header() {
  3763. if ($this->header_xobjid < 0) {
  3764. // start a new XObject Template
  3765. $this->header_xobjid = $this->startTemplate($this->w, $this->tMargin);
  3766. $headerfont = $this->getHeaderFont();
  3767. $headerdata = $this->getHeaderData();
  3768. $this->y = $this->header_margin;
  3769. if ($this->rtl) {
  3770. $this->x = $this->w - $this->original_rMargin;
  3771. } else {
  3772. $this->x = $this->original_lMargin;
  3773. }
  3774. if (($headerdata['logo']) AND ($headerdata['logo'] != K_BLANK_IMAGE)) {
  3775. $imgtype = $this->getImageFileType(K_PATH_IMAGES.$headerdata['logo']);
  3776. if (($imgtype == 'eps') OR ($imgtype == 'ai')) {
  3777. $this->ImageEps(K_PATH_IMAGES.$headerdata['logo'], '', '', $headerdata['logo_width']);
  3778. } elseif ($imgtype == 'svg') {
  3779. $this->ImageSVG(K_PATH_IMAGES.$headerdata['logo'], '', '', $headerdata['logo_width']);
  3780. } else {
  3781. $this->Image(K_PATH_IMAGES.$headerdata['logo'], '', '', $headerdata['logo_width']);
  3782. }
  3783. $imgy = $this->getImageRBY();
  3784. } else {
  3785. $imgy = $this->y;
  3786. }
  3787. $cell_height = round(($this->cell_height_ratio * $headerfont[2]) / $this->k, 2);
  3788. // set starting margin for text data cell
  3789. if ($this->getRTL()) {
  3790. $header_x = $this->original_rMargin + ($headerdata['logo_width'] * 1.1);
  3791. } else {
  3792. $header_x = $this->original_lMargin + ($headerdata['logo_width'] * 1.1);
  3793. }
  3794. $cw = $this->w - $this->original_lMargin - $this->original_rMargin - ($headerdata['logo_width'] * 1.1);
  3795. $this->SetTextColor(0, 0, 0);
  3796. // header title
  3797. $this->SetFont($headerfont[0], 'B', $headerfont[2] + 1);
  3798. $this->SetX($header_x);
  3799. $this->Cell($cw, $cell_height, $headerdata['title'], 0, 1, '', 0, '', 0);
  3800. // header string
  3801. $this->SetFont($headerfont[0], $headerfont[1], $headerfont[2]);
  3802. $this->SetX($header_x);
  3803. $this->MultiCell($cw, $cell_height, $headerdata['string'], 0, '', 0, 1, '', '', true, 0, false, true, 0, 'T', false);
  3804. // print an ending header line
  3805. $this->SetLineStyle(array('width' => 0.85 / $this->k, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)));
  3806. $this->SetY((2.835 / $this->k) + max($imgy, $this->y));
  3807. if ($this->rtl) {
  3808. $this->SetX($this->original_rMargin);
  3809. } else {
  3810. $this->SetX($this->original_lMargin);
  3811. }
  3812. $this->Cell(($this->w - $this->original_lMargin - $this->original_rMargin), 0, '', 'T', 0, 'C');
  3813. $this->endTemplate();
  3814. }
  3815. // print header template
  3816. $x = 0;
  3817. $dx = 0;
  3818. if ($this->booklet AND (($this->page % 2) == 0)) {
  3819. // adjust margins for booklet mode
  3820. $dx = ($this->original_lMargin - $this->original_rMargin);
  3821. }
  3822. if ($this->rtl) {
  3823. $x = $this->w + $dx;
  3824. } else {
  3825. $x = 0 + $dx;
  3826. }
  3827. $this->printTemplate($this->header_xobjid, $x, 0, 0, 0, '', '', false);
  3828. if ($this->header_xobj_autoreset) {
  3829. // reset header xobject template at each page
  3830. $this->header_xobjid = -1;
  3831. }
  3832. }
  3833. /**
  3834. * This method is used to render the page footer.
  3835. * It is automatically called by AddPage() and could be overwritten in your own inherited class.
  3836. * @public
  3837. */
  3838. public function Footer() {
  3839. $cur_y = $this->y;
  3840. $this->SetTextColor(0, 0, 0);
  3841. //set style for cell border
  3842. $line_width = 0.85 / $this->k;
  3843. $this->SetLineStyle(array('width' => $line_width, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)));
  3844. //print document barcode
  3845. $barcode = $this->getBarcode();
  3846. if (!empty($barcode)) {
  3847. $this->Ln($line_width);
  3848. $barcode_width = round(($this->w - $this->original_lMargin - $this->original_rMargin) / 3);
  3849. $style = array(
  3850. 'position' => $this->rtl?'R':'L',
  3851. 'align' => $this->rtl?'R':'L',
  3852. 'stretch' => false,
  3853. 'fitwidth' => true,
  3854. 'cellfitalign' => '',
  3855. 'border' => false,
  3856. 'padding' => 0,
  3857. 'fgcolor' => array(0,0,0),
  3858. 'bgcolor' => false,
  3859. 'text' => false
  3860. );
  3861. $this->write1DBarcode($barcode, 'C128', '', $cur_y + $line_width, '', (($this->footer_margin / 3) - $line_width), 0.3, $style, '');
  3862. }
  3863. if (empty($this->pagegroups)) {
  3864. $pagenumtxt = $this->l['w_page'].' '.$this->getAliasNumPage().' / '.$this->getAliasNbPages();
  3865. } else {
  3866. $pagenumtxt = $this->l['w_page'].' '.$this->getPageNumGroupAlias().' / '.$this->getPageGroupAlias();
  3867. }
  3868. $this->SetY($cur_y);
  3869. //Print page number
  3870. if ($this->getRTL()) {
  3871. $this->SetX($this->original_rMargin);
  3872. $this->Cell(0, 0, $pagenumtxt, 'T', 0, 'L');
  3873. } else {
  3874. $this->SetX($this->original_lMargin);
  3875. $this->Cell(0, 0, $this->getAliasRightShift().$pagenumtxt, 'T', 0, 'R');
  3876. }
  3877. }
  3878. /**
  3879. * This method is used to render the page header.
  3880. * @protected
  3881. * @since 4.0.012 (2008-07-24)
  3882. */
  3883. protected function setHeader() {
  3884. if (!$this->print_header) {
  3885. return;
  3886. }
  3887. $this->InHeader = true;
  3888. $this->setGraphicVars($this->default_graphic_vars);
  3889. $temp_thead = $this->thead;
  3890. $temp_theadMargins = $this->theadMargins;
  3891. $lasth = $this->lasth;
  3892. $this->_out('q');
  3893. $this->rMargin = $this->original_rMargin;
  3894. $this->lMargin = $this->original_lMargin;
  3895. $this->SetCellPadding(0);
  3896. //set current position
  3897. if ($this->rtl) {
  3898. $this->SetXY($this->original_rMargin, $this->header_margin);
  3899. } else {
  3900. $this->SetXY($this->original_lMargin, $this->header_margin);
  3901. }
  3902. $this->SetFont($this->header_font[0], $this->header_font[1], $this->header_font[2]);
  3903. $this->Header();
  3904. //restore position
  3905. if ($this->rtl) {
  3906. $this->SetXY($this->original_rMargin, $this->tMargin);
  3907. } else {
  3908. $this->SetXY($this->original_lMargin, $this->tMargin);
  3909. }
  3910. $this->_out('Q');
  3911. $this->lasth = $lasth;
  3912. $this->thead = $temp_thead;
  3913. $this->theadMargins = $temp_theadMargins;
  3914. $this->newline = false;
  3915. $this->InHeader = false;
  3916. }
  3917. /**
  3918. * This method is used to render the page footer.
  3919. * @protected
  3920. * @since 4.0.012 (2008-07-24)
  3921. */
  3922. protected function setFooter() {
  3923. //Page footer
  3924. $this->InFooter = true;
  3925. // save current graphic settings
  3926. $gvars = $this->getGraphicVars();
  3927. // mark this point
  3928. $this->footerpos[$this->page] = $this->pagelen[$this->page];
  3929. $this->_out("\n");
  3930. if ($this->print_footer) {
  3931. $this->setGraphicVars($this->default_graphic_vars);
  3932. $this->current_column = 0;
  3933. $this->num_columns = 1;
  3934. $temp_thead = $this->thead;
  3935. $temp_theadMargins = $this->theadMargins;
  3936. $lasth = $this->lasth;
  3937. $this->_out('q');
  3938. $this->rMargin = $this->original_rMargin;
  3939. $this->lMargin = $this->original_lMargin;
  3940. $this->SetCellPadding(0);
  3941. //set current position
  3942. $footer_y = $this->h - $this->footer_margin;
  3943. if ($this->rtl) {
  3944. $this->SetXY($this->original_rMargin, $footer_y);
  3945. } else {
  3946. $this->SetXY($this->original_lMargin, $footer_y);
  3947. }
  3948. $this->SetFont($this->footer_font[0], $this->footer_font[1], $this->footer_font[2]);
  3949. $this->Footer();
  3950. //restore position
  3951. if ($this->rtl) {
  3952. $this->SetXY($this->original_rMargin, $this->tMargin);
  3953. } else {
  3954. $this->SetXY($this->original_lMargin, $this->tMargin);
  3955. }
  3956. $this->_out('Q');
  3957. $this->lasth = $lasth;
  3958. $this->thead = $temp_thead;
  3959. $this->theadMargins = $temp_theadMargins;
  3960. }
  3961. // restore graphic settings
  3962. $this->setGraphicVars($gvars);
  3963. $this->current_column = $gvars['current_column'];
  3964. $this->num_columns = $gvars['num_columns'];
  3965. // calculate footer length
  3966. $this->footerlen[$this->page] = $this->pagelen[$this->page] - $this->footerpos[$this->page] + 1;
  3967. $this->InFooter = false;
  3968. }
  3969. /**
  3970. * Check if we are on the page body (excluding page header and footer).
  3971. * @return true if we are not in page header nor in page footer, false otherwise.
  3972. * @protected
  3973. * @since 5.9.091 (2011-06-15)
  3974. */
  3975. protected function inPageBody() {
  3976. return (($this->InHeader === false) AND ($this->InFooter === false));
  3977. }
  3978. /**
  3979. * This method is used to render the table header on new page (if any).
  3980. * @protected
  3981. * @since 4.5.030 (2009-03-25)
  3982. */
  3983. protected function setTableHeader() {
  3984. if ($this->num_columns > 1) {
  3985. // multi column mode
  3986. return;
  3987. }
  3988. if (isset($this->theadMargins['top'])) {
  3989. // restore the original top-margin
  3990. $this->tMargin = $this->theadMargins['top'];
  3991. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  3992. $this->y = $this->tMargin;
  3993. }
  3994. if (!$this->empty_string($this->thead) AND (!$this->inthead)) {
  3995. // set margins
  3996. $prev_lMargin = $this->lMargin;
  3997. $prev_rMargin = $this->rMargin;
  3998. $prev_cell_padding = $this->cell_padding;
  3999. $this->lMargin = $this->theadMargins['lmargin'] + ($this->pagedim[$this->page]['olm'] - $this->pagedim[$this->theadMargins['page']]['olm']);
  4000. $this->rMargin = $this->theadMargins['rmargin'] + ($this->pagedim[$this->page]['orm'] - $this->pagedim[$this->theadMargins['page']]['orm']);
  4001. $this->cell_padding = $this->theadMargins['cell_padding'];
  4002. if ($this->rtl) {
  4003. $this->x = $this->w - $this->rMargin;
  4004. } else {
  4005. $this->x = $this->lMargin;
  4006. }
  4007. // account for special "cell" mode
  4008. if ($this->theadMargins['cell']) {
  4009. if ($this->rtl) {
  4010. $this->x -= $this->cell_padding['R'];
  4011. } else {
  4012. $this->x += $this->cell_padding['L'];
  4013. }
  4014. }
  4015. // print table header
  4016. $this->writeHTML($this->thead, false, false, false, false, '');
  4017. // set new top margin to skip the table headers
  4018. if (!isset($this->theadMargins['top'])) {
  4019. $this->theadMargins['top'] = $this->tMargin;
  4020. }
  4021. // store end of header position
  4022. if (!isset($this->columns[0]['th'])) {
  4023. $this->columns[0]['th'] = array();
  4024. }
  4025. $this->columns[0]['th']['\''.$this->page.'\''] = $this->y;
  4026. $this->tMargin = $this->y;
  4027. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  4028. $this->lasth = 0;
  4029. $this->lMargin = $prev_lMargin;
  4030. $this->rMargin = $prev_rMargin;
  4031. $this->cell_padding = $prev_cell_padding;
  4032. }
  4033. }
  4034. /**
  4035. * Returns the current page number.
  4036. * @return int page number
  4037. * @public
  4038. * @since 1.0
  4039. * @see getAliasNbPages()
  4040. */
  4041. public function PageNo() {
  4042. return $this->page;
  4043. }
  4044. /**
  4045. * Defines a new spot color.
  4046. * It can be expressed in RGB components or gray scale.
  4047. * The method can be called before the first page is created and the value is retained from page to page.
  4048. * @param $name (string) name of the spot color
  4049. * @param $c (int) Cyan color for CMYK. Value between 0 and 100
  4050. * @param $m (int) Magenta color for CMYK. Value between 0 and 100
  4051. * @param $y (int) Yellow color for CMYK. Value between 0 and 100
  4052. * @param $k (int) Key (Black) color for CMYK. Value between 0 and 100
  4053. * @public
  4054. * @since 4.0.024 (2008-09-12)
  4055. * @see SetDrawSpotColor(), SetFillSpotColor(), SetTextSpotColor()
  4056. */
  4057. public function AddSpotColor($name, $c, $m, $y, $k) {
  4058. if (!isset($this->spot_colors[$name])) {
  4059. $i = 1 + count($this->spot_colors);
  4060. $this->spot_colors[$name] = array('i' => $i, 'c' => $c, 'm' => $m, 'y' => $y, 'k' => $k);
  4061. }
  4062. $color = preg_replace('/[\s]*/', '', $name); // remove extra spaces
  4063. $color = strtolower($color);
  4064. if (!isset($this->spotcolor[$color])) {
  4065. $this->spotcolor[$color] = array($c, $m, $y, $k, $name);
  4066. }
  4067. }
  4068. /**
  4069. * Defines the color used for all drawing operations (lines, rectangles and cell borders).
  4070. * It can be expressed in RGB, CMYK or GRAY SCALE components.
  4071. * The method can be called before the first page is created and the value is retained from page to page.
  4072. * @param $color (array) array of colors
  4073. * @param $ret (boolean) if true do not send the PDF command.
  4074. * @return string the PDF command
  4075. * @public
  4076. * @since 3.1.000 (2008-06-11)
  4077. * @see SetDrawColor()
  4078. */
  4079. public function SetDrawColorArray($color, $ret=false) {
  4080. if (is_array($color)) {
  4081. $color = array_values($color);
  4082. $r = isset($color[0]) ? $color[0] : -1;
  4083. $g = isset($color[1]) ? $color[1] : -1;
  4084. $b = isset($color[2]) ? $color[2] : -1;
  4085. $k = isset($color[3]) ? $color[3] : -1;
  4086. $name = isset($color[4]) ? $color[4] : ''; // spot color name
  4087. if ($r >= 0) {
  4088. return $this->SetDrawColor($r, $g, $b, $k, $ret, $name);
  4089. }
  4090. }
  4091. return '';
  4092. }
  4093. /**
  4094. * 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.
  4095. * @param $col1 (int) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  4096. * @param $col2 (int) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  4097. * @param $col3 (int) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  4098. * @param $col4 (int) KEY (BLACK) color for CMYK (0-100).
  4099. * @param $ret (boolean) if true do not send the command.
  4100. * @param $name (string) spot color name (if any)
  4101. * @return string the PDF command
  4102. * @public
  4103. * @since 1.3
  4104. * @see SetDrawColorArray(), SetFillColor(), SetTextColor(), Line(), Rect(), Cell(), MultiCell()
  4105. */
  4106. public function SetDrawColor($col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
  4107. // set default values
  4108. if (!is_numeric($col1)) {
  4109. $col1 = 0;
  4110. }
  4111. if (!is_numeric($col2)) {
  4112. $col2 = -1;
  4113. }
  4114. if (!is_numeric($col3)) {
  4115. $col3 = -1;
  4116. }
  4117. if (!is_numeric($col4)) {
  4118. $col4 = -1;
  4119. }
  4120. //Set color for all stroking operations
  4121. if (($col2 == -1) AND ($col3 == -1) AND ($col4 == -1)) {
  4122. // Grey scale
  4123. $this->DrawColor = sprintf('%.3F G', ($col1 / 255));
  4124. $this->strokecolor = array('G' => $col1);
  4125. } elseif ($col4 == -1) {
  4126. // RGB
  4127. $this->DrawColor = sprintf('%.3F %.3F %.3F RG', ($col1 / 255), ($col2 / 255), ($col3 / 255));
  4128. $this->strokecolor = array('R' => $col1, 'G' => $col2, 'B' => $col3);
  4129. } elseif (empty($name)) {
  4130. // CMYK
  4131. $this->DrawColor = sprintf('%.3F %.3F %.3F %.3F K', ($col1 / 100), ($col2 / 100), ($col3 / 100), ($col4 / 100));
  4132. $this->strokecolor = array('C' => $col1, 'M' => $col2, 'Y' => $col3, 'K' => $col4);
  4133. } else {
  4134. // SPOT COLOR
  4135. $this->AddSpotColor($name, $col1, $col2, $col3, $col4);
  4136. $this->DrawColor = sprintf('/CS%d CS %.3F SCN', $this->spot_colors[$name]['i'], 1);
  4137. $this->strokecolor = array('C' => $col1, 'M' => $col2, 'Y' => $col3, 'K' => $col4, 'name' => $name);
  4138. }
  4139. if ($this->page > 0) {
  4140. if (!$ret) {
  4141. $this->_out($this->DrawColor);
  4142. }
  4143. return $this->DrawColor;
  4144. }
  4145. return '';
  4146. }
  4147. /**
  4148. * Defines the spot color used for all drawing operations (lines, rectangles and cell borders).
  4149. * @param $name (string) name of the spot color
  4150. * @param $tint (int) the intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  4151. * @public
  4152. * @since 4.0.024 (2008-09-12)
  4153. * @see AddSpotColor(), SetFillSpotColor(), SetTextSpotColor()
  4154. */
  4155. public function SetDrawSpotColor($name, $tint=100) {
  4156. if (!isset($this->spot_colors[$name])) {
  4157. $this->Error('Undefined spot color: '.$name);
  4158. }
  4159. $this->DrawColor = sprintf('/CS%d CS %.3F SCN', $this->spot_colors[$name]['i'], ($tint / 100));
  4160. $this->strokecolor = array('C' => $this->spot_colors[$name]['c'], 'M' => $this->spot_colors[$name]['m'], 'Y' => $this->spot_colors[$name]['y'], 'K' => $this->spot_colors[$name]['k'], 'name' => $name);
  4161. if ($this->page > 0) {
  4162. $this->_out($this->DrawColor);
  4163. }
  4164. }
  4165. /**
  4166. * Defines the color used for all filling operations (filled rectangles and cell backgrounds).
  4167. * It can be expressed in RGB, CMYK or GRAY SCALE components.
  4168. * The method can be called before the first page is created and the value is retained from page to page.
  4169. * @param $color (array) array of colors
  4170. * @param $ret (boolean) if true do not send the PDF command.
  4171. * @public
  4172. * @since 3.1.000 (2008-6-11)
  4173. * @see SetFillColor()
  4174. */
  4175. public function SetFillColorArray($color, $ret=false) {
  4176. if (is_array($color)) {
  4177. $color = array_values($color);
  4178. $r = isset($color[0]) ? $color[0] : -1;
  4179. $g = isset($color[1]) ? $color[1] : -1;
  4180. $b = isset($color[2]) ? $color[2] : -1;
  4181. $k = isset($color[3]) ? $color[3] : -1;
  4182. $name = isset($color[4]) ? $color[4] : ''; // spot color name
  4183. if ($r >= 0) {
  4184. $this->SetFillColor($r, $g, $b, $k, $ret, $name);
  4185. }
  4186. }
  4187. }
  4188. /**
  4189. * 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.
  4190. * @param $col1 (int) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  4191. * @param $col2 (int) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  4192. * @param $col3 (int) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  4193. * @param $col4 (int) KEY (BLACK) color for CMYK (0-100).
  4194. * @param $ret (boolean) if true do not send the command.
  4195. * @param $name (string) spot color name (if any)
  4196. * @return string the PDF command
  4197. * @public
  4198. * @since 1.3
  4199. * @see SetFillColorArray(), SetDrawColor(), SetTextColor(), Rect(), Cell(), MultiCell()
  4200. */
  4201. public function SetFillColor($col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
  4202. // set default values
  4203. if (!is_numeric($col1)) {
  4204. $col1 = 0;
  4205. }
  4206. if (!is_numeric($col2)) {
  4207. $col2 = -1;
  4208. }
  4209. if (!is_numeric($col3)) {
  4210. $col3 = -1;
  4211. }
  4212. if (!is_numeric($col4)) {
  4213. $col4 = -1;
  4214. }
  4215. //Set color for all filling operations
  4216. if (($col2 == -1) AND ($col3 == -1) AND ($col4 == -1)) {
  4217. // Grey scale
  4218. $this->FillColor = sprintf('%.3F g', ($col1 / 255));
  4219. $this->bgcolor = array('G' => $col1);
  4220. } elseif ($col4 == -1) {
  4221. // RGB
  4222. $this->FillColor = sprintf('%.3F %.3F %.3F rg', ($col1 / 255), ($col2 / 255), ($col3 / 255));
  4223. $this->bgcolor = array('R' => $col1, 'G' => $col2, 'B' => $col3);
  4224. } elseif (empty($name)) {
  4225. // CMYK
  4226. $this->FillColor = sprintf('%.3F %.3F %.3F %.3F k', ($col1 / 100), ($col2 / 100), ($col3 / 100), ($col4 / 100));
  4227. $this->bgcolor = array('C' => $col1, 'M' => $col2, 'Y' => $col3, 'K' => $col4);
  4228. } else {
  4229. // SPOT COLOR
  4230. $this->AddSpotColor($name, $col1, $col2, $col3, $col4);
  4231. $this->FillColor = sprintf('/CS%d cs %.3F scn', $this->spot_colors[$name]['i'], 1);
  4232. $this->bgcolor = array('C' => $col1, 'M' => $col2, 'Y' => $col3, 'K' => $col4, 'name' => $name);
  4233. }
  4234. $this->ColorFlag = ($this->FillColor != $this->TextColor);
  4235. if ($this->page > 0) {
  4236. if (!$ret) {
  4237. $this->_out($this->FillColor);
  4238. }
  4239. return $this->FillColor;
  4240. }
  4241. return '';
  4242. }
  4243. /**
  4244. * Defines the spot color used for all filling operations (filled rectangles and cell backgrounds).
  4245. * @param $name (string) name of the spot color
  4246. * @param $tint (int) the intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  4247. * @public
  4248. * @since 4.0.024 (2008-09-12)
  4249. * @see AddSpotColor(), SetDrawSpotColor(), SetTextSpotColor()
  4250. */
  4251. public function SetFillSpotColor($name, $tint=100) {
  4252. if (!isset($this->spot_colors[$name])) {
  4253. $this->Error('Undefined spot color: '.$name);
  4254. }
  4255. $this->FillColor = sprintf('/CS%d cs %.3F scn', $this->spot_colors[$name]['i'], ($tint / 100));
  4256. $this->bgcolor = array('C' => $this->spot_colors[$name]['c'], 'M' => $this->spot_colors[$name]['m'], 'Y' => $this->spot_colors[$name]['y'], 'K' => $this->spot_colors[$name]['k'], 'name' => $name);
  4257. $this->ColorFlag = ($this->FillColor != $this->TextColor);
  4258. if ($this->page > 0) {
  4259. $this->_out($this->FillColor);
  4260. }
  4261. }
  4262. /**
  4263. * Defines the color used for text. It can be expressed in RGB components or gray scale.
  4264. * The method can be called before the first page is created and the value is retained from page to page.
  4265. * @param $color (array) array of colors
  4266. * @param $ret (boolean) if true do not send the PDF command.
  4267. * @public
  4268. * @since 3.1.000 (2008-6-11)
  4269. * @see SetFillColor()
  4270. */
  4271. public function SetTextColorArray($color, $ret=false) {
  4272. if (is_array($color)) {
  4273. $color = array_values($color);
  4274. $r = isset($color[0]) ? $color[0] : -1;
  4275. $g = isset($color[1]) ? $color[1] : -1;
  4276. $b = isset($color[2]) ? $color[2] : -1;
  4277. $k = isset($color[3]) ? $color[3] : -1;
  4278. $name = isset($color[4]) ? $color[4] : ''; // spot color name
  4279. if ($r >= 0) {
  4280. $this->SetTextColor($r, $g, $b, $k, $ret, $name);
  4281. }
  4282. }
  4283. }
  4284. /**
  4285. * 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.
  4286. * @param $col1 (int) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  4287. * @param $col2 (int) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  4288. * @param $col3 (int) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  4289. * @param $col4 (int) KEY (BLACK) color for CMYK (0-100).
  4290. * @param $ret (boolean) if true do not send the command.
  4291. * @param $name (string) spot color name (if any)
  4292. * @public
  4293. * @since 1.3
  4294. * @see SetTextColorArray(), SetDrawColor(), SetFillColor(), Text(), Cell(), MultiCell()
  4295. */
  4296. public function SetTextColor($col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
  4297. // set default values
  4298. if (!is_numeric($col1)) {
  4299. $col1 = 0;
  4300. }
  4301. if (!is_numeric($col2)) {
  4302. $col2 = -1;
  4303. }
  4304. if (!is_numeric($col3)) {
  4305. $col3 = -1;
  4306. }
  4307. if (!is_numeric($col4)) {
  4308. $col4 = -1;
  4309. }
  4310. //Set color for text
  4311. if (($col2 == -1) AND ($col3 == -1) AND ($col4 == -1)) {
  4312. // Grey scale
  4313. $this->TextColor = sprintf('%.3F g', ($col1 / 255));
  4314. $this->fgcolor = array('G' => $col1);
  4315. } elseif ($col4 == -1) {
  4316. // RGB
  4317. $this->TextColor = sprintf('%.3F %.3F %.3F rg', ($col1 / 255), ($col2 / 255), ($col3 / 255));
  4318. $this->fgcolor = array('R' => $col1, 'G' => $col2, 'B' => $col3);
  4319. } elseif (empty($name)) {
  4320. // CMYK
  4321. $this->TextColor = sprintf('%.3F %.3F %.3F %.3F k', ($col1 / 100), ($col2 / 100), ($col3 / 100), ($col4 / 100));
  4322. $this->fgcolor = array('C' => $col1, 'M' => $col2, 'Y' => $col3, 'K' => $col4);
  4323. } else {
  4324. // SPOT COLOR
  4325. $this->AddSpotColor($name, $col1, $col2, $col3, $col4);
  4326. $this->TextColor = sprintf('/CS%d cs %.3F scn', $this->spot_colors[$name]['i'], 1);
  4327. $this->fgcolor = array('C' => $col1, 'M' => $col2, 'Y' => $col3, 'K' => $col4, 'name' => $name);
  4328. }
  4329. $this->ColorFlag = ($this->FillColor != $this->TextColor);
  4330. }
  4331. /**
  4332. * Defines the spot color used for text.
  4333. * @param $name (string) name of the spot color
  4334. * @param $tint (int) the intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  4335. * @public
  4336. * @since 4.0.024 (2008-09-12)
  4337. * @see AddSpotColor(), SetDrawSpotColor(), SetFillSpotColor()
  4338. */
  4339. public function SetTextSpotColor($name, $tint=100) {
  4340. if (!isset($this->spot_colors[$name])) {
  4341. $this->Error('Undefined spot color: '.$name);
  4342. }
  4343. $this->TextColor = sprintf('/CS%d cs %.3F scn', $this->spot_colors[$name]['i'], ($tint / 100));
  4344. $this->fgcolor = array('C' => $this->spot_colors[$name]['c'], 'M' => $this->spot_colors[$name]['m'], 'Y' => $this->spot_colors[$name]['y'], 'K' => $this->spot_colors[$name]['k'], 'name' => $name);
  4345. $this->ColorFlag = ($this->FillColor != $this->TextColor);
  4346. if ($this->page > 0) {
  4347. $this->_out($this->TextColor);
  4348. }
  4349. }
  4350. /**
  4351. * Returns the length of a string in user unit. A font must be selected.<br>
  4352. * @param $s (string) The string whose length is to be computed
  4353. * @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.
  4354. * @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.
  4355. * @param $fontsize (float) Font size in points. The default value is the current size.
  4356. * @param $getarray (boolean) if true returns an array of characters widths, if false returns the total length.
  4357. * @return mixed int total string length or array of characted widths
  4358. * @author Nicola Asuni
  4359. * @public
  4360. * @since 1.2
  4361. */
  4362. public function GetStringWidth($s, $fontname='', $fontstyle='', $fontsize=0, $getarray=false) {
  4363. return $this->GetArrStringWidth($this->utf8Bidi($this->UTF8StringToArray($s), $s, $this->tmprtl), $fontname, $fontstyle, $fontsize, $getarray);
  4364. }
  4365. /**
  4366. * Returns the string length of an array of chars in user unit or an array of characters widths. A font must be selected.<br>
  4367. * @param $sa (string) The array of chars whose total length is to be computed
  4368. * @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.
  4369. * @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.
  4370. * @param $fontsize (float) Font size in points. The default value is the current size.
  4371. * @param $getarray (boolean) if true returns an array of characters widths, if false returns the total length.
  4372. * @return mixed int total string length or array of characted widths
  4373. * @author Nicola Asuni
  4374. * @public
  4375. * @since 2.4.000 (2008-03-06)
  4376. */
  4377. public function GetArrStringWidth($sa, $fontname='', $fontstyle='', $fontsize=0, $getarray=false) {
  4378. // store current values
  4379. if (!$this->empty_string($fontname)) {
  4380. $prev_FontFamily = $this->FontFamily;
  4381. $prev_FontStyle = $this->FontStyle;
  4382. $prev_FontSizePt = $this->FontSizePt;
  4383. $this->SetFont($fontname, $fontstyle, $fontsize);
  4384. }
  4385. // convert UTF-8 array to Latin1 if required
  4386. $sa = $this->UTF8ArrToLatin1($sa);
  4387. $w = 0; // total width
  4388. $wa = array(); // array of characters widths
  4389. foreach ($sa as $ck => $char) {
  4390. // character width
  4391. $cw = $this->GetCharWidth($char, isset($sa[($ck + 1)]));
  4392. $wa[] = $cw;
  4393. $w += $cw;
  4394. }
  4395. // restore previous values
  4396. if (!$this->empty_string($fontname)) {
  4397. $this->SetFont($prev_FontFamily, $prev_FontStyle, $prev_FontSizePt);
  4398. }
  4399. if ($getarray) {
  4400. return $wa;
  4401. }
  4402. return $w;
  4403. }
  4404. /**
  4405. * Returns the length of the char in user unit for the current font considering current stretching and spacing (tracking/kerning).
  4406. * @param $char (int) The char code whose length is to be returned
  4407. * @param $notlast (boolean) set to false for the latest character on string, true otherwise (default)
  4408. * @return float char width
  4409. * @author Nicola Asuni
  4410. * @public
  4411. * @since 2.4.000 (2008-03-06)
  4412. */
  4413. public function GetCharWidth($char, $notlast=true) {
  4414. // get raw width
  4415. $chw = $this->getRawCharWidth($char);
  4416. if (($this->font_spacing != 0) AND $notlast) {
  4417. // increase/decrease font spacing
  4418. $chw += $this->font_spacing;
  4419. }
  4420. if ($this->font_stretching != 100) {
  4421. // fixed stretching mode
  4422. $chw *= ($this->font_stretching / 100);
  4423. }
  4424. return $chw;
  4425. }
  4426. /**
  4427. * Returns the length of the char in user unit for the current font.
  4428. * @param $char (int) The char code whose length is to be returned
  4429. * @return float char width
  4430. * @author Nicola Asuni
  4431. * @public
  4432. * @since 5.9.000 (2010-09-28)
  4433. */
  4434. public function getRawCharWidth($char) {
  4435. if ($char == 173) {
  4436. // SHY character will not be printed
  4437. return (0);
  4438. }
  4439. if (isset($this->CurrentFont['cw'][$char])) {
  4440. $w = $this->CurrentFont['cw'][$char];
  4441. } elseif (isset($this->CurrentFont['dw'])) {
  4442. // default width
  4443. $w = $this->CurrentFont['dw'];
  4444. } elseif (isset($this->CurrentFont['cw'][32])) {
  4445. // default width
  4446. $w = $this->CurrentFont['cw'][32];
  4447. } else {
  4448. $w = 600;
  4449. }
  4450. return ($w * $this->FontSize / 1000);
  4451. }
  4452. /**
  4453. * Returns the numbero of characters in a string.
  4454. * @param $s (string) The input string.
  4455. * @return int number of characters
  4456. * @public
  4457. * @since 2.0.0001 (2008-01-07)
  4458. */
  4459. public function GetNumChars($s) {
  4460. if ($this->isUnicodeFont()) {
  4461. return count($this->UTF8StringToArray($s));
  4462. }
  4463. return strlen($s);
  4464. }
  4465. /**
  4466. * Fill the list of available fonts ($this->fontlist).
  4467. * @protected
  4468. * @since 4.0.013 (2008-07-28)
  4469. */
  4470. protected function getFontsList() {
  4471. $fontsdir = opendir($this->_getfontpath());
  4472. while (($file = readdir($fontsdir)) !== false) {
  4473. if (substr($file, -4) == '.php') {
  4474. array_push($this->fontlist, strtolower(basename($file, '.php')));
  4475. }
  4476. }
  4477. closedir($fontsdir);
  4478. }
  4479. /**
  4480. * Imports a TrueType, Type1, core, or CID0 font and makes it available.
  4481. * It is necessary to generate a font definition file first (read /fonts/utils/README.TXT).
  4482. * 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.
  4483. * @param $family (string) Font family. The name can be chosen arbitrarily. If it is a standard family name, it will override the corresponding font.
  4484. * @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>
  4485. * @param $fontfile (string) The font definition file. By default, the name is built from the family and style, in lower case with no spaces.
  4486. * @return array containing the font data, or false in case of error.
  4487. * @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.
  4488. * @public
  4489. * @since 1.5
  4490. * @see SetFont(), setFontSubsetting()
  4491. */
  4492. public function AddFont($family, $style='', $fontfile='', $subset='default') {
  4493. if ($subset === 'default') {
  4494. $subset = $this->font_subsetting;
  4495. }
  4496. if ($this->empty_string($family)) {
  4497. if (!$this->empty_string($this->FontFamily)) {
  4498. $family = $this->FontFamily;
  4499. } else {
  4500. $this->Error('Empty font family');
  4501. }
  4502. }
  4503. // move embedded styles on $style
  4504. if (substr($family, -1) == 'I') {
  4505. $style .= 'I';
  4506. $family = substr($family, 0, -1);
  4507. }
  4508. if (substr($family, -1) == 'B') {
  4509. $style .= 'B';
  4510. $family = substr($family, 0, -1);
  4511. }
  4512. // normalize family name
  4513. $family = strtolower($family);
  4514. if ((!$this->isunicode) AND ($family == 'arial')) {
  4515. $family = 'helvetica';
  4516. }
  4517. if (($family == 'symbol') OR ($family == 'zapfdingbats')) {
  4518. $style = '';
  4519. }
  4520. $tempstyle = strtoupper($style);
  4521. $style = '';
  4522. // underline
  4523. if (strpos($tempstyle, 'U') !== false) {
  4524. $this->underline = true;
  4525. } else {
  4526. $this->underline = false;
  4527. }
  4528. // line-through (deleted)
  4529. if (strpos($tempstyle, 'D') !== false) {
  4530. $this->linethrough = true;
  4531. } else {
  4532. $this->linethrough = false;
  4533. }
  4534. // overline
  4535. if (strpos($tempstyle, 'O') !== false) {
  4536. $this->overline = true;
  4537. } else {
  4538. $this->overline = false;
  4539. }
  4540. // bold
  4541. if (strpos($tempstyle, 'B') !== false) {
  4542. $style .= 'B';
  4543. }
  4544. // oblique
  4545. if (strpos($tempstyle, 'I') !== false) {
  4546. $style .= 'I';
  4547. }
  4548. $bistyle = $style;
  4549. $fontkey = $family.$style;
  4550. $font_style = $style.($this->underline ? 'U' : '').($this->linethrough ? 'D' : '').($this->overline ? 'O' : '');
  4551. $fontdata = array('fontkey' => $fontkey, 'family' => $family, 'style' => $font_style);
  4552. // check if the font has been already added
  4553. $fb = $this->getFontBuffer($fontkey);
  4554. if ($fb !== false) {
  4555. if ($this->inxobj) {
  4556. // we are inside an XObject template
  4557. $this->xobjects[$this->xobjid]['fonts'][$fontkey] = $fb['i'];
  4558. }
  4559. return $fontdata;
  4560. }
  4561. if (isset($type)) {
  4562. unset($type);
  4563. }
  4564. if (isset($cw)) {
  4565. unset($cw);
  4566. }
  4567. // get specified font directory (if any)
  4568. $fontdir = false;
  4569. if (!$this->empty_string($fontfile)) {
  4570. $fontdir = dirname($fontfile);
  4571. if ($this->empty_string($fontdir) OR ($fontdir == '.')) {
  4572. $fontdir = '';
  4573. } else {
  4574. $fontdir .= '/';
  4575. }
  4576. }
  4577. $missing_style = false; // true when the font style variation is missing
  4578. // search and include font file
  4579. if ($this->empty_string($fontfile) OR (!file_exists($fontfile))) {
  4580. // build a standard filenames for specified font
  4581. $tmp_fontfile = str_replace(' ', '', $family).strtolower($style).'.php';
  4582. // search files on various directories
  4583. if (($fontdir !== false) AND file_exists($fontdir.$tmp_fontfile)) {
  4584. $fontfile = $fontdir.$tmp_fontfile;
  4585. } elseif (file_exists($this->_getfontpath().$tmp_fontfile)) {
  4586. $fontfile = $this->_getfontpath().$tmp_fontfile;
  4587. } elseif (file_exists($tmp_fontfile)) {
  4588. $fontfile = $tmp_fontfile;
  4589. } elseif (!$this->empty_string($style)) {
  4590. $missing_style = true;
  4591. // try to remove the style part
  4592. $tmp_fontfile = str_replace(' ', '', $family).'.php';
  4593. if (($fontdir !== false) AND file_exists($fontdir.$tmp_fontfile)) {
  4594. $fontfile = $fontdir.$tmp_fontfile;
  4595. } elseif (file_exists($this->_getfontpath().$tmp_fontfile)) {
  4596. $fontfile = $this->_getfontpath().$tmp_fontfile;
  4597. } else {
  4598. $fontfile = $tmp_fontfile;
  4599. }
  4600. }
  4601. }
  4602. // include font file
  4603. if (file_exists($fontfile)) {
  4604. include($fontfile);
  4605. } else {
  4606. $this->Error('Could not include font definition file: '.$family.'');
  4607. }
  4608. // check font parameters
  4609. if ((!isset($type)) OR (!isset($cw))) {
  4610. $this->Error('The font definition file has a bad format: '.$fontfile.'');
  4611. }
  4612. // SET default parameters
  4613. if (!isset($file) OR $this->empty_string($file)) {
  4614. $file = '';
  4615. }
  4616. if (!isset($enc) OR $this->empty_string($enc)) {
  4617. $enc = '';
  4618. }
  4619. if (!isset($cidinfo) OR $this->empty_string($cidinfo)) {
  4620. $cidinfo = array('Registry'=>'Adobe', 'Ordering'=>'Identity', 'Supplement'=>0);
  4621. $cidinfo['uni2cid'] = array();
  4622. }
  4623. if (!isset($ctg) OR $this->empty_string($ctg)) {
  4624. $ctg = '';
  4625. }
  4626. if (!isset($desc) OR $this->empty_string($desc)) {
  4627. $desc = array();
  4628. }
  4629. if (!isset($up) OR $this->empty_string($up)) {
  4630. $up = -100;
  4631. }
  4632. if (!isset($ut) OR $this->empty_string($ut)) {
  4633. $ut = 50;
  4634. }
  4635. if (!isset($cw) OR $this->empty_string($cw)) {
  4636. $cw = array();
  4637. }
  4638. if (!isset($dw) OR $this->empty_string($dw)) {
  4639. // set default width
  4640. if (isset($desc['MissingWidth']) AND ($desc['MissingWidth'] > 0)) {
  4641. $dw = $desc['MissingWidth'];
  4642. } elseif (isset($cw[32])) {
  4643. $dw = $cw[32];
  4644. } else {
  4645. $dw = 600;
  4646. }
  4647. }
  4648. ++$this->numfonts;
  4649. if ($type == 'core') {
  4650. $name = $this->CoreFonts[$fontkey];
  4651. $subset = false;
  4652. } elseif (($type == 'TrueType') OR ($type == 'Type1')) {
  4653. $subset = false;
  4654. } elseif ($type == 'TrueTypeUnicode') {
  4655. $enc = 'Identity-H';
  4656. } elseif ($type != 'cidfont0') {
  4657. $this->Error('Unknow font type: '.$type.'');
  4658. }
  4659. // set name if unset
  4660. if (!isset($name) OR empty($name)) {
  4661. $name = $fontkey;
  4662. }
  4663. // create artificial font style variations if missing (only works with non-embedded fonts)
  4664. if (($type != 'core') AND $missing_style) {
  4665. // style variations
  4666. $styles = array('' => '', 'B' => ',Bold', 'I' => ',Italic', 'BI' => ',BoldItalic');
  4667. $name .= $styles[$bistyle];
  4668. // artificial bold
  4669. if (strpos($bistyle, 'B') !== false) {
  4670. if (isset($desc['StemV'])) {
  4671. $desc['StemV'] *= 2;
  4672. } else {
  4673. $desc['StemV'] = 120;
  4674. }
  4675. }
  4676. // artificial italic
  4677. if (strpos($bistyle, 'I') !== false) {
  4678. if (isset($desc['ItalicAngle'])) {
  4679. $desc['ItalicAngle'] -= 11;
  4680. } else {
  4681. $desc['ItalicAngle'] = -11;
  4682. }
  4683. if (isset($desc['Flags'])) {
  4684. $desc['Flags'] |= 128; //bit 7
  4685. } else {
  4686. $desc['Flags'] = 128;
  4687. }
  4688. }
  4689. }
  4690. // initialize subsetchars to contain default ASCII values (0-255)
  4691. $subsetchars = array_fill(0, 256, true);
  4692. $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));
  4693. if ($this->inxobj) {
  4694. // we are inside an XObject template
  4695. $this->xobjects[$this->xobjid]['fonts'][$fontkey] = $this->numfonts;
  4696. }
  4697. if (isset($diff) AND (!empty($diff))) {
  4698. //Search existing encodings
  4699. $d = 0;
  4700. $nb = count($this->diffs);
  4701. for ($i=1; $i <= $nb; ++$i) {
  4702. if ($this->diffs[$i] == $diff) {
  4703. $d = $i;
  4704. break;
  4705. }
  4706. }
  4707. if ($d == 0) {
  4708. $d = $nb + 1;
  4709. $this->diffs[$d] = $diff;
  4710. }
  4711. $this->setFontSubBuffer($fontkey, 'diff', $d);
  4712. }
  4713. if (!$this->empty_string($file)) {
  4714. if (!isset($this->FontFiles[$file])) {
  4715. if ((strcasecmp($type,'TrueType') == 0) OR (strcasecmp($type, 'TrueTypeUnicode') == 0)) {
  4716. $this->FontFiles[$file] = array('length1' => $originalsize, 'fontdir' => $fontdir, 'subset' => $subset, 'fontkeys' => array($fontkey));
  4717. } elseif ($type != 'core') {
  4718. $this->FontFiles[$file] = array('length1' => $size1, 'length2' => $size2, 'fontdir' => $fontdir, 'subset' => $subset, 'fontkeys' => array($fontkey));
  4719. }
  4720. } else {
  4721. // update fontkeys that are sharing this font file
  4722. $this->FontFiles[$file]['subset'] = ($this->FontFiles[$file]['subset'] AND $subset);
  4723. if (!in_array($fontkey, $this->FontFiles[$file]['fontkeys'])) {
  4724. $this->FontFiles[$file]['fontkeys'][] = $fontkey;
  4725. }
  4726. }
  4727. }
  4728. return $fontdata;
  4729. }
  4730. /**
  4731. * Sets the font used to print character strings.
  4732. * The font can be either a standard one or a font added via the AddFont() method. Standard fonts use Windows encoding cp1252 (Western Europe).
  4733. * The method can be called before the first page is created and the font is retained from page to page.
  4734. * If you just wish to change the current font size, it is simpler to call SetFontSize().
  4735. * 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 />
  4736. * @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.
  4737. * @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.
  4738. * @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
  4739. * @param $fontfile (string) The font definition file. By default, the name is built from the family and style, in lower case with no spaces.
  4740. * @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.
  4741. * @author Nicola Asuni
  4742. * @public
  4743. * @since 1.0
  4744. * @see AddFont(), SetFontSize()
  4745. */
  4746. public function SetFont($family, $style='', $size=0, $fontfile='', $subset='default') {
  4747. //Select a font; size given in points
  4748. if ($size == 0) {
  4749. $size = $this->FontSizePt;
  4750. }
  4751. // try to add font (if not already added)
  4752. $fontdata = $this->AddFont($family, $style, $fontfile, $subset);
  4753. $this->FontFamily = $fontdata['family'];
  4754. $this->FontStyle = $fontdata['style'];
  4755. $this->CurrentFont = $this->getFontBuffer($fontdata['fontkey']);
  4756. $this->SetFontSize($size);
  4757. }
  4758. /**
  4759. * Defines the size of the current font.
  4760. * @param $size (float) The size (in points)
  4761. * @param $out (boolean) if true output the font size command, otherwise only set the font properties.
  4762. * @public
  4763. * @since 1.0
  4764. * @see SetFont()
  4765. */
  4766. public function SetFontSize($size, $out=true) {
  4767. // font size in points
  4768. $this->FontSizePt = $size;
  4769. // font size in user units
  4770. $this->FontSize = $size / $this->k;
  4771. // calculate some font metrics
  4772. if (isset($this->CurrentFont['desc']['FontBBox'])) {
  4773. $bbox = explode(' ', substr($this->CurrentFont['desc']['FontBBox'], 1, -1));
  4774. $font_height = ((intval($bbox[3]) - intval($bbox[1])) * $size / 1000);
  4775. } else {
  4776. $font_height = $size * 1.219;
  4777. }
  4778. if (isset($this->CurrentFont['desc']['Ascent']) AND ($this->CurrentFont['desc']['Ascent'] > 0)) {
  4779. $font_ascent = ($this->CurrentFont['desc']['Ascent'] * $size / 1000);
  4780. }
  4781. if (isset($this->CurrentFont['desc']['Descent']) AND ($this->CurrentFont['desc']['Descent'] <= 0)) {
  4782. $font_descent = (- $this->CurrentFont['desc']['Descent'] * $size / 1000);
  4783. }
  4784. if (!isset($font_ascent) AND !isset($font_descent)) {
  4785. // core font
  4786. $font_ascent = 0.76 * $font_height;
  4787. $font_descent = $font_height - $font_ascent;
  4788. } elseif (!isset($font_descent)) {
  4789. $font_descent = $font_height - $font_ascent;
  4790. } elseif (!isset($font_ascent)) {
  4791. $font_ascent = $font_height - $font_descent;
  4792. }
  4793. $this->FontAscent = $font_ascent / $this->k;
  4794. $this->FontDescent = $font_descent / $this->k;
  4795. if ($out AND ($this->page > 0) AND (isset($this->CurrentFont['i']))) {
  4796. $this->_out(sprintf('BT /F%d %.2F Tf ET', $this->CurrentFont['i'], $this->FontSizePt));
  4797. }
  4798. }
  4799. /**
  4800. * Return the font descent value
  4801. * @param $font (string) font name
  4802. * @param $style (string) font style
  4803. * @param $size (float) The size (in points)
  4804. * @return int font descent
  4805. * @public
  4806. * @author Nicola Asuni
  4807. * @since 4.9.003 (2010-03-30)
  4808. */
  4809. public function getFontDescent($font, $style='', $size=0) {
  4810. $fontdata = $this->AddFont($font, $style);
  4811. $fontinfo = $this->getFontBuffer($fontdata['fontkey']);
  4812. if (isset($fontinfo['desc']['Descent']) AND ($fontinfo['desc']['Descent'] <= 0)) {
  4813. $descent = (- $fontinfo['desc']['Descent'] * $size / 1000);
  4814. } else {
  4815. $descent = 1.219 * 0.24 * $size;
  4816. }
  4817. return ($descent / $this->k);
  4818. }
  4819. /**
  4820. * Return the font ascent value
  4821. * @param $font (string) font name
  4822. * @param $style (string) font style
  4823. * @param $size (float) The size (in points)
  4824. * @return int font ascent
  4825. * @public
  4826. * @author Nicola Asuni
  4827. * @since 4.9.003 (2010-03-30)
  4828. */
  4829. public function getFontAscent($font, $style='', $size=0) {
  4830. $fontdata = $this->AddFont($font, $style);
  4831. $fontinfo = $this->getFontBuffer($fontdata['fontkey']);
  4832. if (isset($fontinfo['desc']['Ascent']) AND ($fontinfo['desc']['Ascent'] > 0)) {
  4833. $ascent = ($fontinfo['desc']['Ascent'] * $size / 1000);
  4834. } else {
  4835. $ascent = 1.219 * 0.76 * $size;
  4836. }
  4837. return ($ascent / $this->k);
  4838. }
  4839. /**
  4840. * Defines the default monospaced font.
  4841. * @param $font (string) Font name.
  4842. * @public
  4843. * @since 4.5.025
  4844. */
  4845. public function SetDefaultMonospacedFont($font) {
  4846. $this->default_monospaced_font = $font;
  4847. }
  4848. /**
  4849. * 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 />
  4850. * The identifier can then be passed to Cell(), Write(), Image() or Link(). The destination is defined with SetLink().
  4851. * @public
  4852. * @since 1.5
  4853. * @see Cell(), Write(), Image(), Link(), SetLink()
  4854. */
  4855. public function AddLink() {
  4856. //Create a new internal link
  4857. $n = count($this->links) + 1;
  4858. $this->links[$n] = array(0, 0);
  4859. return $n;
  4860. }
  4861. /**
  4862. * Defines the page and position a link points to.
  4863. * @param $link (int) The link identifier returned by AddLink()
  4864. * @param $y (float) Ordinate of target position; -1 indicates the current position. The default value is 0 (top of page)
  4865. * @param $page (int) Number of target page; -1 indicates the current page. This is the default value
  4866. * @public
  4867. * @since 1.5
  4868. * @see AddLink()
  4869. */
  4870. public function SetLink($link, $y=0, $page=-1) {
  4871. if ($y == -1) {
  4872. $y = $this->y;
  4873. }
  4874. if ($page == -1) {
  4875. $page = $this->page;
  4876. }
  4877. $this->links[$link] = array($page, $y);
  4878. }
  4879. /**
  4880. * Puts a link on a rectangular area of the page.
  4881. * 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.
  4882. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  4883. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  4884. * @param $w (float) Width of the rectangle
  4885. * @param $h (float) Height of the rectangle
  4886. * @param $link (mixed) URL or identifier returned by AddLink()
  4887. * @param $spaces (int) number of spaces on the text to link
  4888. * @public
  4889. * @since 1.5
  4890. * @see AddLink(), Annotation(), Cell(), Write(), Image()
  4891. */
  4892. public function Link($x, $y, $w, $h, $link, $spaces=0) {
  4893. $this->Annotation($x, $y, $w, $h, $link, array('Subtype'=>'Link'), $spaces);
  4894. }
  4895. /**
  4896. * Puts a markup annotation on a rectangular area of the page.
  4897. * !!!!THE ANNOTATION SUPPORT IS NOT YET FULLY IMPLEMENTED !!!!
  4898. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  4899. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  4900. * @param $w (float) Width of the rectangle
  4901. * @param $h (float) Height of the rectangle
  4902. * @param $text (string) annotation text or alternate content
  4903. * @param $opt (array) array of options (see section 8.4 of PDF reference 1.7).
  4904. * @param $spaces (int) number of spaces on the text to link
  4905. * @public
  4906. * @since 4.0.018 (2008-08-06)
  4907. */
  4908. public function Annotation($x, $y, $w, $h, $text, $opt=array('Subtype'=>'Text'), $spaces=0) {
  4909. if ($this->inxobj) {
  4910. // store parameters for later use on template
  4911. $this->xobjects[$this->xobjid]['annotations'][] = array('x' => $x, 'y' => $y, 'w' => $w, 'h' => $h, 'text' => $text, 'opt' => $opt, 'spaces' => $spaces);
  4912. return;
  4913. }
  4914. if ($x === '') {
  4915. $x = $this->x;
  4916. }
  4917. if ($y === '') {
  4918. $y = $this->y;
  4919. }
  4920. // check page for no-write regions and adapt page margins if necessary
  4921. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  4922. // recalculate coordinates to account for graphic transformations
  4923. if (isset($this->transfmatrix) AND !empty($this->transfmatrix)) {
  4924. for ($i=$this->transfmatrix_key; $i > 0; --$i) {
  4925. $maxid = count($this->transfmatrix[$i]) - 1;
  4926. for ($j=$maxid; $j >= 0; --$j) {
  4927. $ctm = $this->transfmatrix[$i][$j];
  4928. if (isset($ctm['a'])) {
  4929. $x = $x * $this->k;
  4930. $y = ($this->h - $y) * $this->k;
  4931. $w = $w * $this->k;
  4932. $h = $h * $this->k;
  4933. // top left
  4934. $xt = $x;
  4935. $yt = $y;
  4936. $x1 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  4937. $y1 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  4938. // top right
  4939. $xt = $x + $w;
  4940. $yt = $y;
  4941. $x2 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  4942. $y2 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  4943. // bottom left
  4944. $xt = $x;
  4945. $yt = $y - $h;
  4946. $x3 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  4947. $y3 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  4948. // bottom right
  4949. $xt = $x + $w;
  4950. $yt = $y - $h;
  4951. $x4 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  4952. $y4 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  4953. // new coordinates (rectangle area)
  4954. $x = min($x1, $x2, $x3, $x4);
  4955. $y = max($y1, $y2, $y3, $y4);
  4956. $w = (max($x1, $x2, $x3, $x4) - $x) / $this->k;
  4957. $h = ($y - min($y1, $y2, $y3, $y4)) / $this->k;
  4958. $x = $x / $this->k;
  4959. $y = $this->h - ($y / $this->k);
  4960. }
  4961. }
  4962. }
  4963. }
  4964. if ($this->page <= 0) {
  4965. $page = 1;
  4966. } else {
  4967. $page = $this->page;
  4968. }
  4969. if (!isset($this->PageAnnots[$page])) {
  4970. $this->PageAnnots[$page] = array();
  4971. }
  4972. ++$this->n;
  4973. $this->PageAnnots[$page][] = array('n' => $this->n, 'x' => $x, 'y' => $y, 'w' => $w, 'h' => $h, 'txt' => $text, 'opt' => $opt, 'numspaces' => $spaces);
  4974. 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'])]))) {
  4975. ++$this->n;
  4976. $this->embeddedfiles[basename($opt['FS'])] = array('n' => $this->n, 'file' => $opt['FS']);
  4977. }
  4978. // Add widgets annotation's icons
  4979. if (isset($opt['mk']['i']) AND file_exists($opt['mk']['i'])) {
  4980. $this->Image($opt['mk']['i'], '', '', 10, 10, '', '', '', false, 300, '', false, false, 0, false, true);
  4981. }
  4982. if (isset($opt['mk']['ri']) AND file_exists($opt['mk']['ri'])) {
  4983. $this->Image($opt['mk']['ri'], '', '', 0, 0, '', '', '', false, 300, '', false, false, 0, false, true);
  4984. }
  4985. if (isset($opt['mk']['ix']) AND file_exists($opt['mk']['ix'])) {
  4986. $this->Image($opt['mk']['ix'], '', '', 0, 0, '', '', '', false, 300, '', false, false, 0, false, true);
  4987. }
  4988. }
  4989. /**
  4990. * Embedd the attached files.
  4991. * @since 4.4.000 (2008-12-07)
  4992. * @protected
  4993. * @see Annotation()
  4994. */
  4995. protected function _putEmbeddedFiles() {
  4996. reset($this->embeddedfiles);
  4997. foreach ($this->embeddedfiles as $filename => $filedata) {
  4998. $data = file_get_contents($filedata['file']);
  4999. $filter = '';
  5000. if ($this->compress) {
  5001. $data = gzcompress($data);
  5002. $filter = ' /Filter /FlateDecode';
  5003. }
  5004. $stream = $this->_getrawstream($data, $filedata['n']);
  5005. $out = $this->_getobj($filedata['n'])."\n";
  5006. $out .= '<< /Type /EmbeddedFile'.$filter.' /Length '.strlen($stream).' >>';
  5007. $out .= ' stream'."\n".$stream."\n".'endstream';
  5008. $out .= "\n".'endobj';
  5009. $this->_out($out);
  5010. }
  5011. }
  5012. /**
  5013. * Prints a text cell at the specified position.
  5014. * This method allows to place a string precisely on the page.
  5015. * @param $x (float) Abscissa of the cell origin
  5016. * @param $y (float) Ordinate of the cell origin
  5017. * @param $txt (string) String to print
  5018. * @param $fstroke (int) outline size in user units (false = disable)
  5019. * @param $fclip (boolean) if true activate clipping mode (you must call StartTransform() before this function and StopTransform() to stop the clipping tranformation).
  5020. * @param $ffill (boolean) if true fills the text
  5021. * @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)))
  5022. * @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.
  5023. * @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>
  5024. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  5025. * @param $link (mixed) URL or identifier returned by AddLink().
  5026. * @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.
  5027. * @param $ignore_min_height (boolean) if true ignore automatic minimum height value.
  5028. * @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>
  5029. * @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>
  5030. * @param $rtloff (boolean) if true uses the page top-left corner as origin of axis for $x and $y initial position.
  5031. * @public
  5032. * @since 1.0
  5033. * @see Cell(), Write(), MultiCell(), WriteHTML(), WriteHTMLCell()
  5034. */
  5035. 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) {
  5036. $textrendermode = $this->textrendermode;
  5037. $textstrokewidth = $this->textstrokewidth;
  5038. $this->setTextRenderingMode($fstroke, $ffill, $fclip);
  5039. $this->SetXY($x, $y, $rtloff);
  5040. $this->Cell(0, 0, $txt, $border, $ln, $align, $fill, $link, $stretch, $ignore_min_height, $calign, $valign);
  5041. // restore previous rendering mode
  5042. $this->textrendermode = $textrendermode;
  5043. $this->textstrokewidth = $textstrokewidth;
  5044. }
  5045. /**
  5046. * Whenever a page break condition is met, the method is called, and the break is issued or not depending on the returned value.
  5047. * The default implementation returns a value according to the mode selected by SetAutoPageBreak().<br />
  5048. * This method is called automatically and should not be called directly by the application.
  5049. * @return boolean
  5050. * @public
  5051. * @since 1.4
  5052. * @see SetAutoPageBreak()
  5053. */
  5054. public function AcceptPageBreak() {
  5055. if ($this->num_columns > 1) {
  5056. // multi column mode
  5057. if ($this->current_column < ($this->num_columns - 1)) {
  5058. // go to next column
  5059. $this->selectColumn($this->current_column + 1);
  5060. } else {
  5061. // add a new page
  5062. $this->AddPage();
  5063. // set first column
  5064. $this->selectColumn(0);
  5065. }
  5066. // avoid page breaking from checkPageBreak()
  5067. return false;
  5068. }
  5069. return $this->AutoPageBreak;
  5070. }
  5071. /**
  5072. * Add page if needed.
  5073. * @param $h (float) Cell height. Default value: 0.
  5074. * @param $y (mixed) starting y position, leave empty for current position.
  5075. * @param $addpage (boolean) if true add a page, otherwise only return the true/false state
  5076. * @return boolean true in case of page break, false otherwise.
  5077. * @since 3.2.000 (2008-07-01)
  5078. * @protected
  5079. */
  5080. protected function checkPageBreak($h=0, $y='', $addpage=true) {
  5081. if ($this->empty_string($y)) {
  5082. $y = $this->y;
  5083. }
  5084. $current_page = $this->page;
  5085. if ((($y + $h) > $this->PageBreakTrigger) AND ($this->inPageBody()) AND ($this->AcceptPageBreak())) {
  5086. if ($addpage) {
  5087. //Automatic page break
  5088. $x = $this->x;
  5089. $this->AddPage($this->CurOrientation);
  5090. $this->y = $this->tMargin;
  5091. $oldpage = $this->page - 1;
  5092. if ($this->rtl) {
  5093. if ($this->pagedim[$this->page]['orm'] != $this->pagedim[$oldpage]['orm']) {
  5094. $this->x = $x - ($this->pagedim[$this->page]['orm'] - $this->pagedim[$oldpage]['orm']);
  5095. } else {
  5096. $this->x = $x;
  5097. }
  5098. } else {
  5099. if ($this->pagedim[$this->page]['olm'] != $this->pagedim[$oldpage]['olm']) {
  5100. $this->x = $x + ($this->pagedim[$this->page]['olm'] - $this->pagedim[$oldpage]['olm']);
  5101. } else {
  5102. $this->x = $x;
  5103. }
  5104. }
  5105. }
  5106. return true;
  5107. }
  5108. if ($current_page != $this->page) {
  5109. // account for columns mode
  5110. return true;
  5111. }
  5112. return false;
  5113. }
  5114. /**
  5115. * Removes SHY characters from text.
  5116. * Unicode Data:<ul>
  5117. * <li>Name : SOFT HYPHEN, commonly abbreviated as SHY</li>
  5118. * <li>HTML Entity (decimal): "&amp;#173;"</li>
  5119. * <li>HTML Entity (hex): "&amp;#xad;"</li>
  5120. * <li>HTML Entity (named): "&amp;shy;"</li>
  5121. * <li>How to type in Microsoft Windows: [Alt +00AD] or [Alt 0173]</li>
  5122. * <li>UTF-8 (hex): 0xC2 0xAD (c2ad)</li>
  5123. * <li>UTF-8 character: chr(194).chr(173)</li>
  5124. * </ul>
  5125. * @param $txt (string) input string
  5126. * @return string without SHY characters.
  5127. * @public
  5128. * @since (4.5.019) 2009-02-28
  5129. */
  5130. public function removeSHY($txt='') {
  5131. $txt = preg_replace('/([\\xc2]{1}[\\xad]{1})/', '', $txt);
  5132. if (!$this->isunicode) {
  5133. $txt = preg_replace('/([\\xad]{1})/', '', $txt);
  5134. }
  5135. return $txt;
  5136. }
  5137. /**
  5138. * 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 />
  5139. * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
  5140. * @param $w (float) Cell width. If 0, the cell extends up to the right margin.
  5141. * @param $h (float) Cell height. Default value: 0.
  5142. * @param $txt (string) String to print. Default value: empty string.
  5143. * @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)))
  5144. * @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.
  5145. * @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>
  5146. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  5147. * @param $link (mixed) URL or identifier returned by AddLink().
  5148. * @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.
  5149. * @param $ignore_min_height (boolean) if true ignore automatic minimum height value.
  5150. * @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>
  5151. * @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>
  5152. * @public
  5153. * @since 1.0
  5154. * @see SetFont(), SetDrawColor(), SetFillColor(), SetTextColor(), SetLineWidth(), AddLink(), Ln(), MultiCell(), Write(), SetAutoPageBreak()
  5155. */
  5156. 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') {
  5157. $prev_cell_margin = $this->cell_margin;
  5158. $prev_cell_padding = $this->cell_padding;
  5159. $this->adjustCellPadding($border);
  5160. if (!$ignore_min_height) {
  5161. $min_cell_height = ($this->FontSize * $this->cell_height_ratio) + $this->cell_padding['T'] + $this->cell_padding['B'];
  5162. if ($h < $min_cell_height) {
  5163. $h = $min_cell_height;
  5164. }
  5165. }
  5166. $this->checkPageBreak($h + $this->cell_margin['T'] + $this->cell_margin['B']);
  5167. $this->_out($this->getCellCode($w, $h, $txt, $border, $ln, $align, $fill, $link, $stretch, true, $calign, $valign));
  5168. $this->cell_padding = $prev_cell_padding;
  5169. $this->cell_margin = $prev_cell_margin;
  5170. }
  5171. /**
  5172. * 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 />
  5173. * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
  5174. * @param $w (float) Cell width. If 0, the cell extends up to the right margin.
  5175. * @param $h (float) Cell height. Default value: 0.
  5176. * @param $txt (string) String to print. Default value: empty string.
  5177. * @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)))
  5178. * @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.
  5179. * @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>
  5180. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  5181. * @param $link (mixed) URL or identifier returned by AddLink().
  5182. * @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.
  5183. * @param $ignore_min_height (boolean) if true ignore automatic minimum height value.
  5184. * @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>
  5185. * @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>
  5186. * @return string containing cell code
  5187. * @protected
  5188. * @since 1.0
  5189. * @see Cell()
  5190. */
  5191. 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') {
  5192. // replace 'NO-BREAK SPACE' (U+00A0) character with a simple space
  5193. $txt = str_replace($this->unichr(160), ' ', $txt);
  5194. $prev_cell_margin = $this->cell_margin;
  5195. $prev_cell_padding = $this->cell_padding;
  5196. $txt = $this->removeSHY($txt);
  5197. $rs = ''; //string to be returned
  5198. $this->adjustCellPadding($border);
  5199. if (!$ignore_min_height) {
  5200. $min_cell_height = ($this->FontSize * $this->cell_height_ratio) + $this->cell_padding['T'] + $this->cell_padding['B'];
  5201. if ($h < $min_cell_height) {
  5202. $h = $min_cell_height;
  5203. }
  5204. }
  5205. $k = $this->k;
  5206. // check page for no-write regions and adapt page margins if necessary
  5207. list($this->x, $this->y) = $this->checkPageRegions($h, $this->x, $this->y);
  5208. if ($this->rtl) {
  5209. $x = $this->x - $this->cell_margin['R'];
  5210. } else {
  5211. $x = $this->x + $this->cell_margin['L'];
  5212. }
  5213. $y = $this->y + $this->cell_margin['T'];
  5214. $prev_font_stretching = $this->font_stretching;
  5215. $prev_font_spacing = $this->font_spacing;
  5216. // cell vertical alignment
  5217. switch ($calign) {
  5218. case 'A': {
  5219. // font top
  5220. switch ($valign) {
  5221. case 'T': {
  5222. // top
  5223. $y -= $this->cell_padding['T'];
  5224. break;
  5225. }
  5226. case 'B': {
  5227. // bottom
  5228. $y -= ($h - $this->cell_padding['B'] - $this->FontAscent - $this->FontDescent);
  5229. break;
  5230. }
  5231. default:
  5232. case 'C':
  5233. case 'M': {
  5234. // center
  5235. $y -= (($h - $this->FontAscent - $this->FontDescent) / 2);
  5236. break;
  5237. }
  5238. }
  5239. break;
  5240. }
  5241. case 'L': {
  5242. // font baseline
  5243. switch ($valign) {
  5244. case 'T': {
  5245. // top
  5246. $y -= ($this->cell_padding['T'] + $this->FontAscent);
  5247. break;
  5248. }
  5249. case 'B': {
  5250. // bottom
  5251. $y -= ($h - $this->cell_padding['B'] - $this->FontDescent);
  5252. break;
  5253. }
  5254. default:
  5255. case 'C':
  5256. case 'M': {
  5257. // center
  5258. $y -= (($h + $this->FontAscent - $this->FontDescent) / 2);
  5259. break;
  5260. }
  5261. }
  5262. break;
  5263. }
  5264. case 'D': {
  5265. // font bottom
  5266. switch ($valign) {
  5267. case 'T': {
  5268. // top
  5269. $y -= ($this->cell_padding['T'] + $this->FontAscent + $this->FontDescent);
  5270. break;
  5271. }
  5272. case 'B': {
  5273. // bottom
  5274. $y -= ($h - $this->cell_padding['B']);
  5275. break;
  5276. }
  5277. default:
  5278. case 'C':
  5279. case 'M': {
  5280. // center
  5281. $y -= (($h + $this->FontAscent + $this->FontDescent) / 2);
  5282. break;
  5283. }
  5284. }
  5285. break;
  5286. }
  5287. case 'B': {
  5288. // cell bottom
  5289. $y -= $h;
  5290. break;
  5291. }
  5292. case 'C':
  5293. case 'M': {
  5294. // cell center
  5295. $y -= ($h / 2);
  5296. break;
  5297. }
  5298. default:
  5299. case 'T': {
  5300. // cell top
  5301. break;
  5302. }
  5303. }
  5304. // text vertical alignment
  5305. switch ($valign) {
  5306. case 'T': {
  5307. // top
  5308. $yt = $y + $this->cell_padding['T'];
  5309. break;
  5310. }
  5311. case 'B': {
  5312. // bottom
  5313. $yt = $y + $h - $this->cell_padding['B'] - $this->FontAscent - $this->FontDescent;
  5314. break;
  5315. }
  5316. default:
  5317. case 'C':
  5318. case 'M': {
  5319. // center
  5320. $yt = $y + (($h - $this->FontAscent - $this->FontDescent) / 2);
  5321. break;
  5322. }
  5323. }
  5324. $basefonty = $yt + $this->FontAscent;
  5325. if ($this->empty_string($w) OR ($w <= 0)) {
  5326. if ($this->rtl) {
  5327. $w = $x - $this->lMargin;
  5328. } else {
  5329. $w = $this->w - $this->rMargin - $x;
  5330. }
  5331. }
  5332. $s = '';
  5333. // fill and borders
  5334. if (is_string($border) AND (strlen($border) == 4)) {
  5335. // full border
  5336. $border = 1;
  5337. }
  5338. if ($fill OR ($border == 1)) {
  5339. if ($fill) {
  5340. $op = ($border == 1) ? 'B' : 'f';
  5341. } else {
  5342. $op = 'S';
  5343. }
  5344. if ($this->rtl) {
  5345. $xk = (($x - $w) * $k);
  5346. } else {
  5347. $xk = ($x * $k);
  5348. }
  5349. $s .= sprintf('%.2F %.2F %.2F %.2F re %s ', $xk, (($this->h - $y) * $k), ($w * $k), (-$h * $k), $op);
  5350. }
  5351. // draw borders
  5352. $s .= $this->getCellBorder($x, $y, $w, $h, $border);
  5353. if ($txt != '') {
  5354. $txt2 = $txt;
  5355. if ($this->isunicode) {
  5356. if (($this->CurrentFont['type'] == 'core') OR ($this->CurrentFont['type'] == 'TrueType') OR ($this->CurrentFont['type'] == 'Type1')) {
  5357. $txt2 = $this->UTF8ToLatin1($txt2);
  5358. } else {
  5359. $unicode = $this->UTF8StringToArray($txt); // array of UTF-8 unicode values
  5360. $unicode = $this->utf8Bidi($unicode, '', $this->tmprtl);
  5361. if (defined('K_THAI_TOPCHARS') AND (K_THAI_TOPCHARS == true)) {
  5362. // ---- Fix for bug #2977340 "Incorrect Thai characters position arrangement" ----
  5363. // NOTE: this doesn't work with HTML justification
  5364. // Symbols that could overlap on the font top (only works in LTR)
  5365. $topchar = array(3611, 3613, 3615, 3650, 3651, 3652); // chars that extends on top
  5366. $topsym = array(3633, 3636, 3637, 3638, 3639, 3655, 3656, 3657, 3658, 3659, 3660, 3661, 3662); // symbols with top position
  5367. $numchars = count($unicode); // number of chars
  5368. $unik = 0;
  5369. $uniblock = array();
  5370. $uniblock[$unik] = array();
  5371. $uniblock[$unik][] = $unicode[0];
  5372. // resolve overlapping conflicts by splitting the string in several parts
  5373. for ($i = 1; $i < $numchars; ++$i) {
  5374. // check if symbols overlaps at top
  5375. if (in_array($unicode[$i], $topsym) AND (in_array($unicode[($i - 1)], $topsym) OR in_array($unicode[($i - 1)], $topchar))) {
  5376. // move symbols to another array
  5377. ++$unik;
  5378. $uniblock[$unik] = array();
  5379. $uniblock[$unik][] = $unicode[$i];
  5380. ++$unik;
  5381. $uniblock[$unik] = array();
  5382. $unicode[$i] = 0x200b; // Unicode Character 'ZERO WIDTH SPACE' (DEC:8203, U+200B)
  5383. } else {
  5384. $uniblock[$unik][] = $unicode[$i];
  5385. }
  5386. }
  5387. // ---- END OF Fix for bug #2977340
  5388. }
  5389. $txt2 = $this->arrUTF8ToUTF16BE($unicode, false);
  5390. }
  5391. }
  5392. $txt2 = $this->_escape($txt2);
  5393. // get current text width (considering general font stretching and spacing)
  5394. $txwidth = $this->GetStringWidth($txt);
  5395. $width = $txwidth;
  5396. // check for stretch mode
  5397. if ($stretch > 0) {
  5398. // calculate ratio between cell width and text width
  5399. if ($width <= 0) {
  5400. $ratio = 1;
  5401. } else {
  5402. $ratio = (($w - $this->cell_padding['L'] - $this->cell_padding['R']) / $width);
  5403. }
  5404. // check if stretching is required
  5405. if (($ratio < 1) OR (($ratio > 1) AND (($stretch % 2) == 0))) {
  5406. // the text will be stretched to fit cell width
  5407. if ($stretch > 2) {
  5408. // set new character spacing
  5409. $this->font_spacing += ($w - $this->cell_padding['L'] - $this->cell_padding['R'] - $width) / (max(($this->GetNumChars($txt) - 1), 1) * ($this->font_stretching / 100));
  5410. } else {
  5411. // set new horizontal stretching
  5412. $this->font_stretching *= $ratio;
  5413. }
  5414. // recalculate text width (the text fills the entire cell)
  5415. $width = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  5416. // reset alignment
  5417. $align = '';
  5418. }
  5419. }
  5420. if ($this->font_stretching != 100) {
  5421. // apply font stretching
  5422. $rs .= sprintf('BT %.2F Tz ET ', $this->font_stretching);
  5423. }
  5424. if ($this->font_spacing != 0) {
  5425. // increase/decrease font spacing
  5426. $rs .= sprintf('BT %.2F Tc ET ', ($this->font_spacing * $this->k));
  5427. }
  5428. if ($this->ColorFlag AND ($this->textrendermode < 4)) {
  5429. $s .= 'q '.$this->TextColor.' ';
  5430. }
  5431. // rendering mode
  5432. $s .= sprintf('BT %d Tr %.2F w ET ', $this->textrendermode, $this->textstrokewidth);
  5433. // count number of spaces
  5434. $ns = substr_count($txt, chr(32));
  5435. // Justification
  5436. $spacewidth = 0;
  5437. if (($align == 'J') AND ($ns > 0)) {
  5438. if ($this->isUnicodeFont()) {
  5439. // get string width without spaces
  5440. $width = $this->GetStringWidth(str_replace(' ', '', $txt));
  5441. // calculate average space width
  5442. $spacewidth = -1000 * ($w - $width - $this->cell_padding['L'] - $this->cell_padding['R']) / ($ns?$ns:1) / $this->FontSize;
  5443. if ($this->font_stretching != 100) {
  5444. // word spacing is affected by stretching
  5445. $spacewidth /= ($this->font_stretching / 100);
  5446. }
  5447. // set word position to be used with TJ operator
  5448. $txt2 = str_replace(chr(0).chr(32), ') '.sprintf('%.3F', $spacewidth).' (', $txt2);
  5449. $unicode_justification = true;
  5450. } else {
  5451. // get string width
  5452. $width = $txwidth;
  5453. // new space width
  5454. $spacewidth = (($w - $width - $this->cell_padding['L'] - $this->cell_padding['R']) / ($ns?$ns:1)) * $this->k;
  5455. if ($this->font_stretching != 100) {
  5456. // word spacing (Tw) is affected by stretching
  5457. $spacewidth /= ($this->font_stretching / 100);
  5458. }
  5459. // set word spacing
  5460. $rs .= sprintf('BT %.3F Tw ET ', $spacewidth);
  5461. }
  5462. $width = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  5463. }
  5464. // replace carriage return characters
  5465. $txt2 = str_replace("\r", ' ', $txt2);
  5466. switch ($align) {
  5467. case 'C': {
  5468. $dx = ($w - $width) / 2;
  5469. break;
  5470. }
  5471. case 'R': {
  5472. if ($this->rtl) {
  5473. $dx = $this->cell_padding['R'];
  5474. } else {
  5475. $dx = $w - $width - $this->cell_padding['R'];
  5476. }
  5477. break;
  5478. }
  5479. case 'L': {
  5480. if ($this->rtl) {
  5481. $dx = $w - $width - $this->cell_padding['L'];
  5482. } else {
  5483. $dx = $this->cell_padding['L'];
  5484. }
  5485. break;
  5486. }
  5487. case 'J':
  5488. default: {
  5489. if ($this->rtl) {
  5490. $dx = $this->cell_padding['R'];
  5491. } else {
  5492. $dx = $this->cell_padding['L'];
  5493. }
  5494. break;
  5495. }
  5496. }
  5497. if ($this->rtl) {
  5498. $xdx = $x - $dx - $width;
  5499. } else {
  5500. $xdx = $x + $dx;
  5501. }
  5502. $xdk = $xdx * $k;
  5503. // print text
  5504. $s .= sprintf('BT %.2F %.2F Td [(%s)] TJ ET', $xdk, (($this->h - $basefonty) * $k), $txt2);
  5505. if (isset($uniblock)) {
  5506. // print overlapping characters as separate string
  5507. $xshift = 0; // horizontal shift
  5508. $ty = (($this->h - $basefonty + (0.2 * $this->FontSize)) * $k);
  5509. $spw = (($w - $txwidth - $this->cell_padding['L'] - $this->cell_padding['R']) / ($ns?$ns:1));
  5510. foreach ($uniblock as $uk => $uniarr) {
  5511. if (($uk % 2) == 0) {
  5512. // x space to skip
  5513. if ($spacewidth != 0) {
  5514. // justification shift
  5515. $xshift += (count(array_keys($uniarr, 32)) * $spw);
  5516. }
  5517. $xshift += $this->GetArrStringWidth($uniarr); // + shift justification
  5518. } else {
  5519. // character to print
  5520. $topchr = $this->arrUTF8ToUTF16BE($uniarr, false);
  5521. $topchr = $this->_escape($topchr);
  5522. $s .= sprintf(' BT %.2F %.2F Td [(%s)] TJ ET', ($xdk + ($xshift * $k)), $ty, $topchr);
  5523. }
  5524. }
  5525. }
  5526. if ($this->underline) {
  5527. $s .= ' '.$this->_dounderlinew($xdx, $basefonty, $width);
  5528. }
  5529. if ($this->linethrough) {
  5530. $s .= ' '.$this->_dolinethroughw($xdx, $basefonty, $width);
  5531. }
  5532. if ($this->overline) {
  5533. $s .= ' '.$this->_dooverlinew($xdx, $basefonty, $width);
  5534. }
  5535. if ($this->ColorFlag AND ($this->textrendermode < 4)) {
  5536. $s .= ' Q';
  5537. }
  5538. if ($link) {
  5539. $this->Link($xdx, $yt, $width, ($this->FontAscent + $this->FontDescent), $link, $ns);
  5540. }
  5541. }
  5542. // output cell
  5543. if ($s) {
  5544. // output cell
  5545. $rs .= $s;
  5546. if ($this->font_spacing != 0) {
  5547. // reset font spacing mode
  5548. $rs .= ' BT 0 Tc ET';
  5549. }
  5550. if ($this->font_stretching != 100) {
  5551. // reset font stretching mode
  5552. $rs .= ' BT 100 Tz ET';
  5553. }
  5554. }
  5555. // reset word spacing
  5556. if (!$this->isUnicodeFont() AND ($align == 'J')) {
  5557. $rs .= ' BT 0 Tw ET';
  5558. }
  5559. // reset stretching and spacing
  5560. $this->font_stretching = $prev_font_stretching;
  5561. $this->font_spacing = $prev_font_spacing;
  5562. $this->lasth = $h;
  5563. if ($ln > 0) {
  5564. //Go to the beginning of the next line
  5565. $this->y = $y + $h + $this->cell_margin['B'];
  5566. if ($ln == 1) {
  5567. if ($this->rtl) {
  5568. $this->x = $this->w - $this->rMargin;
  5569. } else {
  5570. $this->x = $this->lMargin;
  5571. }
  5572. }
  5573. } else {
  5574. // go left or right by case
  5575. if ($this->rtl) {
  5576. $this->x = $x - $w - $this->cell_margin['L'];
  5577. } else {
  5578. $this->x = $x + $w + $this->cell_margin['R'];
  5579. }
  5580. }
  5581. $gstyles = ''.$this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor.' '.$this->FillColor."\n";
  5582. $rs = $gstyles.$rs;
  5583. $this->cell_padding = $prev_cell_padding;
  5584. $this->cell_margin = $prev_cell_margin;
  5585. return $rs;
  5586. }
  5587. /**
  5588. * Returns the code to draw the cell border
  5589. * @param $x (float) X coordinate.
  5590. * @param $y (float) Y coordinate.
  5591. * @param $w (float) Cell width.
  5592. * @param $h (float) Cell height.
  5593. * @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)))
  5594. * @return string containing cell border code
  5595. * @protected
  5596. * @see SetLineStyle()
  5597. * @since 5.7.000 (2010-08-02)
  5598. */
  5599. protected function getCellBorder($x, $y, $w, $h, $brd) {
  5600. $s = ''; // string to be returned
  5601. if (empty($brd)) {
  5602. return $s;
  5603. }
  5604. if ($brd == 1) {
  5605. $brd = array('LRTB' => true);
  5606. }
  5607. // calculate coordinates for border
  5608. $k = $this->k;
  5609. if ($this->rtl) {
  5610. $xeL = ($x - $w) * $k;
  5611. $xeR = $x * $k;
  5612. } else {
  5613. $xeL = $x * $k;
  5614. $xeR = ($x + $w) * $k;
  5615. }
  5616. $yeL = (($this->h - ($y + $h)) * $k);
  5617. $yeT = (($this->h - $y) * $k);
  5618. $xeT = $xeL;
  5619. $xeB = $xeR;
  5620. $yeR = $yeT;
  5621. $yeB = $yeL;
  5622. if (is_string($brd)) {
  5623. // convert string to array
  5624. $slen = strlen($brd);
  5625. $newbrd = array();
  5626. for ($i = 0; $i < $slen; ++$i) {
  5627. $newbrd[$brd{$i}] = array('cap' => 'square', 'join' => 'miter');
  5628. }
  5629. $brd = $newbrd;
  5630. }
  5631. if (isset($brd['mode'])) {
  5632. $mode = $brd['mode'];
  5633. unset($brd['mode']);
  5634. } else {
  5635. $mode = 'normal';
  5636. }
  5637. foreach ($brd as $border => $style) {
  5638. if (is_array($style) AND !empty($style)) {
  5639. // apply border style
  5640. $prev_style = $this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor.' ';
  5641. $s .= $this->SetLineStyle($style, true)."\n";
  5642. }
  5643. switch ($mode) {
  5644. case 'ext': {
  5645. $off = (($this->LineWidth / 2) * $k);
  5646. $xL = $xeL - $off;
  5647. $xR = $xeR + $off;
  5648. $yT = $yeT + $off;
  5649. $yL = $yeL - $off;
  5650. $xT = $xL;
  5651. $xB = $xR;
  5652. $yR = $yT;
  5653. $yB = $yL;
  5654. $w += $this->LineWidth;
  5655. $h += $this->LineWidth;
  5656. break;
  5657. }
  5658. case 'int': {
  5659. $off = ($this->LineWidth / 2) * $k;
  5660. $xL = $xeL + $off;
  5661. $xR = $xeR - $off;
  5662. $yT = $yeT - $off;
  5663. $yL = $yeL + $off;
  5664. $xT = $xL;
  5665. $xB = $xR;
  5666. $yR = $yT;
  5667. $yB = $yL;
  5668. $w -= $this->LineWidth;
  5669. $h -= $this->LineWidth;
  5670. break;
  5671. }
  5672. case 'normal':
  5673. default: {
  5674. $xL = $xeL;
  5675. $xT = $xeT;
  5676. $xB = $xeB;
  5677. $xR = $xeR;
  5678. $yL = $yeL;
  5679. $yT = $yeT;
  5680. $yB = $yeB;
  5681. $yR = $yeR;
  5682. break;
  5683. }
  5684. }
  5685. // draw borders by case
  5686. if (strlen($border) == 4) {
  5687. $s .= sprintf('%.2F %.2F %.2F %.2F re S ', $xT, $yT, ($w * $k), (-$h * $k));
  5688. } elseif (strlen($border) == 3) {
  5689. if (strpos($border,'B') === false) { // LTR
  5690. $s .= sprintf('%.2F %.2F m ', $xL, $yL);
  5691. $s .= sprintf('%.2F %.2F l ', $xT, $yT);
  5692. $s .= sprintf('%.2F %.2F l ', $xR, $yR);
  5693. $s .= sprintf('%.2F %.2F l ', $xB, $yB);
  5694. $s .= 'S ';
  5695. } elseif (strpos($border,'L') === false) { // TRB
  5696. $s .= sprintf('%.2F %.2F m ', $xT, $yT);
  5697. $s .= sprintf('%.2F %.2F l ', $xR, $yR);
  5698. $s .= sprintf('%.2F %.2F l ', $xB, $yB);
  5699. $s .= sprintf('%.2F %.2F l ', $xL, $yL);
  5700. $s .= 'S ';
  5701. } elseif (strpos($border,'T') === false) { // RBL
  5702. $s .= sprintf('%.2F %.2F m ', $xR, $yR);
  5703. $s .= sprintf('%.2F %.2F l ', $xB, $yB);
  5704. $s .= sprintf('%.2F %.2F l ', $xL, $yL);
  5705. $s .= sprintf('%.2F %.2F l ', $xT, $yT);
  5706. $s .= 'S ';
  5707. } elseif (strpos($border,'R') === false) { // BLT
  5708. $s .= sprintf('%.2F %.2F m ', $xB, $yB);
  5709. $s .= sprintf('%.2F %.2F l ', $xL, $yL);
  5710. $s .= sprintf('%.2F %.2F l ', $xT, $yT);
  5711. $s .= sprintf('%.2F %.2F l ', $xR, $yR);
  5712. $s .= 'S ';
  5713. }
  5714. } elseif (strlen($border) == 2) {
  5715. if ((strpos($border,'L') !== false) AND (strpos($border,'T') !== false)) { // LT
  5716. $s .= sprintf('%.2F %.2F m ', $xL, $yL);
  5717. $s .= sprintf('%.2F %.2F l ', $xT, $yT);
  5718. $s .= sprintf('%.2F %.2F l ', $xR, $yR);
  5719. $s .= 'S ';
  5720. } elseif ((strpos($border,'T') !== false) AND (strpos($border,'R') !== false)) { // TR
  5721. $s .= sprintf('%.2F %.2F m ', $xT, $yT);
  5722. $s .= sprintf('%.2F %.2F l ', $xR, $yR);
  5723. $s .= sprintf('%.2F %.2F l ', $xB, $yB);
  5724. $s .= 'S ';
  5725. } elseif ((strpos($border,'R') !== false) AND (strpos($border,'B') !== false)) { // RB
  5726. $s .= sprintf('%.2F %.2F m ', $xR, $yR);
  5727. $s .= sprintf('%.2F %.2F l ', $xB, $yB);
  5728. $s .= sprintf('%.2F %.2F l ', $xL, $yL);
  5729. $s .= 'S ';
  5730. } elseif ((strpos($border,'B') !== false) AND (strpos($border,'L') !== false)) { // BL
  5731. $s .= sprintf('%.2F %.2F m ', $xB, $yB);
  5732. $s .= sprintf('%.2F %.2F l ', $xL, $yL);
  5733. $s .= sprintf('%.2F %.2F l ', $xT, $yT);
  5734. $s .= 'S ';
  5735. } elseif ((strpos($border,'L') !== false) AND (strpos($border,'R') !== false)) { // LR
  5736. $s .= sprintf('%.2F %.2F m ', $xL, $yL);
  5737. $s .= sprintf('%.2F %.2F l ', $xT, $yT);
  5738. $s .= 'S ';
  5739. $s .= sprintf('%.2F %.2F m ', $xR, $yR);
  5740. $s .= sprintf('%.2F %.2F l ', $xB, $yB);
  5741. $s .= 'S ';
  5742. } elseif ((strpos($border,'T') !== false) AND (strpos($border,'B') !== false)) { // TB
  5743. $s .= sprintf('%.2F %.2F m ', $xT, $yT);
  5744. $s .= sprintf('%.2F %.2F l ', $xR, $yR);
  5745. $s .= 'S ';
  5746. $s .= sprintf('%.2F %.2F m ', $xB, $yB);
  5747. $s .= sprintf('%.2F %.2F l ', $xL, $yL);
  5748. $s .= 'S ';
  5749. }
  5750. } else { // strlen($border) == 1
  5751. if (strpos($border,'L') !== false) { // L
  5752. $s .= sprintf('%.2F %.2F m ', $xL, $yL);
  5753. $s .= sprintf('%.2F %.2F l ', $xT, $yT);
  5754. $s .= 'S ';
  5755. } elseif (strpos($border,'T') !== false) { // T
  5756. $s .= sprintf('%.2F %.2F m ', $xT, $yT);
  5757. $s .= sprintf('%.2F %.2F l ', $xR, $yR);
  5758. $s .= 'S ';
  5759. } elseif (strpos($border,'R') !== false) { // R
  5760. $s .= sprintf('%.2F %.2F m ', $xR, $yR);
  5761. $s .= sprintf('%.2F %.2F l ', $xB, $yB);
  5762. $s .= 'S ';
  5763. } elseif (strpos($border,'B') !== false) { // B
  5764. $s .= sprintf('%.2F %.2F m ', $xB, $yB);
  5765. $s .= sprintf('%.2F %.2F l ', $xL, $yL);
  5766. $s .= 'S ';
  5767. }
  5768. }
  5769. if (is_array($style) AND !empty($style)) {
  5770. // reset border style to previous value
  5771. $s .= "\n".$this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor."\n";
  5772. }
  5773. }
  5774. return $s;
  5775. }
  5776. /**
  5777. * This method allows printing text with line breaks.
  5778. * 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 />
  5779. * Text can be aligned, centered or justified. The cell block can be framed and the background painted.
  5780. * @param $w (float) Width of cells. If 0, they extend up to the right margin of the page.
  5781. * @param $h (float) Cell minimum height. The cell extends automatically if needed.
  5782. * @param $txt (string) String to print
  5783. * @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)))
  5784. * @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>
  5785. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  5786. * @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>
  5787. * @param $x (float) x position in user units
  5788. * @param $y (float) y position in user units
  5789. * @param $reseth (boolean) if true reset the last cell height (default true).
  5790. * @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.
  5791. * @param $ishtml (boolean) set to true if $txt is HTML content (default = false).
  5792. * @param $autopadding (boolean) if true, uses internal padding and automatically adjust it to account for line width.
  5793. * @param $maxh (float) maximum height. It should be >= $h and less then remaining space to the bottom of the page, or 0 for disable this feature. This feature works only when $ishtml=false.
  5794. * @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.
  5795. * @param $fitcell (boolean) if true attempt to fit all the text within the cell by reducing the font size.
  5796. * @return int Return the number of cells or 1 for html mode.
  5797. * @public
  5798. * @since 1.3
  5799. * @see SetFont(), SetDrawColor(), SetFillColor(), SetTextColor(), SetLineWidth(), Cell(), Write(), SetAutoPageBreak()
  5800. */
  5801. 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) {
  5802. $prev_cell_margin = $this->cell_margin;
  5803. $prev_cell_padding = $this->cell_padding;
  5804. // adjust internal padding
  5805. $this->adjustCellPadding($border);
  5806. $mc_padding = $this->cell_padding;
  5807. $mc_margin = $this->cell_margin;
  5808. $this->cell_padding['T'] = 0;
  5809. $this->cell_padding['B'] = 0;
  5810. $this->setCellMargins(0, 0, 0, 0);
  5811. if ($this->empty_string($this->lasth) OR $reseth) {
  5812. // reset row height
  5813. $this->resetLastH();
  5814. }
  5815. if (!$this->empty_string($y)) {
  5816. $this->SetY($y);
  5817. } else {
  5818. $y = $this->GetY();
  5819. }
  5820. $resth = 0;
  5821. if (($h > 0) AND $this->inPageBody() AND (($y + $h + $mc_margin['T'] + $mc_margin['B']) > $this->PageBreakTrigger)) {
  5822. // spit cell in more pages/columns
  5823. $newh = ($this->PageBreakTrigger - $y);
  5824. $resth = ($h - $newh); // cell to be printed on the next page/column
  5825. $h = $newh;
  5826. }
  5827. // get current page number
  5828. $startpage = $this->page;
  5829. // get current column
  5830. $startcolumn = $this->current_column;
  5831. if (!$this->empty_string($x)) {
  5832. $this->SetX($x);
  5833. } else {
  5834. $x = $this->GetX();
  5835. }
  5836. // check page for no-write regions and adapt page margins if necessary
  5837. list($x, $y) = $this->checkPageRegions(0, $x, $y);
  5838. // apply margins
  5839. $oy = $y + $mc_margin['T'];
  5840. if ($this->rtl) {
  5841. $ox = $this->w - $x - $mc_margin['R'];
  5842. } else {
  5843. $ox = $x + $mc_margin['L'];
  5844. }
  5845. $this->x = $ox;
  5846. $this->y = $oy;
  5847. // set width
  5848. if ($this->empty_string($w) OR ($w <= 0)) {
  5849. if ($this->rtl) {
  5850. $w = $this->x - $this->lMargin - $mc_margin['L'];
  5851. } else {
  5852. $w = $this->w - $this->x - $this->rMargin - $mc_margin['R'];
  5853. }
  5854. }
  5855. // store original margin values
  5856. $lMargin = $this->lMargin;
  5857. $rMargin = $this->rMargin;
  5858. if ($this->rtl) {
  5859. $this->rMargin = $this->w - $this->x;
  5860. $this->lMargin = $this->x - $w;
  5861. } else {
  5862. $this->lMargin = $this->x;
  5863. $this->rMargin = $this->w - $this->x - $w;
  5864. }
  5865. if ($autopadding) {
  5866. // add top padding
  5867. $this->y += $mc_padding['T'];
  5868. }
  5869. if ($ishtml) { // ******* Write HTML text
  5870. $this->writeHTML($txt, true, false, $reseth, true, $align);
  5871. $nl = 1;
  5872. } else { // ******* Write simple text
  5873. $prev_FontSizePt = $this->FontSizePt;
  5874. // vertical alignment
  5875. if ($maxh > 0) {
  5876. // get text height
  5877. $text_height = $this->getStringHeight($w, $txt, $reseth, $autopadding, $mc_padding, $border);
  5878. if ($fitcell) {
  5879. // try to reduce font size to fit text on cell (use a quick search algorithm)
  5880. $fmin = 1;
  5881. $fmax = $this->FontSizePt;
  5882. $prev_text_height = $text_height;
  5883. $maxit = 100; // max number of iterations
  5884. while ($maxit > 0) {
  5885. $fmid = (($fmax + $fmin) / 2);
  5886. $this->SetFontSize($fmid, false);
  5887. $this->resetLastH();
  5888. $text_height = $this->getStringHeight($w, $txt, $reseth, $autopadding, $mc_padding, $border);
  5889. if (($text_height == $maxh) OR (($text_height < $maxh) AND ($fmin >= ($fmax - 0.01)))) {
  5890. break;
  5891. } elseif ($text_height < $maxh) {
  5892. $fmin = $fmid;
  5893. } else {
  5894. $fmax = $fmid;
  5895. }
  5896. --$maxit;
  5897. }
  5898. $this->SetFontSize($this->FontSizePt);
  5899. }
  5900. if ($text_height < $maxh) {
  5901. if ($valign == 'M') {
  5902. // text vertically centered
  5903. $this->y += (($maxh - $text_height) / 2);
  5904. } elseif ($valign == 'B') {
  5905. // text vertically aligned on bottom
  5906. $this->y += ($maxh - $text_height);
  5907. }
  5908. }
  5909. }
  5910. $nl = $this->Write($this->lasth, $txt, '', 0, $align, true, $stretch, false, true, $maxh, 0, $mc_margin);
  5911. if ($fitcell) {
  5912. // restore font size
  5913. $this->SetFontSize($prev_FontSizePt);
  5914. }
  5915. }
  5916. if ($autopadding) {
  5917. // add bottom padding
  5918. $this->y += $mc_padding['B'];
  5919. }
  5920. // Get end-of-text Y position
  5921. $currentY = $this->y;
  5922. // get latest page number
  5923. $endpage = $this->page;
  5924. if ($resth > 0) {
  5925. $skip = ($endpage - $startpage);
  5926. $tmpresth = $resth;
  5927. while ($tmpresth > 0) {
  5928. if ($skip <= 0) {
  5929. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  5930. $this->checkPageBreak($this->PageBreakTrigger + 1);
  5931. }
  5932. if ($this->num_columns > 1) {
  5933. $tmpresth -= ($this->h - $this->y - $this->bMargin);
  5934. } else {
  5935. $tmpresth -= ($this->h - $this->tMargin - $this->bMargin);
  5936. }
  5937. --$skip;
  5938. }
  5939. $currentY = $this->y;
  5940. $endpage = $this->page;
  5941. }
  5942. // get latest column
  5943. $endcolumn = $this->current_column;
  5944. if ($this->num_columns == 0) {
  5945. $this->num_columns = 1;
  5946. }
  5947. // get border modes
  5948. $border_start = $this->getBorderMode($border, $position='start');
  5949. $border_end = $this->getBorderMode($border, $position='end');
  5950. $border_middle = $this->getBorderMode($border, $position='middle');
  5951. // design borders around HTML cells.
  5952. for ($page = $startpage; $page <= $endpage; ++$page) { // for each page
  5953. $ccode = '';
  5954. $this->setPage($page);
  5955. if ($this->num_columns < 2) {
  5956. // single-column mode
  5957. $this->SetX($x);
  5958. $this->y = $this->tMargin;
  5959. }
  5960. // account for margin changes
  5961. if ($page > $startpage) {
  5962. if (($this->rtl) AND ($this->pagedim[$page]['orm'] != $this->pagedim[$startpage]['orm'])) {
  5963. $this->x -= ($this->pagedim[$page]['orm'] - $this->pagedim[$startpage]['orm']);
  5964. } elseif ((!$this->rtl) AND ($this->pagedim[$page]['olm'] != $this->pagedim[$startpage]['olm'])) {
  5965. $this->x += ($this->pagedim[$page]['olm'] - $this->pagedim[$startpage]['olm']);
  5966. }
  5967. }
  5968. if ($startpage == $endpage) {
  5969. // single page
  5970. for ($column = $startcolumn; $column <= $endcolumn; ++$column) { // for each column
  5971. $this->selectColumn($column);
  5972. if ($this->rtl) {
  5973. $this->x -= $mc_margin['R'];
  5974. } else {
  5975. $this->x += $mc_margin['L'];
  5976. }
  5977. if ($startcolumn == $endcolumn) { // single column
  5978. $cborder = $border;
  5979. $h = max($h, ($currentY - $oy));
  5980. $this->y = $oy;
  5981. } elseif ($column == $startcolumn) { // first column
  5982. $cborder = $border_start;
  5983. $this->y = $oy;
  5984. $h = $this->h - $this->y - $this->bMargin;
  5985. } elseif ($column == $endcolumn) { // end column
  5986. $cborder = $border_end;
  5987. $h = $currentY - $this->y;
  5988. if ($resth > $h) {
  5989. $h = $resth;
  5990. }
  5991. } else { // middle column
  5992. $cborder = $border_middle;
  5993. $h = $this->h - $this->y - $this->bMargin;
  5994. $resth -= $h;
  5995. }
  5996. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  5997. } // end for each column
  5998. } elseif ($page == $startpage) { // first page
  5999. for ($column = $startcolumn; $column < $this->num_columns; ++$column) { // for each column
  6000. $this->selectColumn($column);
  6001. if ($this->rtl) {
  6002. $this->x -= $mc_margin['R'];
  6003. } else {
  6004. $this->x += $mc_margin['L'];
  6005. }
  6006. if ($column == $startcolumn) { // first column
  6007. $cborder = $border_start;
  6008. $this->y = $oy;
  6009. $h = $this->h - $this->y - $this->bMargin;
  6010. } else { // middle column
  6011. $cborder = $border_middle;
  6012. $h = $this->h - $this->y - $this->bMargin;
  6013. $resth -= $h;
  6014. }
  6015. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  6016. } // end for each column
  6017. } elseif ($page == $endpage) { // last page
  6018. for ($column = 0; $column <= $endcolumn; ++$column) { // for each column
  6019. $this->selectColumn($column);
  6020. if ($this->rtl) {
  6021. $this->x -= $mc_margin['R'];
  6022. } else {
  6023. $this->x += $mc_margin['L'];
  6024. }
  6025. if ($column == $endcolumn) {
  6026. // end column
  6027. $cborder = $border_end;
  6028. $h = $currentY - $this->y;
  6029. if ($resth > $h) {
  6030. $h = $resth;
  6031. }
  6032. } else {
  6033. // middle column
  6034. $cborder = $border_middle;
  6035. $h = $this->h - $this->y - $this->bMargin;
  6036. $resth -= $h;
  6037. }
  6038. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  6039. } // end for each column
  6040. } else { // middle page
  6041. for ($column = 0; $column < $this->num_columns; ++$column) { // for each column
  6042. $this->selectColumn($column);
  6043. if ($this->rtl) {
  6044. $this->x -= $mc_margin['R'];
  6045. } else {
  6046. $this->x += $mc_margin['L'];
  6047. }
  6048. $cborder = $border_middle;
  6049. $h = $this->h - $this->y - $this->bMargin;
  6050. $resth -= $h;
  6051. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  6052. } // end for each column
  6053. }
  6054. if ($cborder OR $fill) {
  6055. $offsetlen = strlen($ccode);
  6056. // draw border and fill
  6057. if ($this->inxobj) {
  6058. // we are inside an XObject template
  6059. if (end($this->xobjects[$this->xobjid]['transfmrk']) !== false) {
  6060. $pagemarkkey = key($this->xobjects[$this->xobjid]['transfmrk']);
  6061. $pagemark = $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey];
  6062. $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey] += $offsetlen;
  6063. } else {
  6064. $pagemark = $this->xobjects[$this->xobjid]['intmrk'];
  6065. $this->xobjects[$this->xobjid]['intmrk'] += $offsetlen;
  6066. }
  6067. $pagebuff = $this->xobjects[$this->xobjid]['outdata'];
  6068. $pstart = substr($pagebuff, 0, $pagemark);
  6069. $pend = substr($pagebuff, $pagemark);
  6070. $this->xobjects[$this->xobjid]['outdata'] = $pstart.$ccode.$pend;
  6071. } else {
  6072. if (end($this->transfmrk[$this->page]) !== false) {
  6073. $pagemarkkey = key($this->transfmrk[$this->page]);
  6074. $pagemark = $this->transfmrk[$this->page][$pagemarkkey];
  6075. $this->transfmrk[$this->page][$pagemarkkey] += $offsetlen;
  6076. } elseif ($this->InFooter) {
  6077. $pagemark = $this->footerpos[$this->page];
  6078. $this->footerpos[$this->page] += $offsetlen;
  6079. } else {
  6080. $pagemark = $this->intmrk[$this->page];
  6081. $this->intmrk[$this->page] += $offsetlen;
  6082. }
  6083. $pagebuff = $this->getPageBuffer($this->page);
  6084. $pstart = substr($pagebuff, 0, $pagemark);
  6085. $pend = substr($pagebuff, $pagemark);
  6086. $this->setPageBuffer($this->page, $pstart.$ccode.$pend);
  6087. }
  6088. }
  6089. } // end for each page
  6090. // Get end-of-cell Y position
  6091. $currentY = $this->GetY();
  6092. // restore original margin values
  6093. $this->SetLeftMargin($lMargin);
  6094. $this->SetRightMargin($rMargin);
  6095. if ($ln > 0) {
  6096. //Go to the beginning of the next line
  6097. $this->SetY($currentY + $mc_margin['B']);
  6098. if ($ln == 2) {
  6099. $this->SetX($x + $w + $mc_margin['L'] + $mc_margin['R']);
  6100. }
  6101. } else {
  6102. // go left or right by case
  6103. $this->setPage($startpage);
  6104. $this->y = $y;
  6105. $this->SetX($x + $w + $mc_margin['L'] + $mc_margin['R']);
  6106. }
  6107. $this->setContentMark();
  6108. $this->cell_padding = $prev_cell_padding;
  6109. $this->cell_margin = $prev_cell_margin;
  6110. return $nl;
  6111. }
  6112. /**
  6113. * Get the border mode accounting for multicell position (opens bottom side of multicell crossing pages)
  6114. * @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)))
  6115. * @param $position (string) multicell position: 'start', 'middle', 'end'
  6116. * @return border mode array
  6117. * @protected
  6118. * @since 4.4.002 (2008-12-09)
  6119. */
  6120. protected function getBorderMode($brd, $position='start') {
  6121. if ((!$this->opencell) OR empty($brd)) {
  6122. return $brd;
  6123. }
  6124. if ($brd == 1) {
  6125. $brd = 'LTRB';
  6126. }
  6127. if (is_string($brd)) {
  6128. // convert string to array
  6129. $slen = strlen($brd);
  6130. $newbrd = array();
  6131. for ($i = 0; $i < $slen; ++$i) {
  6132. $newbrd[$brd{$i}] = array('cap' => 'square', 'join' => 'miter');
  6133. }
  6134. $brd = $newbrd;
  6135. }
  6136. foreach ($brd as $border => $style) {
  6137. switch ($position) {
  6138. case 'start': {
  6139. if (strpos($border, 'B') !== false) {
  6140. // remove bottom line
  6141. $newkey = str_replace('B', '', $border);
  6142. if (strlen($newkey) > 0) {
  6143. $brd[$newkey] = $style;
  6144. }
  6145. unset($brd[$border]);
  6146. }
  6147. break;
  6148. }
  6149. case 'middle': {
  6150. if (strpos($border, 'B') !== false) {
  6151. // remove bottom line
  6152. $newkey = str_replace('B', '', $border);
  6153. if (strlen($newkey) > 0) {
  6154. $brd[$newkey] = $style;
  6155. }
  6156. unset($brd[$border]);
  6157. $border = $newkey;
  6158. }
  6159. if (strpos($border, 'T') !== false) {
  6160. // remove bottom line
  6161. $newkey = str_replace('T', '', $border);
  6162. if (strlen($newkey) > 0) {
  6163. $brd[$newkey] = $style;
  6164. }
  6165. unset($brd[$border]);
  6166. }
  6167. break;
  6168. }
  6169. case 'end': {
  6170. if (strpos($border, 'T') !== false) {
  6171. // remove bottom line
  6172. $newkey = str_replace('T', '', $border);
  6173. if (strlen($newkey) > 0) {
  6174. $brd[$newkey] = $style;
  6175. }
  6176. unset($brd[$border]);
  6177. }
  6178. break;
  6179. }
  6180. }
  6181. }
  6182. return $brd;
  6183. }
  6184. /**
  6185. * This method return the estimated number of lines for print a simple text string using Multicell() method.
  6186. * @param $txt (string) String for calculating his height
  6187. * @param $w (float) Width of cells. If 0, they extend up to the right margin of the page.
  6188. * @param $reseth (boolean) if true reset the last cell height (default false).
  6189. * @param $autopadding (boolean) if true, uses internal padding and automatically adjust it to account for line width (default true).
  6190. * @param $cellpadding (float) Internal cell padding, if empty uses default cell padding.
  6191. * @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)))
  6192. * @return float Return the minimal height needed for multicell method for printing the $txt param.
  6193. * @author Alexander Escalona Fernández, Nicola Asuni
  6194. * @public
  6195. * @since 4.5.011
  6196. */
  6197. public function getNumLines($txt, $w=0, $reseth=false, $autopadding=true, $cellpadding='', $border=0) {
  6198. if ($txt === '') {
  6199. // empty string
  6200. return 1;
  6201. }
  6202. // adjust internal padding
  6203. $prev_cell_padding = $this->cell_padding;
  6204. $prev_lasth = $this->lasth;
  6205. if (is_array($cellpadding)) {
  6206. $this->cell_padding = $cellpadding;
  6207. }
  6208. $this->adjustCellPadding($border);
  6209. if ($this->empty_string($w) OR ($w <= 0)) {
  6210. if ($this->rtl) {
  6211. $w = $this->x - $this->lMargin;
  6212. } else {
  6213. $w = $this->w - $this->rMargin - $this->x;
  6214. }
  6215. }
  6216. $wmax = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  6217. if ($reseth) {
  6218. // reset row height
  6219. $this->resetLastH();
  6220. }
  6221. $lines = 1;
  6222. $sum = 0;
  6223. $chars = $this->utf8Bidi($this->UTF8StringToArray($txt), $txt, $this->tmprtl);
  6224. $charsWidth = $this->GetArrStringWidth($chars, '', '', 0, true);
  6225. $length = count($chars);
  6226. $lastSeparator = -1;
  6227. for ($i = 0; $i < $length; ++$i) {
  6228. $charWidth = $charsWidth[$i];
  6229. if (preg_match($this->re_spaces, $this->unichr($chars[$i]))) {
  6230. $lastSeparator = $i;
  6231. }
  6232. if ((($sum + $charWidth) > $wmax) OR ($chars[$i] == 10)) {
  6233. ++$lines;
  6234. if ($chars[$i] == 10) {
  6235. $lastSeparator = -1;
  6236. $sum = 0;
  6237. } elseif ($lastSeparator != -1) {
  6238. $i = $lastSeparator;
  6239. $lastSeparator = -1;
  6240. $sum = 0;
  6241. } else {
  6242. $sum = $charWidth;
  6243. }
  6244. } else {
  6245. $sum += $charWidth;
  6246. }
  6247. }
  6248. if ($chars[($length - 1)] == 10) {
  6249. --$lines;
  6250. }
  6251. $this->cell_padding = $prev_cell_padding;
  6252. $this->lasth = $prev_lasth;
  6253. return $lines;
  6254. }
  6255. /**
  6256. * This method return the estimated needed height for print a simple text string in Multicell() method.
  6257. * Generally, if you want to know the exact height for a block of content you can use the following alternative technique:
  6258. * @pre
  6259. * // store current object
  6260. * $pdf->startTransaction();
  6261. * // store starting values
  6262. * $start_y = $pdf->GetY();
  6263. * $start_page = $pdf->getPage();
  6264. * // call your printing functions with your parameters
  6265. * // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  6266. * $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);
  6267. * // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  6268. * // get the new Y
  6269. * $end_y = $pdf->GetY();
  6270. * $end_page = $pdf->getPage();
  6271. * // calculate height
  6272. * $height = 0;
  6273. * if ($end_page == $start_page) {
  6274. * $height = $end_y - $start_y;
  6275. * } else {
  6276. * for ($page=$start_page; $page <= $end_page; ++$page) {
  6277. * $this->setPage($page);
  6278. * if ($page == $start_page) {
  6279. * // first page
  6280. * $height = $this->h - $start_y - $this->bMargin;
  6281. * } elseif ($page == $end_page) {
  6282. * // last page
  6283. * $height = $end_y - $this->tMargin;
  6284. * } else {
  6285. * $height = $this->h - $this->tMargin - $this->bMargin;
  6286. * }
  6287. * }
  6288. * }
  6289. * // restore previous object
  6290. * $pdf = $pdf->rollbackTransaction();
  6291. *
  6292. * @param $w (float) Width of cells. If 0, they extend up to the right margin of the page.
  6293. * @param $txt (string) String for calculating his height
  6294. * @param $reseth (boolean) if true reset the last cell height (default false).
  6295. * @param $autopadding (boolean) if true, uses internal padding and automatically adjust it to account for line width (default true).
  6296. * @param $cellpadding (float) Internal cell padding, if empty uses default cell padding.
  6297. * @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)))
  6298. * @return float Return the minimal height needed for multicell method for printing the $txt param.
  6299. * @author Nicola Asuni, Alexander Escalona Fernández
  6300. * @public
  6301. */
  6302. public function getStringHeight($w, $txt, $reseth=false, $autopadding=true, $cellpadding='', $border=0) {
  6303. // adjust internal padding
  6304. $prev_cell_padding = $this->cell_padding;
  6305. $prev_lasth = $this->lasth;
  6306. if (is_array($cellpadding)) {
  6307. $this->cell_padding = $cellpadding;
  6308. }
  6309. $this->adjustCellPadding($border);
  6310. $lines = $this->getNumLines($txt, $w, $reseth, $autopadding, $cellpadding, $border);
  6311. $height = $lines * ($this->FontSize * $this->cell_height_ratio);
  6312. if ($autopadding) {
  6313. // add top and bottom padding
  6314. $height += ($this->cell_padding['T'] + $this->cell_padding['B']);
  6315. }
  6316. $this->cell_padding = $prev_cell_padding;
  6317. $this->lasth = $prev_lasth;
  6318. return $height;
  6319. }
  6320. /**
  6321. * This method prints text from the current position.<br />
  6322. * @param $h (float) Line height
  6323. * @param $txt (string) String to print
  6324. * @param $link (mixed) URL or identifier returned by AddLink()
  6325. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  6326. * @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>
  6327. * @param $ln (boolean) if true set cursor at the bottom of the line, otherwise set cursor at the top of the line.
  6328. * @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.
  6329. * @param $firstline (boolean) if true prints only the first line and return the remaining string.
  6330. * @param $firstblock (boolean) if true the string is the starting of a line.
  6331. * @param $maxh (float) maximum height. The remaining unprinted text will be returned. It should be >= $h and less then remaining space to the bottom of the page, or 0 for disable this feature.
  6332. * @param $wadj (float) first line width will be reduced by this amount (used in HTML mode).
  6333. * @param $margin (array) margin array of the parent container
  6334. * @return mixed Return the number of cells or the remaining string if $firstline = true.
  6335. * @public
  6336. * @since 1.5
  6337. */
  6338. public function Write($h, $txt, $link='', $fill=false, $align='', $ln=false, $stretch=0, $firstline=false, $firstblock=false, $maxh=0, $wadj=0, $margin='') {
  6339. // check page for no-write regions and adapt page margins if necessary
  6340. list($this->x, $this->y) = $this->checkPageRegions($h, $this->x, $this->y);
  6341. if (strlen($txt) == 0) {
  6342. // fix empty text
  6343. $txt = ' ';
  6344. }
  6345. if ($margin === '') {
  6346. // set default margins
  6347. $margin = $this->cell_margin;
  6348. }
  6349. // remove carriage returns
  6350. $s = str_replace("\r", '', $txt);
  6351. // check if string contains arabic text
  6352. if (preg_match($this->unicode->uni_RE_PATTERN_ARABIC, $s)) {
  6353. $arabic = true;
  6354. } else {
  6355. $arabic = false;
  6356. }
  6357. // check if string contains RTL text
  6358. if ($arabic OR ($this->tmprtl == 'R') OR preg_match($this->unicode->uni_RE_PATTERN_RTL, $s)) {
  6359. $rtlmode = true;
  6360. } else {
  6361. $rtlmode = false;
  6362. }
  6363. // get a char width
  6364. $chrwidth = $this->GetCharWidth(46); // dot character
  6365. // get array of unicode values
  6366. $chars = $this->UTF8StringToArray($s);
  6367. // get array of chars
  6368. $uchars = $this->UTF8ArrayToUniArray($chars);
  6369. // get the number of characters
  6370. $nb = count($chars);
  6371. // replacement for SHY character (minus symbol)
  6372. $shy_replacement = 45;
  6373. $shy_replacement_char = $this->unichr($shy_replacement);
  6374. // widht for SHY replacement
  6375. $shy_replacement_width = $this->GetCharWidth($shy_replacement);
  6376. // max Y
  6377. $maxy = $this->y + $maxh - $h - $this->cell_padding['T'] - $this->cell_padding['B'];
  6378. // page width
  6379. $pw = $w = $this->w - $this->lMargin - $this->rMargin;
  6380. // calculate remaining line width ($w)
  6381. if ($this->rtl) {
  6382. $w = $this->x - $this->lMargin;
  6383. } else {
  6384. $w = $this->w - $this->rMargin - $this->x;
  6385. }
  6386. // max column width
  6387. $wmax = $w - $wadj;
  6388. if (!$firstline) {
  6389. $wmax -= ($this->cell_padding['L'] + $this->cell_padding['R']);
  6390. }
  6391. if ((!$firstline) AND (($chrwidth > $wmax) OR ($this->GetCharWidth($chars[0]) > $wmax))) {
  6392. // a single character do not fit on column
  6393. return '';
  6394. }
  6395. // minimum row height
  6396. $row_height = max($h, $this->FontSize * $this->cell_height_ratio);
  6397. $start_page = $this->page;
  6398. $i = 0; // character position
  6399. $j = 0; // current starting position
  6400. $sep = -1; // position of the last blank space
  6401. $shy = false; // true if the last blank is a soft hypen (SHY)
  6402. $l = 0; // current string length
  6403. $nl = 0; //number of lines
  6404. $linebreak = false;
  6405. $pc = 0; // previous character
  6406. // for each character
  6407. while ($i < $nb) {
  6408. if (($maxh > 0) AND ($this->y >= $maxy) ) {
  6409. break;
  6410. }
  6411. //Get the current character
  6412. $c = $chars[$i];
  6413. if ($c == 10) { // 10 = "\n" = new line
  6414. //Explicit line break
  6415. if ($align == 'J') {
  6416. if ($this->rtl) {
  6417. $talign = 'R';
  6418. } else {
  6419. $talign = 'L';
  6420. }
  6421. } else {
  6422. $talign = $align;
  6423. }
  6424. $tmpstr = $this->UniArrSubString($uchars, $j, $i);
  6425. if ($firstline) {
  6426. $startx = $this->x;
  6427. $tmparr = array_slice($chars, $j, ($i - $j));
  6428. if ($rtlmode) {
  6429. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  6430. }
  6431. $linew = $this->GetArrStringWidth($tmparr);
  6432. unset($tmparr);
  6433. if ($this->rtl) {
  6434. $this->endlinex = $startx - $linew;
  6435. } else {
  6436. $this->endlinex = $startx + $linew;
  6437. }
  6438. $w = $linew;
  6439. $tmpcellpadding = $this->cell_padding;
  6440. if ($maxh == 0) {
  6441. $this->SetCellPadding(0);
  6442. }
  6443. }
  6444. if ($firstblock AND $this->isRTLTextDir()) {
  6445. $tmpstr = $this->stringRightTrim($tmpstr);
  6446. }
  6447. // Skip newlines at the begining of a page or column
  6448. if (!empty($tmpstr) OR ($this->y < ($this->PageBreakTrigger - $row_height))) {
  6449. $this->Cell($w, $h, $tmpstr, 0, 1, $talign, $fill, $link, $stretch);
  6450. }
  6451. unset($tmpstr);
  6452. if ($firstline) {
  6453. $this->cell_padding = $tmpcellpadding;
  6454. return ($this->UniArrSubString($uchars, $i));
  6455. }
  6456. ++$nl;
  6457. $j = $i + 1;
  6458. $l = 0;
  6459. $sep = -1;
  6460. $shy = false;
  6461. // account for margin changes
  6462. if ((($this->y + $this->lasth) > $this->PageBreakTrigger) AND ($this->inPageBody())) {
  6463. $this->AcceptPageBreak();
  6464. if ($this->rtl) {
  6465. $this->x -= $margin['R'];
  6466. } else {
  6467. $this->x += $margin['L'];
  6468. }
  6469. $this->lMargin += $margin['L'];
  6470. $this->rMargin += $margin['R'];
  6471. }
  6472. $w = $this->getRemainingWidth();
  6473. $wmax = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  6474. } else {
  6475. // 160 is the non-breaking space.
  6476. // 173 is SHY (Soft Hypen).
  6477. // \p{Z} or \p{Separator}: any kind of Unicode whitespace or invisible separator.
  6478. // \p{Lo} or \p{Other_Letter}: a Unicode letter or ideograph that does not have lowercase and uppercase variants.
  6479. // \p{Lo} is needed because Chinese characters are packed next to each other without spaces in between.
  6480. if (($c != 160) AND (($c == 173) OR preg_match($this->re_spaces, $this->unichr($c)))) {
  6481. // update last blank space position
  6482. $sep = $i;
  6483. // check if is a SHY
  6484. if ($c == 173) {
  6485. $shy = true;
  6486. if ($pc == 45) {
  6487. $tmp_shy_replacement_width = 0;
  6488. $tmp_shy_replacement_char = '';
  6489. } else {
  6490. $tmp_shy_replacement_width = $shy_replacement_width;
  6491. $tmp_shy_replacement_char = $shy_replacement_char;
  6492. }
  6493. } else {
  6494. $shy = false;
  6495. }
  6496. }
  6497. // update string length
  6498. if ($this->isUnicodeFont() AND ($arabic)) {
  6499. // with bidirectional algorithm some chars may be changed affecting the line length
  6500. // *** very slow ***
  6501. $l = $this->GetArrStringWidth($this->utf8Bidi(array_slice($chars, $j, ($i - $j)), '', $this->tmprtl));
  6502. } else {
  6503. $l += $this->GetCharWidth($c);
  6504. }
  6505. if (($l > $wmax) OR (($c == 173) AND (($l + $tmp_shy_replacement_width) > $wmax)) ) {
  6506. // we have reached the end of column
  6507. if ($sep == -1) {
  6508. // check if the line was already started
  6509. if (($this->rtl AND ($this->x <= ($this->w - $this->rMargin - $chrwidth)))
  6510. OR ((!$this->rtl) AND ($this->x >= ($this->lMargin + $chrwidth)))) {
  6511. // print a void cell and go to next line
  6512. $this->Cell($w, $h, '', 0, 1);
  6513. $linebreak = true;
  6514. if ($firstline) {
  6515. return ($this->UniArrSubString($uchars, $j));
  6516. }
  6517. } else {
  6518. // truncate the word because do not fit on column
  6519. $tmpstr = $this->UniArrSubString($uchars, $j, $i);
  6520. if ($firstline) {
  6521. $startx = $this->x;
  6522. $tmparr = array_slice($chars, $j, ($i - $j));
  6523. if ($rtlmode) {
  6524. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  6525. }
  6526. $linew = $this->GetArrStringWidth($tmparr);
  6527. unset($tmparr);
  6528. if ($this->rtl) {
  6529. $this->endlinex = $startx - $linew;
  6530. } else {
  6531. $this->endlinex = $startx + $linew;
  6532. }
  6533. $w = $linew;
  6534. $tmpcellpadding = $this->cell_padding;
  6535. if ($maxh == 0) {
  6536. $this->SetCellPadding(0);
  6537. }
  6538. }
  6539. if ($firstblock AND $this->isRTLTextDir()) {
  6540. $tmpstr = $this->stringRightTrim($tmpstr);
  6541. }
  6542. $this->Cell($w, $h, $tmpstr, 0, 1, $align, $fill, $link, $stretch);
  6543. unset($tmpstr);
  6544. if ($firstline) {
  6545. $this->cell_padding = $tmpcellpadding;
  6546. return ($this->UniArrSubString($uchars, $i));
  6547. }
  6548. $j = $i;
  6549. --$i;
  6550. }
  6551. } else {
  6552. // word wrapping
  6553. if ($this->rtl AND (!$firstblock) AND ($sep < $i)) {
  6554. $endspace = 1;
  6555. } else {
  6556. $endspace = 0;
  6557. }
  6558. // check the length of the next string
  6559. $strrest = $this->UniArrSubString($uchars, ($sep + $endspace));
  6560. $nextstr = preg_split('/'.$this->re_space['p'].'/'.$this->re_space['m'], $this->stringTrim($strrest));
  6561. if (isset($nextstr[0]) AND ($this->GetStringWidth($nextstr[0]) > $pw)) {
  6562. // truncate the word because do not fit on a full page width
  6563. $tmpstr = $this->UniArrSubString($uchars, $j, $i);
  6564. if ($firstline) {
  6565. $startx = $this->x;
  6566. $tmparr = array_slice($chars, $j, ($i - $j));
  6567. if ($rtlmode) {
  6568. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  6569. }
  6570. $linew = $this->GetArrStringWidth($tmparr);
  6571. unset($tmparr);
  6572. if ($this->rtl) {
  6573. $this->endlinex = $startx - $linew;
  6574. } else {
  6575. $this->endlinex = $startx + $linew;
  6576. }
  6577. $w = $linew;
  6578. $tmpcellpadding = $this->cell_padding;
  6579. if ($maxh == 0) {
  6580. $this->SetCellPadding(0);
  6581. }
  6582. }
  6583. if ($firstblock AND $this->isRTLTextDir()) {
  6584. $tmpstr = $this->stringRightTrim($tmpstr);
  6585. }
  6586. $this->Cell($w, $h, $tmpstr, 0, 1, $align, $fill, $link, $stretch);
  6587. unset($tmpstr);
  6588. if ($firstline) {
  6589. $this->cell_padding = $tmpcellpadding;
  6590. return ($this->UniArrSubString($uchars, $i));
  6591. }
  6592. $j = $i;
  6593. --$i;
  6594. } else {
  6595. // word wrapping
  6596. if ($shy) {
  6597. // add hypen (minus symbol) at the end of the line
  6598. $shy_width = $tmp_shy_replacement_width;
  6599. if ($this->rtl) {
  6600. $shy_char_left = $tmp_shy_replacement_char;
  6601. $shy_char_right = '';
  6602. } else {
  6603. $shy_char_left = '';
  6604. $shy_char_right = $tmp_shy_replacement_char;
  6605. }
  6606. } else {
  6607. $shy_width = 0;
  6608. $shy_char_left = '';
  6609. $shy_char_right = '';
  6610. }
  6611. $tmpstr = $this->UniArrSubString($uchars, $j, ($sep + $endspace));
  6612. if ($firstline) {
  6613. $startx = $this->x;
  6614. $tmparr = array_slice($chars, $j, (($sep + $endspace) - $j));
  6615. if ($rtlmode) {
  6616. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  6617. }
  6618. $linew = $this->GetArrStringWidth($tmparr);
  6619. unset($tmparr);
  6620. if ($this->rtl) {
  6621. $this->endlinex = $startx - $linew - $shy_width;
  6622. } else {
  6623. $this->endlinex = $startx + $linew + $shy_width;
  6624. }
  6625. $w = $linew;
  6626. $tmpcellpadding = $this->cell_padding;
  6627. if ($maxh == 0) {
  6628. $this->SetCellPadding(0);
  6629. }
  6630. }
  6631. // print the line
  6632. if ($firstblock AND $this->isRTLTextDir()) {
  6633. $tmpstr = $this->stringRightTrim($tmpstr);
  6634. }
  6635. $this->Cell($w, $h, $shy_char_left.$tmpstr.$shy_char_right, 0, 1, $align, $fill, $link, $stretch);
  6636. unset($tmpstr);
  6637. if ($firstline) {
  6638. // return the remaining text
  6639. $this->cell_padding = $tmpcellpadding;
  6640. return ($this->UniArrSubString($uchars, ($sep + $endspace)));
  6641. }
  6642. $i = $sep;
  6643. $sep = -1;
  6644. $shy = false;
  6645. $j = ($i+1);
  6646. }
  6647. }
  6648. // account for margin changes
  6649. if ((($this->y + $this->lasth) > $this->PageBreakTrigger) AND ($this->inPageBody())) {
  6650. $this->AcceptPageBreak();
  6651. if ($this->rtl) {
  6652. $this->x -= $margin['R'];
  6653. } else {
  6654. $this->x += $margin['L'];
  6655. }
  6656. $this->lMargin += $margin['L'];
  6657. $this->rMargin += $margin['R'];
  6658. }
  6659. $w = $this->getRemainingWidth();
  6660. $wmax = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  6661. if ($linebreak) {
  6662. $linebreak = false;
  6663. } else {
  6664. ++$nl;
  6665. $l = 0;
  6666. }
  6667. }
  6668. }
  6669. // save last character
  6670. $pc = $c;
  6671. ++$i;
  6672. } // end while i < nb
  6673. // print last substring (if any)
  6674. if ($l > 0) {
  6675. switch ($align) {
  6676. case 'J':
  6677. case 'C': {
  6678. $w = $w;
  6679. break;
  6680. }
  6681. case 'L': {
  6682. if ($this->rtl) {
  6683. $w = $w;
  6684. } else {
  6685. $w = $l;
  6686. }
  6687. break;
  6688. }
  6689. case 'R': {
  6690. if ($this->rtl) {
  6691. $w = $l;
  6692. } else {
  6693. $w = $w;
  6694. }
  6695. break;
  6696. }
  6697. default: {
  6698. $w = $l;
  6699. break;
  6700. }
  6701. }
  6702. $tmpstr = $this->UniArrSubString($uchars, $j, $nb);
  6703. if ($firstline) {
  6704. $startx = $this->x;
  6705. $tmparr = array_slice($chars, $j, ($nb - $j));
  6706. if ($rtlmode) {
  6707. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  6708. }
  6709. $linew = $this->GetArrStringWidth($tmparr);
  6710. unset($tmparr);
  6711. if ($this->rtl) {
  6712. $this->endlinex = $startx - $linew;
  6713. } else {
  6714. $this->endlinex = $startx + $linew;
  6715. }
  6716. $w = $linew;
  6717. $tmpcellpadding = $this->cell_padding;
  6718. if ($maxh == 0) {
  6719. $this->SetCellPadding(0);
  6720. }
  6721. }
  6722. if ($firstblock AND $this->isRTLTextDir()) {
  6723. $tmpstr = $this->stringRightTrim($tmpstr);
  6724. }
  6725. $this->Cell($w, $h, $tmpstr, 0, $ln, $align, $fill, $link, $stretch);
  6726. unset($tmpstr);
  6727. if ($firstline) {
  6728. $this->cell_padding = $tmpcellpadding;
  6729. return ($this->UniArrSubString($uchars, $nb));
  6730. }
  6731. ++$nl;
  6732. }
  6733. if ($firstline) {
  6734. return '';
  6735. }
  6736. return $nl;
  6737. }
  6738. /**
  6739. * Returns the remaining width between the current position and margins.
  6740. * @return int Return the remaining width
  6741. * @protected
  6742. */
  6743. protected function getRemainingWidth() {
  6744. list($this->x, $this->y) = $this->checkPageRegions(0, $this->x, $this->y);
  6745. if ($this->rtl) {
  6746. return ($this->x - $this->lMargin);
  6747. } else {
  6748. return ($this->w - $this->rMargin - $this->x);
  6749. }
  6750. }
  6751. /**
  6752. * Extract a slice of the $strarr array and return it as string.
  6753. * @param $strarr (string) The input array of characters.
  6754. * @param $start (int) the starting element of $strarr.
  6755. * @param $end (int) first element that will not be returned.
  6756. * @return Return part of a string
  6757. * @public
  6758. */
  6759. public function UTF8ArrSubString($strarr, $start='', $end='') {
  6760. if (strlen($start) == 0) {
  6761. $start = 0;
  6762. }
  6763. if (strlen($end) == 0) {
  6764. $end = count($strarr);
  6765. }
  6766. $string = '';
  6767. for ($i=$start; $i < $end; ++$i) {
  6768. $string .= $this->unichr($strarr[$i]);
  6769. }
  6770. return $string;
  6771. }
  6772. /**
  6773. * Extract a slice of the $uniarr array and return it as string.
  6774. * @param $uniarr (string) The input array of characters.
  6775. * @param $start (int) the starting element of $strarr.
  6776. * @param $end (int) first element that will not be returned.
  6777. * @return Return part of a string
  6778. * @public
  6779. * @since 4.5.037 (2009-04-07)
  6780. */
  6781. public function UniArrSubString($uniarr, $start='', $end='') {
  6782. if (strlen($start) == 0) {
  6783. $start = 0;
  6784. }
  6785. if (strlen($end) == 0) {
  6786. $end = count($uniarr);
  6787. }
  6788. $string = '';
  6789. for ($i=$start; $i < $end; ++$i) {
  6790. $string .= $uniarr[$i];
  6791. }
  6792. return $string;
  6793. }
  6794. /**
  6795. * Convert an array of UTF8 values to array of unicode characters
  6796. * @param $ta (string) The input array of UTF8 values.
  6797. * @return Return array of unicode characters
  6798. * @public
  6799. * @since 4.5.037 (2009-04-07)
  6800. */
  6801. public function UTF8ArrayToUniArray($ta) {
  6802. return array_map(array($this, 'unichr'), $ta);
  6803. }
  6804. /**
  6805. * Returns the unicode caracter specified by UTF-8 value
  6806. * @param $c (int) UTF-8 value
  6807. * @return Returns the specified character.
  6808. * @author Miguel Perez, Nicola Asuni
  6809. * @public
  6810. * @since 2.3.000 (2008-03-05)
  6811. */
  6812. public function unichr($c) {
  6813. if (!$this->isunicode) {
  6814. return chr($c);
  6815. } elseif ($c <= 0x7F) {
  6816. // one byte
  6817. return chr($c);
  6818. } elseif ($c <= 0x7FF) {
  6819. // two bytes
  6820. return chr(0xC0 | $c >> 6).chr(0x80 | $c & 0x3F);
  6821. } elseif ($c <= 0xFFFF) {
  6822. // three bytes
  6823. return chr(0xE0 | $c >> 12).chr(0x80 | $c >> 6 & 0x3F).chr(0x80 | $c & 0x3F);
  6824. } elseif ($c <= 0x10FFFF) {
  6825. // four bytes
  6826. return chr(0xF0 | $c >> 18).chr(0x80 | $c >> 12 & 0x3F).chr(0x80 | $c >> 6 & 0x3F).chr(0x80 | $c & 0x3F);
  6827. } else {
  6828. return '';
  6829. }
  6830. }
  6831. /**
  6832. * Return the image type given the file name or array returned by getimagesize() function.
  6833. * @param $imgfile (string) image file name
  6834. * @param $iminfo (array) array of image information returned by getimagesize() function.
  6835. * @return string image type
  6836. * @since 4.8.017 (2009-11-27)
  6837. */
  6838. public function getImageFileType($imgfile, $iminfo=array()) {
  6839. $type = '';
  6840. if (isset($iminfo['mime']) AND !empty($iminfo['mime'])) {
  6841. $mime = explode('/', $iminfo['mime']);
  6842. if ((count($mime) > 1) AND ($mime[0] == 'image') AND (!empty($mime[1]))) {
  6843. $type = strtolower(trim($mime[1]));
  6844. }
  6845. }
  6846. if (empty($type)) {
  6847. $fileinfo = pathinfo($imgfile);
  6848. if (isset($fileinfo['extension']) AND (!$this->empty_string($fileinfo['extension']))) {
  6849. $type = strtolower(trim($fileinfo['extension']));
  6850. }
  6851. }
  6852. if ($type == 'jpg') {
  6853. $type = 'jpeg';
  6854. }
  6855. return $type;
  6856. }
  6857. /**
  6858. * Set the block dimensions accounting for page breaks and page/column fitting
  6859. * @param $w (float) width
  6860. * @param $h (float) height
  6861. * @param $x (float) X coordinate
  6862. * @param $y (float) Y coodiante
  6863. * @param $fitonpage (boolean) if true the block is resized to not exceed page dimensions.
  6864. * @return array($w, $h, $x, $y)
  6865. * @protected
  6866. * @since 5.5.009 (2010-07-05)
  6867. */
  6868. protected function fitBlock($w, $h, $x, $y, $fitonpage=false) {
  6869. if ($w <= 0) {
  6870. // set maximum width
  6871. $w = ($this->w - $this->lMargin - $this->rMargin);
  6872. }
  6873. if ($h <= 0) {
  6874. // set maximum height
  6875. $h = ($this->PageBreakTrigger - $this->tMargin);
  6876. }
  6877. // resize the block to be vertically contained on a single page or single column
  6878. if ($fitonpage OR $this->AutoPageBreak) {
  6879. $ratio_wh = ($w / $h);
  6880. if ($h > ($this->PageBreakTrigger - $this->tMargin)) {
  6881. $h = $this->PageBreakTrigger - $this->tMargin;
  6882. $w = ($h * $ratio_wh);
  6883. }
  6884. // resize the block to be horizontally contained on a single page or single column
  6885. if ($fitonpage) {
  6886. $maxw = ($this->w - $this->lMargin - $this->rMargin);
  6887. if ($w > $maxw) {
  6888. $w = $maxw;
  6889. $h = ($w / $ratio_wh);
  6890. }
  6891. }
  6892. }
  6893. // Check whether we need a new page or new column first as this does not fit
  6894. $prev_x = $this->x;
  6895. $prev_y = $this->y;
  6896. if ($this->checkPageBreak($h, $y) OR ($this->y < $prev_y)) {
  6897. $y = $this->y;
  6898. if ($this->rtl) {
  6899. $x += ($prev_x - $this->x);
  6900. } else {
  6901. $x += ($this->x - $prev_x);
  6902. }
  6903. $this->newline = true;
  6904. }
  6905. // resize the block to be contained on the remaining available page or column space
  6906. if ($fitonpage) {
  6907. $ratio_wh = ($w / $h);
  6908. if (($y + $h) > $this->PageBreakTrigger) {
  6909. $h = $this->PageBreakTrigger - $y;
  6910. $w = ($h * $ratio_wh);
  6911. }
  6912. if ((!$this->rtl) AND (($x + $w) > ($this->w - $this->rMargin))) {
  6913. $w = $this->w - $this->rMargin - $x;
  6914. $h = ($w / $ratio_wh);
  6915. } elseif (($this->rtl) AND (($x - $w) < ($this->lMargin))) {
  6916. $w = $x - $this->lMargin;
  6917. $h = ($w / $ratio_wh);
  6918. }
  6919. }
  6920. return array($w, $h, $x, $y);
  6921. }
  6922. /**
  6923. * Puts an image in the page.
  6924. * The upper-left corner must be given.
  6925. * The dimensions can be specified in different ways:<ul>
  6926. * <li>explicit width and height (expressed in user unit)</li>
  6927. * <li>one explicit dimension, the other being calculated automatically in order to keep the original proportions</li>
  6928. * <li>no explicit dimension, in which case the image is put at 72 dpi</li></ul>
  6929. * 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;
  6930. * The format can be specified explicitly or inferred from the file extension.<br />
  6931. * It is possible to put a link on the image.<br />
  6932. * Remark: if an image is used several times, only one copy will be embedded in the file.<br />
  6933. * @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').
  6934. * @param $x (float) Abscissa of the upper-left corner (LTR) or upper-right corner (RTL).
  6935. * @param $y (float) Ordinate of the upper-left corner (LTR) or upper-right corner (RTL).
  6936. * @param $w (float) Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  6937. * @param $h (float) Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  6938. * @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.
  6939. * @param $link (mixed) URL or identifier returned by AddLink().
  6940. * @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>
  6941. * @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).
  6942. * @param $dpi (int) dot-per-inch resolution used on resize
  6943. * @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>
  6944. * @param $ismask (boolean) true if this image is a mask, false otherwise
  6945. * @param $imgmask (mixed) image object returned by this function or false
  6946. * @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)))
  6947. * @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).
  6948. * @param $hidden (boolean) If true do not display the image.
  6949. * @param $fitonpage (boolean) If true the image is resized to not exceed page dimensions.
  6950. * @param $alt (boolean) If true the image will be added as alternative and not directly printed (the ID of the image will be returned).
  6951. * @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.
  6952. * @return image information
  6953. * @public
  6954. * @since 1.1
  6955. */
  6956. 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()) {
  6957. if ($x === '') {
  6958. $x = $this->x;
  6959. }
  6960. if ($y === '') {
  6961. $y = $this->y;
  6962. }
  6963. // check page for no-write regions and adapt page margins if necessary
  6964. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  6965. $cached_file = false; // true when the file is cached
  6966. $exurl = ''; // external streams
  6967. // check if we are passing an image as file or string
  6968. if ($file{0} === '@') {
  6969. // image from string
  6970. $imgdata = substr($file, 1);
  6971. $file = K_PATH_CACHE.'img_'.md5($imgdata);
  6972. $fp = fopen($file, 'w');
  6973. fwrite($fp, $imgdata);
  6974. fclose($fp);
  6975. unset($imgdata);
  6976. $cached_file = true;
  6977. $imsize = @getimagesize($file);
  6978. if ($imsize === FALSE) {
  6979. unlink($file);
  6980. $cached_file = false;
  6981. }
  6982. } else { // image file
  6983. if ($file{0} === '*') {
  6984. // image as external stream
  6985. $file = substr($file, 1);
  6986. $exurl = $file;
  6987. }
  6988. // check if is local file
  6989. if (!@file_exists($file)) {
  6990. // encode spaces on filename (file is probably an URL)
  6991. $file = str_replace(' ', '%20', $file);
  6992. }
  6993. if (@file_exists($file)) {
  6994. // get image dimensions
  6995. $imsize = @getimagesize($file);
  6996. } else {
  6997. $imsize = false;
  6998. }
  6999. if ($imsize === FALSE) {
  7000. if (function_exists('curl_init')) {
  7001. // try to get remote file data using cURL
  7002. $cs = curl_init(); // curl session
  7003. curl_setopt($cs, CURLOPT_URL, $file);
  7004. curl_setopt($cs, CURLOPT_BINARYTRANSFER, true);
  7005. curl_setopt($cs, CURLOPT_FAILONERROR, true);
  7006. curl_setopt($cs, CURLOPT_RETURNTRANSFER, true);
  7007. curl_setopt($cs, CURLOPT_FOLLOWLOCATION, true);
  7008. curl_setopt($cs, CURLOPT_CONNECTTIMEOUT, 5);
  7009. curl_setopt($cs, CURLOPT_TIMEOUT, 30);
  7010. $imgdata = curl_exec($cs);
  7011. curl_close($cs);
  7012. if ($imgdata !== FALSE) {
  7013. // copy image to cache
  7014. $file = K_PATH_CACHE.'img_'.md5($imgdata);
  7015. $fp = fopen($file, 'w');
  7016. fwrite($fp, $imgdata);
  7017. fclose($fp);
  7018. unset($imgdata);
  7019. $cached_file = true;
  7020. $imsize = @getimagesize($file);
  7021. if ($imsize === FALSE) {
  7022. unlink($file);
  7023. $cached_file = false;
  7024. }
  7025. }
  7026. } elseif (($w > 0) AND ($h > 0)) {
  7027. // get measures from specified data
  7028. $pw = $this->getHTMLUnitToUnits($w, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
  7029. $ph = $this->getHTMLUnitToUnits($h, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
  7030. $imsize = array($pw, $ph);
  7031. }
  7032. }
  7033. }
  7034. if ($imsize === FALSE) {
  7035. if (substr($file, 0, -34) == K_PATH_CACHE.'msk') { // mask file
  7036. // get measures from specified data
  7037. $pw = $this->getHTMLUnitToUnits($w, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
  7038. $ph = $this->getHTMLUnitToUnits($h, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
  7039. $imsize = array($pw, $ph);
  7040. } else {
  7041. $this->Error('[Image] Unable to get image: '.$file);
  7042. }
  7043. }
  7044. // get original image width and height in pixels
  7045. list($pixw, $pixh) = $imsize;
  7046. // calculate image width and height on document
  7047. if (($w <= 0) AND ($h <= 0)) {
  7048. // convert image size to document unit
  7049. $w = $this->pixelsToUnits($pixw);
  7050. $h = $this->pixelsToUnits($pixh);
  7051. } elseif ($w <= 0) {
  7052. $w = $h * $pixw / $pixh;
  7053. } elseif ($h <= 0) {
  7054. $h = $w * $pixh / $pixw;
  7055. } elseif (($fitbox !== false) AND ($w > 0) AND ($h > 0)) {
  7056. if (strlen($fitbox) !== 2) {
  7057. // set default alignment
  7058. $fitbox = '--';
  7059. }
  7060. // scale image dimensions proportionally to fit within the ($w, $h) box
  7061. if ((($w * $pixh) / ($h * $pixw)) < 1) {
  7062. // store current height
  7063. $oldh = $h;
  7064. // calculate new height
  7065. $h = $w * $pixh / $pixw;
  7066. // height difference
  7067. $hdiff = ($oldh - $h);
  7068. // vertical alignment
  7069. switch (strtoupper($fitbox{1})) {
  7070. case 'T': {
  7071. break;
  7072. }
  7073. case 'M': {
  7074. $y += ($hdiff / 2);
  7075. break;
  7076. }
  7077. case 'B': {
  7078. $y += $hdiff;
  7079. break;
  7080. }
  7081. }
  7082. } else {
  7083. // store current width
  7084. $oldw = $w;
  7085. // calculate new width
  7086. $w = $h * $pixw / $pixh;
  7087. // width difference
  7088. $wdiff = ($oldw - $w);
  7089. // horizontal alignment
  7090. switch (strtoupper($fitbox{0})) {
  7091. case 'L': {
  7092. if ($this->rtl) {
  7093. $x -= $wdiff;
  7094. }
  7095. break;
  7096. }
  7097. case 'C': {
  7098. if ($this->rtl) {
  7099. $x -= ($wdiff / 2);
  7100. } else {
  7101. $x += ($wdiff / 2);
  7102. }
  7103. break;
  7104. }
  7105. case 'R': {
  7106. if (!$this->rtl) {
  7107. $x += $wdiff;
  7108. }
  7109. break;
  7110. }
  7111. }
  7112. }
  7113. }
  7114. // fit the image on available space
  7115. list($w, $h, $x, $y) = $this->fitBlock($w, $h, $x, $y, $fitonpage);
  7116. // calculate new minimum dimensions in pixels
  7117. $neww = round($w * $this->k * $dpi / $this->dpi);
  7118. $newh = round($h * $this->k * $dpi / $this->dpi);
  7119. // check if resize is necessary (resize is used only to reduce the image)
  7120. $newsize = ($neww * $newh);
  7121. $pixsize = ($pixw * $pixh);
  7122. if (intval($resize) == 2) {
  7123. $resize = true;
  7124. } elseif ($newsize >= $pixsize) {
  7125. $resize = false;
  7126. }
  7127. // check if image has been already added on document
  7128. $newimage = true;
  7129. if (in_array($file, $this->imagekeys)) {
  7130. $newimage = false;
  7131. // get existing image data
  7132. $info = $this->getImageBuffer($file);
  7133. if (substr($file, 0, -34) != K_PATH_CACHE.'msk') {
  7134. // check if the newer image is larger
  7135. $oldsize = ($info['w'] * $info['h']);
  7136. if ((($oldsize < $newsize) AND ($resize)) OR (($oldsize < $pixsize) AND (!$resize))) {
  7137. $newimage = true;
  7138. }
  7139. }
  7140. } elseif (substr($file, 0, -34) != K_PATH_CACHE.'msk') {
  7141. // check for cached images with alpha channel
  7142. $filehash = md5($file);
  7143. $tempfile_plain = K_PATH_CACHE.'mskp_'.$filehash;
  7144. $tempfile_alpha = K_PATH_CACHE.'mska_'.$filehash;
  7145. if (in_array($tempfile_plain, $this->imagekeys)) {
  7146. // get existing image data
  7147. $info = $this->getImageBuffer($tempfile_plain);
  7148. // check if the newer image is larger
  7149. $oldsize = ($info['w'] * $info['h']);
  7150. if ((($oldsize < $newsize) AND ($resize)) OR (($oldsize < $pixsize) AND (!$resize))) {
  7151. $newimage = true;
  7152. } else {
  7153. $newimage = false;
  7154. // embed mask image
  7155. $imgmask = $this->Image($tempfile_alpha, $x, $y, $w, $h, 'PNG', '', '', $resize, $dpi, '', true, false);
  7156. // embed image, masked with previously embedded mask
  7157. return $this->Image($tempfile_plain, $x, $y, $w, $h, $type, $link, $align, $resize, $dpi, $palign, false, $imgmask);
  7158. }
  7159. }
  7160. }
  7161. if ($newimage) {
  7162. //First use of image, get info
  7163. $type = strtolower($type);
  7164. if ($type == '') {
  7165. $type = $this->getImageFileType($file, $imsize);
  7166. } elseif ($type == 'jpg') {
  7167. $type = 'jpeg';
  7168. }
  7169. $mqr = $this->get_mqr();
  7170. $this->set_mqr(false);
  7171. // Specific image handlers
  7172. $mtd = '_parse'.$type;
  7173. // GD image handler function
  7174. $gdfunction = 'imagecreatefrom'.$type;
  7175. $info = false;
  7176. if ((method_exists($this, $mtd)) AND (!($resize AND (function_exists($gdfunction) OR extension_loaded('imagick'))))) {
  7177. // TCPDF image functions
  7178. $info = $this->$mtd($file);
  7179. if ($info == 'pngalpha') {
  7180. return $this->ImagePngAlpha($file, $x, $y, $pixw, $pixh, $w, $h, 'PNG', $link, $align, $resize, $dpi, $palign, $filehash);
  7181. }
  7182. }
  7183. if (!$info) {
  7184. if (function_exists($gdfunction)) {
  7185. // GD library
  7186. $img = $gdfunction($file);
  7187. if ($resize) {
  7188. $imgr = imagecreatetruecolor($neww, $newh);
  7189. if (($type == 'gif') OR ($type == 'png')) {
  7190. $imgr = $this->_setGDImageTransparency($imgr, $img);
  7191. }
  7192. imagecopyresampled($imgr, $img, 0, 0, 0, 0, $neww, $newh, $pixw, $pixh);
  7193. if (($type == 'gif') OR ($type == 'png')) {
  7194. $info = $this->_toPNG($imgr);
  7195. } else {
  7196. $info = $this->_toJPEG($imgr);
  7197. }
  7198. } else {
  7199. if (($type == 'gif') OR ($type == 'png')) {
  7200. $info = $this->_toPNG($img);
  7201. } else {
  7202. $info = $this->_toJPEG($img);
  7203. }
  7204. }
  7205. } elseif (extension_loaded('imagick')) {
  7206. // ImageMagick library
  7207. $img = new Imagick();
  7208. if ($type == 'SVG') {
  7209. // get SVG file content
  7210. $svgimg = file_get_contents($file);
  7211. // get width and height
  7212. $regs = array();
  7213. if (preg_match('/<svg([^\>]*)>/si', $svgimg, $regs)) {
  7214. $svgtag = $regs[1];
  7215. $tmp = array();
  7216. if (preg_match('/[\s]+width[\s]*=[\s]*"([^"]*)"/si', $svgtag, $tmp)) {
  7217. $ow = $this->getHTMLUnitToUnits($tmp[1], 1, $this->svgunit, false);
  7218. $owu = sprintf('%.3F', ($ow * $dpi / 72)).$this->pdfunit;
  7219. $svgtag = preg_replace('/[\s]+width[\s]*=[\s]*"[^"]*"/si', ' width="'.$owu.'"', $svgtag, 1);
  7220. } else {
  7221. $ow = $w;
  7222. }
  7223. $tmp = array();
  7224. if (preg_match('/[\s]+height[\s]*=[\s]*"([^"]*)"/si', $svgtag, $tmp)) {
  7225. $oh = $this->getHTMLUnitToUnits($tmp[1], 1, $this->svgunit, false);
  7226. $ohu = sprintf('%.3F', ($oh * $dpi / 72)).$this->pdfunit;
  7227. $svgtag = preg_replace('/[\s]+height[\s]*=[\s]*"[^"]*"/si', ' height="'.$ohu.'"', $svgtag, 1);
  7228. } else {
  7229. $oh = $h;
  7230. }
  7231. $tmp = array();
  7232. if (!preg_match('/[\s]+viewBox[\s]*=[\s]*"[\s]*([0-9\.]+)[\s]+([0-9\.]+)[\s]+([0-9\.]+)[\s]+([0-9\.]+)[\s]*"/si', $svgtag, $tmp)) {
  7233. $vbw = ($ow * $this->imgscale * $this->k);
  7234. $vbh = ($oh * $this->imgscale * $this->k);
  7235. $vbox = sprintf(' viewBox="0 0 %.3F %.3F" ', $vbw, $vbh);
  7236. $svgtag = $vbox.$svgtag;
  7237. }
  7238. $svgimg = preg_replace('/<svg([^\>]*)>/si', '<svg'.$svgtag.'>', $svgimg, 1);
  7239. }
  7240. $img->readImageBlob($svgimg);
  7241. } else {
  7242. $img->readImage($file);
  7243. }
  7244. if ($resize) {
  7245. $img->resizeImage($neww, $newh, 10, 1, false);
  7246. }
  7247. $img->setCompressionQuality($this->jpeg_quality);
  7248. $img->setImageFormat('jpeg');
  7249. $tempname = tempnam(K_PATH_CACHE, 'jpg_');
  7250. $img->writeImage($tempname);
  7251. $info = $this->_parsejpeg($tempname);
  7252. unlink($tempname);
  7253. $img->destroy();
  7254. } else {
  7255. return;
  7256. }
  7257. }
  7258. if ($info === false) {
  7259. //If false, we cannot process image
  7260. return;
  7261. }
  7262. $this->set_mqr($mqr);
  7263. if ($ismask) {
  7264. // force grayscale
  7265. $info['cs'] = 'DeviceGray';
  7266. }
  7267. $info['i'] = $this->numimages;
  7268. if (!in_array($file, $this->imagekeys)) {
  7269. ++$info['i'];
  7270. }
  7271. if ($imgmask !== false) {
  7272. $info['masked'] = $imgmask;
  7273. }
  7274. if (!empty($exurl)) {
  7275. $info['exurl'] = $exurl;
  7276. }
  7277. // array of alternative images
  7278. $info['altimgs'] = $altimgs;
  7279. // add image to document
  7280. $this->setImageBuffer($file, $info);
  7281. }
  7282. if ($cached_file) {
  7283. // remove cached file
  7284. unlink($file);
  7285. }
  7286. // set alignment
  7287. $this->img_rb_y = $y + $h;
  7288. // set alignment
  7289. if ($this->rtl) {
  7290. if ($palign == 'L') {
  7291. $ximg = $this->lMargin;
  7292. } elseif ($palign == 'C') {
  7293. $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  7294. } elseif ($palign == 'R') {
  7295. $ximg = $this->w - $this->rMargin - $w;
  7296. } else {
  7297. $ximg = $x - $w;
  7298. }
  7299. $this->img_rb_x = $ximg;
  7300. } else {
  7301. if ($palign == 'L') {
  7302. $ximg = $this->lMargin;
  7303. } elseif ($palign == 'C') {
  7304. $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  7305. } elseif ($palign == 'R') {
  7306. $ximg = $this->w - $this->rMargin - $w;
  7307. } else {
  7308. $ximg = $x;
  7309. }
  7310. $this->img_rb_x = $ximg + $w;
  7311. }
  7312. if ($ismask OR $hidden) {
  7313. // image is not displayed
  7314. return $info['i'];
  7315. }
  7316. $xkimg = $ximg * $this->k;
  7317. if (!$alt) {
  7318. // only non-alternative immages will be set
  7319. $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']));
  7320. }
  7321. if (!empty($border)) {
  7322. $bx = $this->x;
  7323. $by = $this->y;
  7324. $this->x = $ximg;
  7325. if ($this->rtl) {
  7326. $this->x += $w;
  7327. }
  7328. $this->y = $y;
  7329. $this->Cell($w, $h, '', $border, 0, '', 0, '', 0, true);
  7330. $this->x = $bx;
  7331. $this->y = $by;
  7332. }
  7333. if ($link) {
  7334. $this->Link($ximg, $y, $w, $h, $link, 0);
  7335. }
  7336. // set pointer to align the next text/objects
  7337. switch($align) {
  7338. case 'T': {
  7339. $this->y = $y;
  7340. $this->x = $this->img_rb_x;
  7341. break;
  7342. }
  7343. case 'M': {
  7344. $this->y = $y + round($h/2);
  7345. $this->x = $this->img_rb_x;
  7346. break;
  7347. }
  7348. case 'B': {
  7349. $this->y = $this->img_rb_y;
  7350. $this->x = $this->img_rb_x;
  7351. break;
  7352. }
  7353. case 'N': {
  7354. $this->SetY($this->img_rb_y);
  7355. break;
  7356. }
  7357. default:{
  7358. break;
  7359. }
  7360. }
  7361. $this->endlinex = $this->img_rb_x;
  7362. if ($this->inxobj) {
  7363. // we are inside an XObject template
  7364. $this->xobjects[$this->xobjid]['images'][] = $info['i'];
  7365. }
  7366. return $info['i'];
  7367. }
  7368. /**
  7369. * Sets the current active configuration setting of magic_quotes_runtime (if the set_magic_quotes_runtime function exist)
  7370. * @param $mqr (boolean) FALSE for off, TRUE for on.
  7371. * @since 4.6.025 (2009-08-17)
  7372. */
  7373. public function set_mqr($mqr) {
  7374. if (!defined('PHP_VERSION_ID')) {
  7375. $version = PHP_VERSION;
  7376. define('PHP_VERSION_ID', (($version{0} * 10000) + ($version{2} * 100) + $version{4}));
  7377. }
  7378. if (PHP_VERSION_ID < 50300) {
  7379. @set_magic_quotes_runtime($mqr);
  7380. }
  7381. }
  7382. /**
  7383. * Gets the current active configuration setting of magic_quotes_runtime (if the get_magic_quotes_runtime function exist)
  7384. * @return Returns 0 if magic quotes runtime is off or get_magic_quotes_runtime doesn't exist, 1 otherwise.
  7385. * @since 4.6.025 (2009-08-17)
  7386. */
  7387. public function get_mqr() {
  7388. if (!defined('PHP_VERSION_ID')) {
  7389. $version = PHP_VERSION;
  7390. define('PHP_VERSION_ID', (($version{0} * 10000) + ($version{2} * 100) + $version{4}));
  7391. }
  7392. if (PHP_VERSION_ID < 50300) {
  7393. return @get_magic_quotes_runtime();
  7394. }
  7395. return 0;
  7396. }
  7397. /**
  7398. * Convert the loaded image to a JPEG and then return a structure for the PDF creator.
  7399. * This function requires GD library and write access to the directory defined on K_PATH_CACHE constant.
  7400. * @param $image (image) Image object.
  7401. * return image JPEG image object.
  7402. * @protected
  7403. */
  7404. protected function _toJPEG($image) {
  7405. $tempname = tempnam(K_PATH_CACHE, 'jpg_');
  7406. imagejpeg($image, $tempname, $this->jpeg_quality);
  7407. imagedestroy($image);
  7408. $retvars = $this->_parsejpeg($tempname);
  7409. // tidy up by removing temporary image
  7410. unlink($tempname);
  7411. return $retvars;
  7412. }
  7413. /**
  7414. * Convert the loaded image to a PNG and then return a structure for the PDF creator.
  7415. * This function requires GD library and write access to the directory defined on K_PATH_CACHE constant.
  7416. * @param $image (image) Image object.
  7417. * return image PNG image object.
  7418. * @protected
  7419. * @since 4.9.016 (2010-04-20)
  7420. */
  7421. protected function _toPNG($image) {
  7422. // set temporary image file name
  7423. $tempname = tempnam(K_PATH_CACHE, 'jpg_');
  7424. // turn off interlaced mode
  7425. imageinterlace($image, 0);
  7426. // create temporary PNG image
  7427. imagepng($image, $tempname);
  7428. // remove image from memory
  7429. imagedestroy($image);
  7430. // get PNG image data
  7431. $retvars = $this->_parsepng($tempname);
  7432. // tidy up by removing temporary image
  7433. unlink($tempname);
  7434. return $retvars;
  7435. }
  7436. /**
  7437. * Set the transparency for the given GD image.
  7438. * @param $new_image (image) GD image object
  7439. * @param $image (image) GD image object.
  7440. * return GD image object.
  7441. * @protected
  7442. * @since 4.9.016 (2010-04-20)
  7443. */
  7444. protected function _setGDImageTransparency($new_image, $image) {
  7445. // transparency index
  7446. $tid = imagecolortransparent($image);
  7447. // default transparency color
  7448. $tcol = array('red' => 255, 'green' => 255, 'blue' => 255);
  7449. if ($tid >= 0) {
  7450. // get the colors for the transparency index
  7451. $tcol = imagecolorsforindex($image, $tid);
  7452. }
  7453. $tid = imagecolorallocate($new_image, $tcol['red'], $tcol['green'], $tcol['blue']);
  7454. imagefill($new_image, 0, 0, $tid);
  7455. imagecolortransparent($new_image, $tid);
  7456. return $new_image;
  7457. }
  7458. /**
  7459. * Extract info from a JPEG file without using the GD library.
  7460. * @param $file (string) image file to parse
  7461. * @return array structure containing the image data
  7462. * @protected
  7463. */
  7464. protected function _parsejpeg($file) {
  7465. $a = getimagesize($file);
  7466. if (empty($a)) {
  7467. $this->Error('Missing or incorrect image file: '.$file);
  7468. }
  7469. if ($a[2] != 2) {
  7470. $this->Error('Not a JPEG file: '.$file);
  7471. }
  7472. // bits per pixel
  7473. $bpc = isset($a['bits']) ? intval($a['bits']) : 8;
  7474. // number of image channels
  7475. if (!isset($a['channels'])) {
  7476. $channels = 3;
  7477. } else {
  7478. $channels = intval($a['channels']);
  7479. }
  7480. // default colour space
  7481. switch ($channels) {
  7482. case 1: {
  7483. $colspace = 'DeviceGray';
  7484. break;
  7485. }
  7486. case 3: {
  7487. $colspace = 'DeviceRGB';
  7488. break;
  7489. }
  7490. case 4: {
  7491. $colspace = 'DeviceCMYK';
  7492. break;
  7493. }
  7494. default: {
  7495. $channels = 3;
  7496. $colspace = 'DeviceRGB';
  7497. break;
  7498. }
  7499. }
  7500. // get file content
  7501. $data = file_get_contents($file);
  7502. // check for embedded ICC profile
  7503. $icc = array();
  7504. $offset = 0;
  7505. while (($pos = strpos($data, "ICC_PROFILE\0", $offset)) !== false) {
  7506. // get ICC sequence length
  7507. $length = ($this->_getUSHORT($data, ($pos - 2)) - 16);
  7508. // marker sequence number
  7509. $msn = max(1, ord($data{($pos + 12)}));
  7510. // number of markers (total of APP2 used)
  7511. $nom = max(1, ord($data{($pos + 13)}));
  7512. // get sequence segment
  7513. $icc[($msn - 1)] = substr($data, ($pos + 14), $length);
  7514. // move forward to next sequence
  7515. $offset = ($pos + 14 + $length);
  7516. }
  7517. // order and compact ICC segments
  7518. if (count($icc) > 0) {
  7519. ksort($icc);
  7520. $icc = implode('', $icc);
  7521. if ((ord($icc{36}) != 0x61) OR (ord($icc{37}) != 0x63) OR (ord($icc{38}) != 0x73) OR (ord($icc{39}) != 0x70)) {
  7522. // invalid ICC profile
  7523. $icc = false;
  7524. }
  7525. } else {
  7526. $icc = false;
  7527. }
  7528. return array('w' => $a[0], 'h' => $a[1], 'ch' => $channels, 'icc' => $icc, 'cs' => $colspace, 'bpc' => $bpc, 'f' => 'DCTDecode', 'data' => $data);
  7529. }
  7530. /**
  7531. * Extract info from a PNG file without using the GD library.
  7532. * @param $file (string) image file to parse
  7533. * @return array structure containing the image data
  7534. * @protected
  7535. */
  7536. protected function _parsepng($file) {
  7537. $f = fopen($file, 'rb');
  7538. if ($f === false) {
  7539. $this->Error('Can\'t open image file: '.$file);
  7540. }
  7541. //Check signature
  7542. if (fread($f, 8) != chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10)) {
  7543. $this->Error('Not a PNG file: '.$file);
  7544. }
  7545. //Read header chunk
  7546. fread($f, 4);
  7547. if (fread($f, 4) != 'IHDR') {
  7548. $this->Error('Incorrect PNG file: '.$file);
  7549. }
  7550. $w = $this->_freadint($f);
  7551. $h = $this->_freadint($f);
  7552. $bpc = ord(fread($f, 1));
  7553. if ($bpc > 8) {
  7554. //$this->Error('16-bit depth not supported: '.$file);
  7555. fclose($f);
  7556. return false;
  7557. }
  7558. $ct = ord(fread($f, 1));
  7559. if ($ct == 0) {
  7560. $colspace = 'DeviceGray';
  7561. } elseif ($ct == 2) {
  7562. $colspace = 'DeviceRGB';
  7563. } elseif ($ct == 3) {
  7564. $colspace = 'Indexed';
  7565. } else {
  7566. // alpha channel
  7567. fclose($f);
  7568. return 'pngalpha';
  7569. }
  7570. if (ord(fread($f, 1)) != 0) {
  7571. //$this->Error('Unknown compression method: '.$file);
  7572. fclose($f);
  7573. return false;
  7574. }
  7575. if (ord(fread($f, 1)) != 0) {
  7576. //$this->Error('Unknown filter method: '.$file);
  7577. fclose($f);
  7578. return false;
  7579. }
  7580. if (ord(fread($f, 1)) != 0) {
  7581. //$this->Error('Interlacing not supported: '.$file);
  7582. fclose($f);
  7583. return false;
  7584. }
  7585. fread($f, 4);
  7586. $channels = ($ct == 2 ? 3 : 1);
  7587. $parms = '/DecodeParms << /Predictor 15 /Colors '.$channels.' /BitsPerComponent '.$bpc.' /Columns '.$w.' >>';
  7588. //Scan chunks looking for palette, transparency and image data
  7589. $pal = '';
  7590. $trns = '';
  7591. $data = '';
  7592. $icc = false;
  7593. do {
  7594. $n = $this->_freadint($f);
  7595. $type = fread($f, 4);
  7596. if ($type == 'PLTE') {
  7597. // read palette
  7598. $pal = $this->rfread($f, $n);
  7599. fread($f, 4);
  7600. } elseif ($type == 'tRNS') {
  7601. // read transparency info
  7602. $t = $this->rfread($f, $n);
  7603. if ($ct == 0) {
  7604. $trns = array(ord($t{1}));
  7605. } elseif ($ct == 2) {
  7606. $trns = array(ord($t{1}), ord($t{3}), ord($t{5}));
  7607. } else {
  7608. $pos = strpos($t, chr(0));
  7609. if ($pos !== false) {
  7610. $trns = array($pos);
  7611. }
  7612. }
  7613. fread($f, 4);
  7614. } elseif ($type == 'IDAT') {
  7615. // read image data block
  7616. $data .= $this->rfread($f, $n);
  7617. fread($f, 4);
  7618. } elseif ($type == 'iCCP') {
  7619. // skip profile name and null separator
  7620. $len = 0;
  7621. while ((ord(fread($f, 1)) > 0) AND ($len < 80)) {
  7622. ++$len;
  7623. }
  7624. // get compression method
  7625. if (ord(fread($f, 1)) != 0) {
  7626. //$this->Error('Unknown filter method: '.$file);
  7627. fclose($f);
  7628. return false;
  7629. }
  7630. // read ICC Color Profile
  7631. $icc = $this->rfread($f, ($n - $len - 2));
  7632. // decompress profile
  7633. $icc = gzuncompress($icc);
  7634. fread($f, 4);
  7635. } elseif ($type == 'IEND') {
  7636. break;
  7637. } else {
  7638. $this->rfread($f, $n + 4);
  7639. }
  7640. } while ($n);
  7641. if (($colspace == 'Indexed') AND (empty($pal))) {
  7642. //$this->Error('Missing palette in '.$file);
  7643. fclose($f);
  7644. return false;
  7645. }
  7646. fclose($f);
  7647. return array('w' => $w, 'h' => $h, 'ch' => $channels, 'icc' => $icc, 'cs' => $colspace, 'bpc' => $bpc, 'f' => 'FlateDecode', 'parms' => $parms, 'pal' => $pal, 'trns' => $trns, 'data' => $data);
  7648. }
  7649. /**
  7650. * Binary-safe and URL-safe file read.
  7651. * 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.
  7652. * @param $handle (resource)
  7653. * @param $length (int)
  7654. * @return Returns the read string or FALSE in case of error.
  7655. * @author Nicola Asuni
  7656. * @protected
  7657. * @since 4.5.027 (2009-03-16)
  7658. */
  7659. protected function rfread($handle, $length) {
  7660. $data = fread($handle, $length);
  7661. if ($data === false) {
  7662. return false;
  7663. }
  7664. $rest = $length - strlen($data);
  7665. if ($rest > 0) {
  7666. $data .= $this->rfread($handle, $rest);
  7667. }
  7668. return $data;
  7669. }
  7670. /**
  7671. * Extract info from a PNG image with alpha channel using the GD library.
  7672. * @param $file (string) Name of the file containing the image.
  7673. * @param $x (float) Abscissa of the upper-left corner.
  7674. * @param $y (float) Ordinate of the upper-left corner.
  7675. * @param $wpx (float) Original width of the image in pixels.
  7676. * @param $hpx (float) original height of the image in pixels.
  7677. * @param $w (float) Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  7678. * @param $h (float) Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  7679. * @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.
  7680. * @param $link (mixed) URL or identifier returned by AddLink().
  7681. * @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>
  7682. * @param $resize (boolean) If true resize (reduce) the image to fit $w and $h (requires GD library).
  7683. * @param $dpi (int) dot-per-inch resolution used on resize
  7684. * @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>
  7685. * @param $filehash (string) File hash used to build unique file names.
  7686. * @author Nicola Asuni
  7687. * @protected
  7688. * @since 4.3.007 (2008-12-04)
  7689. * @see Image()
  7690. */
  7691. protected function ImagePngAlpha($file, $x, $y, $wpx, $hpx, $w, $h, $type, $link, $align, $resize, $dpi, $palign, $filehash='') {
  7692. if (empty($filehash)) {
  7693. $filehash = md5($file);
  7694. }
  7695. // create temp image file (without alpha channel)
  7696. $tempfile_plain = K_PATH_CACHE.'mskp_'.$filehash;
  7697. // create temp alpha file
  7698. $tempfile_alpha = K_PATH_CACHE.'mska_'.$filehash;
  7699. if (extension_loaded('imagick')) { // ImageMagick
  7700. // ImageMagick library
  7701. $img = new Imagick();
  7702. $img->readImage($file);
  7703. // clone image object
  7704. $imga = $img->clone();
  7705. // extract alpha channel
  7706. $img->separateImageChannel(8); // 8 = (imagick::CHANNEL_ALPHA | imagick::CHANNEL_OPACITY | imagick::CHANNEL_MATTE);
  7707. $img->negateImage(true);
  7708. $img->setImageFormat('png');
  7709. $img->writeImage($tempfile_alpha);
  7710. // remove alpha channel
  7711. $imga->separateImageChannel(39); // 39 = (imagick::CHANNEL_ALL & ~(imagick::CHANNEL_ALPHA | imagick::CHANNEL_OPACITY | imagick::CHANNEL_MATTE));
  7712. $imga->setImageFormat('png');
  7713. $imga->writeImage($tempfile_plain);
  7714. } else { // GD library
  7715. // generate images
  7716. $img = imagecreatefrompng($file);
  7717. $imgalpha = imagecreate($wpx, $hpx);
  7718. // generate gray scale palette (0 -> 255)
  7719. for ($c = 0; $c < 256; ++$c) {
  7720. ImageColorAllocate($imgalpha, $c, $c, $c);
  7721. }
  7722. // extract alpha channel
  7723. for ($xpx = 0; $xpx < $wpx; ++$xpx) {
  7724. for ($ypx = 0; $ypx < $hpx; ++$ypx) {
  7725. $color = imagecolorat($img, $xpx, $ypx);
  7726. $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)
  7727. $alpha = (((127 - $alpha) / 127) * 255); // GD alpha is only 7 bit (0 -> 127)
  7728. $alpha = $this->getGDgamma($alpha); // correct gamma
  7729. imagesetpixel($imgalpha, $xpx, $ypx, $alpha);
  7730. }
  7731. }
  7732. imagepng($imgalpha, $tempfile_alpha);
  7733. imagedestroy($imgalpha);
  7734. // extract image without alpha channel
  7735. $imgplain = imagecreatetruecolor($wpx, $hpx);
  7736. imagecopy($imgplain, $img, 0, 0, 0, 0, $wpx, $hpx);
  7737. imagepng($imgplain, $tempfile_plain);
  7738. imagedestroy($imgplain);
  7739. }
  7740. // embed mask image
  7741. $imgmask = $this->Image($tempfile_alpha, $x, $y, $w, $h, 'PNG', '', '', $resize, $dpi, '', true, false);
  7742. // embed image, masked with previously embedded mask
  7743. $this->Image($tempfile_plain, $x, $y, $w, $h, $type, $link, $align, $resize, $dpi, $palign, false, $imgmask);
  7744. // remove temp files
  7745. unlink($tempfile_alpha);
  7746. unlink($tempfile_plain);
  7747. }
  7748. /**
  7749. * Correct the gamma value to be used with GD library
  7750. * @param $v (float) the gamma value to be corrected
  7751. * @protected
  7752. * @since 4.3.007 (2008-12-04)
  7753. */
  7754. protected function getGDgamma($v) {
  7755. return (pow(($v / 255), 2.2) * 255);
  7756. }
  7757. /**
  7758. * Performs a line break.
  7759. * The current abscissa goes back to the left margin and the ordinate increases by the amount passed in parameter.
  7760. * @param $h (float) The height of the break. By default, the value equals the height of the last printed cell.
  7761. * @param $cell (boolean) if true add the current left (or right o for RTL) padding to the X coordinate
  7762. * @public
  7763. * @since 1.0
  7764. * @see Cell()
  7765. */
  7766. public function Ln($h='', $cell=false) {
  7767. 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'])) {
  7768. // revove vertical space from the top of the column
  7769. return;
  7770. }
  7771. if ($cell) {
  7772. if ($this->rtl) {
  7773. $cellpadding = $this->cell_padding['R'];
  7774. } else {
  7775. $cellpadding = $this->cell_padding['L'];
  7776. }
  7777. } else {
  7778. $cellpadding = 0;
  7779. }
  7780. if ($this->rtl) {
  7781. $this->x = $this->w - $this->rMargin - $cellpadding;
  7782. } else {
  7783. $this->x = $this->lMargin + $cellpadding;
  7784. }
  7785. if (is_string($h)) {
  7786. $this->y += $this->lasth;
  7787. } else {
  7788. $this->y += $h;
  7789. }
  7790. $this->newline = true;
  7791. }
  7792. /**
  7793. * Returns the relative X value of current position.
  7794. * The value is relative to the left border for LTR languages and to the right border for RTL languages.
  7795. * @return float
  7796. * @public
  7797. * @since 1.2
  7798. * @see SetX(), GetY(), SetY()
  7799. */
  7800. public function GetX() {
  7801. //Get x position
  7802. if ($this->rtl) {
  7803. return ($this->w - $this->x);
  7804. } else {
  7805. return $this->x;
  7806. }
  7807. }
  7808. /**
  7809. * Returns the absolute X value of current position.
  7810. * @return float
  7811. * @public
  7812. * @since 1.2
  7813. * @see SetX(), GetY(), SetY()
  7814. */
  7815. public function GetAbsX() {
  7816. return $this->x;
  7817. }
  7818. /**
  7819. * Returns the ordinate of the current position.
  7820. * @return float
  7821. * @public
  7822. * @since 1.0
  7823. * @see SetY(), GetX(), SetX()
  7824. */
  7825. public function GetY() {
  7826. return $this->y;
  7827. }
  7828. /**
  7829. * Defines the abscissa of the current position.
  7830. * If the passed value is negative, it is relative to the right of the page (or left if language is RTL).
  7831. * @param $x (float) The value of the abscissa.
  7832. * @param $rtloff (boolean) if true always uses the page top-left corner as origin of axis.
  7833. * @public
  7834. * @since 1.2
  7835. * @see GetX(), GetY(), SetY(), SetXY()
  7836. */
  7837. public function SetX($x, $rtloff=false) {
  7838. if (!$rtloff AND $this->rtl) {
  7839. if ($x >= 0) {
  7840. $this->x = $this->w - $x;
  7841. } else {
  7842. $this->x = abs($x);
  7843. }
  7844. } else {
  7845. if ($x >= 0) {
  7846. $this->x = $x;
  7847. } else {
  7848. $this->x = $this->w + $x;
  7849. }
  7850. }
  7851. if ($this->x < 0) {
  7852. $this->x = 0;
  7853. }
  7854. if ($this->x > $this->w) {
  7855. $this->x = $this->w;
  7856. }
  7857. }
  7858. /**
  7859. * Moves the current abscissa back to the left margin and sets the ordinate.
  7860. * If the passed value is negative, it is relative to the bottom of the page.
  7861. * @param $y (float) The value of the ordinate.
  7862. * @param $resetx (bool) if true (default) reset the X position.
  7863. * @param $rtloff (boolean) if true always uses the page top-left corner as origin of axis.
  7864. * @public
  7865. * @since 1.0
  7866. * @see GetX(), GetY(), SetY(), SetXY()
  7867. */
  7868. public function SetY($y, $resetx=true, $rtloff=false) {
  7869. if ($resetx) {
  7870. //reset x
  7871. if (!$rtloff AND $this->rtl) {
  7872. $this->x = $this->w - $this->rMargin;
  7873. } else {
  7874. $this->x = $this->lMargin;
  7875. }
  7876. }
  7877. if ($y >= 0) {
  7878. $this->y = $y;
  7879. } else {
  7880. $this->y = $this->h + $y;
  7881. }
  7882. if ($this->y < 0) {
  7883. $this->y = 0;
  7884. }
  7885. if ($this->y > $this->h) {
  7886. $this->y = $this->h;
  7887. }
  7888. }
  7889. /**
  7890. * Defines the abscissa and ordinate of the current position.
  7891. * If the passed values are negative, they are relative respectively to the right and bottom of the page.
  7892. * @param $x (float) The value of the abscissa.
  7893. * @param $y (float) The value of the ordinate.
  7894. * @param $rtloff (boolean) if true always uses the page top-left corner as origin of axis.
  7895. * @public
  7896. * @since 1.2
  7897. * @see SetX(), SetY()
  7898. */
  7899. public function SetXY($x, $y, $rtloff=false) {
  7900. $this->SetY($y, false, $rtloff);
  7901. $this->SetX($x, $rtloff);
  7902. }
  7903. /**
  7904. * Ouput input data and compress it if possible.
  7905. * @param $data (string) Data to output.
  7906. * @param $length (int) Data length in bytes.
  7907. * @protected
  7908. * @since 5.9.086
  7909. */
  7910. protected function sendOutputData($data, $length) {
  7911. if (!isset($_SERVER['HTTP_ACCEPT_ENCODING']) OR empty($_SERVER['HTTP_ACCEPT_ENCODING'])) {
  7912. // the content length may vary if the server is using compression
  7913. header('Content-Length: '.$length);
  7914. }
  7915. echo $data;
  7916. }
  7917. /**
  7918. * Send the document to a given destination: string, local file or browser.
  7919. * In the last case, the plug-in may be used (if present) or a download ("Save as" dialog box) may be forced.<br />
  7920. * The method first calls Close() if necessary to terminate the document.
  7921. * @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.
  7922. * @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>
  7923. * @public
  7924. * @since 1.0
  7925. * @see Close()
  7926. */
  7927. public function Output($name='doc.pdf', $dest='I') {
  7928. //Output PDF to some destination
  7929. //Finish document if necessary
  7930. if ($this->state < 3) {
  7931. $this->Close();
  7932. }
  7933. //Normalize parameters
  7934. if (is_bool($dest)) {
  7935. $dest = $dest ? 'D' : 'F';
  7936. }
  7937. $dest = strtoupper($dest);
  7938. if ($dest{0} != 'F') {
  7939. $name = preg_replace('/[\s]+/', '_', $name);
  7940. $name = preg_replace('/[^a-zA-Z0-9_\.-]/', '', $name);
  7941. }
  7942. if ($this->sign) {
  7943. // *** apply digital signature to the document ***
  7944. // get the document content
  7945. $pdfdoc = $this->getBuffer();
  7946. // remove last newline
  7947. $pdfdoc = substr($pdfdoc, 0, -1);
  7948. // Remove the original buffer
  7949. if (isset($this->diskcache) AND $this->diskcache) {
  7950. // remove buffer file from cache
  7951. unlink($this->buffer);
  7952. }
  7953. unset($this->buffer);
  7954. // remove filler space
  7955. $byterange_string_len = strlen($this->byterange_string);
  7956. // define the ByteRange
  7957. $byte_range = array();
  7958. $byte_range[0] = 0;
  7959. $byte_range[1] = strpos($pdfdoc, $this->byterange_string) + $byterange_string_len + 10;
  7960. $byte_range[2] = $byte_range[1] + $this->signature_max_length + 2;
  7961. $byte_range[3] = strlen($pdfdoc) - $byte_range[2];
  7962. $pdfdoc = substr($pdfdoc, 0, $byte_range[1]).substr($pdfdoc, $byte_range[2]);
  7963. // replace the ByteRange
  7964. $byterange = sprintf('/ByteRange[0 %u %u %u]', $byte_range[1], $byte_range[2], $byte_range[3]);
  7965. $byterange .= str_repeat(' ', ($byterange_string_len - strlen($byterange)));
  7966. $pdfdoc = str_replace($this->byterange_string, $byterange, $pdfdoc);
  7967. // write the document to a temporary folder
  7968. $tempdoc = tempnam(K_PATH_CACHE, 'tmppdf_');
  7969. $f = fopen($tempdoc, 'wb');
  7970. if (!$f) {
  7971. $this->Error('Unable to create temporary file: '.$tempdoc);
  7972. }
  7973. $pdfdoc_length = strlen($pdfdoc);
  7974. fwrite($f, $pdfdoc, $pdfdoc_length);
  7975. fclose($f);
  7976. // get digital signature via openssl library
  7977. $tempsign = tempnam(K_PATH_CACHE, 'tmpsig_');
  7978. if (empty($this->signature_data['extracerts'])) {
  7979. openssl_pkcs7_sign($tempdoc, $tempsign, $this->signature_data['signcert'], array($this->signature_data['privkey'], $this->signature_data['password']), array(), PKCS7_BINARY | PKCS7_DETACHED);
  7980. } else {
  7981. 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']);
  7982. }
  7983. unlink($tempdoc);
  7984. // read signature
  7985. $signature = file_get_contents($tempsign);
  7986. unlink($tempsign);
  7987. // extract signature
  7988. $signature = substr($signature, $pdfdoc_length);
  7989. $signature = substr($signature, (strpos($signature, "%%EOF\n\n------") + 13));
  7990. $tmparr = explode("\n\n", $signature);
  7991. $signature = $tmparr[1];
  7992. unset($tmparr);
  7993. // decode signature
  7994. $signature = base64_decode(trim($signature));
  7995. // convert signature to hex
  7996. $signature = current(unpack('H*', $signature));
  7997. $signature = str_pad($signature, $this->signature_max_length, '0');
  7998. // disable disk caching
  7999. $this->diskcache = false;
  8000. // Add signature to the document
  8001. $this->buffer = substr($pdfdoc, 0, $byte_range[1]).'<'.$signature.'>'.substr($pdfdoc, $byte_range[1]);
  8002. $this->bufferlen = strlen($this->buffer);
  8003. }
  8004. switch($dest) {
  8005. case 'I': {
  8006. // Send PDF to the standard output
  8007. if (ob_get_contents()) {
  8008. $this->Error('Some data has already been output, can\'t send PDF file');
  8009. }
  8010. if (php_sapi_name() != 'cli') {
  8011. // send output to a browser
  8012. header('Content-Type: application/pdf');
  8013. if (headers_sent()) {
  8014. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  8015. }
  8016. header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  8017. header('Pragma: public');
  8018. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  8019. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  8020. header('Content-Disposition: inline; filename="'.basename($name).'";');
  8021. $this->sendOutputData($this->getBuffer(), $this->bufferlen);
  8022. } else {
  8023. echo $this->getBuffer();
  8024. }
  8025. break;
  8026. }
  8027. case 'D': {
  8028. // download PDF as file
  8029. if (ob_get_contents()) {
  8030. $this->Error('Some data has already been output, can\'t send PDF file');
  8031. }
  8032. header('Content-Description: File Transfer');
  8033. if (headers_sent()) {
  8034. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  8035. }
  8036. header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  8037. header('Pragma: public');
  8038. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  8039. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  8040. // force download dialog
  8041. if (strpos(php_sapi_name(), 'cgi') === false) {
  8042. header('Content-Type: application/force-download');
  8043. header('Content-Type: application/octet-stream', false);
  8044. header('Content-Type: application/download', false);
  8045. header('Content-Type: application/pdf', false);
  8046. } else {
  8047. header('Content-Type: application/pdf');
  8048. }
  8049. // use the Content-Disposition header to supply a recommended filename
  8050. header('Content-Disposition: attachment; filename="'.basename($name).'";');
  8051. header('Content-Transfer-Encoding: binary');
  8052. $this->sendOutputData($this->getBuffer(), $this->bufferlen);
  8053. break;
  8054. }
  8055. case 'F':
  8056. case 'FI':
  8057. case 'FD': {
  8058. // save PDF to a local file
  8059. if ($this->diskcache) {
  8060. copy($this->buffer, $name);
  8061. } else {
  8062. $f = fopen($name, 'wb');
  8063. if (!$f) {
  8064. $this->Error('Unable to create output file: '.$name);
  8065. }
  8066. fwrite($f, $this->getBuffer(), $this->bufferlen);
  8067. fclose($f);
  8068. }
  8069. if ($dest == 'FI') {
  8070. // send headers to browser
  8071. header('Content-Type: application/pdf');
  8072. header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  8073. header('Pragma: public');
  8074. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  8075. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  8076. header('Content-Disposition: inline; filename="'.basename($name).'";');
  8077. $this->sendOutputData(file_get_contents($name), filesize($name));
  8078. } elseif ($dest == 'FD') {
  8079. // send headers to browser
  8080. if (ob_get_contents()) {
  8081. $this->Error('Some data has already been output, can\'t send PDF file');
  8082. }
  8083. header('Content-Description: File Transfer');
  8084. if (headers_sent()) {
  8085. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  8086. }
  8087. header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  8088. header('Pragma: public');
  8089. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  8090. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  8091. // force download dialog
  8092. if (strpos(php_sapi_name(), 'cgi') === false) {
  8093. header('Content-Type: application/force-download');
  8094. header('Content-Type: application/octet-stream', false);
  8095. header('Content-Type: application/download', false);
  8096. header('Content-Type: application/pdf', false);
  8097. } else {
  8098. header('Content-Type: application/pdf');
  8099. }
  8100. // use the Content-Disposition header to supply a recommended filename
  8101. header('Content-Disposition: attachment; filename="'.basename($name).'";');
  8102. header('Content-Transfer-Encoding: binary');
  8103. $this->sendOutputData(file_get_contents($name), filesize($name));
  8104. }
  8105. break;
  8106. }
  8107. case 'E': {
  8108. // return PDF as base64 mime multi-part email attachment (RFC 2045)
  8109. $retval = 'Content-Type: application/pdf;'."\r\n";
  8110. $retval .= ' name="'.$name.'"'."\r\n";
  8111. $retval .= 'Content-Transfer-Encoding: base64'."\r\n";
  8112. $retval .= 'Content-Disposition: attachment;'."\r\n";
  8113. $retval .= ' filename="'.$name.'"'."\r\n\r\n";
  8114. $retval .= chunk_split(base64_encode($this->getBuffer()), 76, "\r\n");
  8115. return $retval;
  8116. }
  8117. case 'S': {
  8118. // returns PDF as a string
  8119. return $this->getBuffer();
  8120. }
  8121. default: {
  8122. $this->Error('Incorrect output destination: '.$dest);
  8123. }
  8124. }
  8125. return '';
  8126. }
  8127. /**
  8128. * Unset all class variables except the following critical variables: internal_encoding, state, bufferlen, buffer and diskcache.
  8129. * @param $destroyall (boolean) if true destroys all class variables, otherwise preserves critical variables.
  8130. * @param $preserve_objcopy (boolean) if true preserves the objcopy variable
  8131. * @public
  8132. * @since 4.5.016 (2009-02-24)
  8133. */
  8134. public function _destroy($destroyall=false, $preserve_objcopy=false) {
  8135. if ($destroyall AND isset($this->diskcache) AND $this->diskcache AND (!$preserve_objcopy) AND (!$this->empty_string($this->buffer))) {
  8136. // remove buffer file from cache
  8137. unlink($this->buffer);
  8138. }
  8139. foreach (array_keys(get_object_vars($this)) as $val) {
  8140. if ($destroyall OR (
  8141. ($val != 'internal_encoding')
  8142. AND ($val != 'state')
  8143. AND ($val != 'bufferlen')
  8144. AND ($val != 'buffer')
  8145. AND ($val != 'diskcache')
  8146. AND ($val != 'sign')
  8147. AND ($val != 'signature_data')
  8148. AND ($val != 'signature_max_length')
  8149. AND ($val != 'byterange_string')
  8150. )) {
  8151. if ((!$preserve_objcopy OR ($val != 'objcopy')) AND isset($this->$val)) {
  8152. unset($this->$val);
  8153. }
  8154. }
  8155. }
  8156. }
  8157. /**
  8158. * Check for locale-related bug
  8159. * @protected
  8160. */
  8161. protected function _dochecks() {
  8162. //Check for locale-related bug
  8163. if (1.1 == 1) {
  8164. $this->Error('Don\'t alter the locale before including class file');
  8165. }
  8166. //Check for decimal separator
  8167. if (sprintf('%.1F', 1.0) != '1.0') {
  8168. setlocale(LC_NUMERIC, 'C');
  8169. }
  8170. }
  8171. /**
  8172. * Return fonts path
  8173. * @return string
  8174. * @protected
  8175. */
  8176. protected function _getfontpath() {
  8177. if (!defined('K_PATH_FONTS') AND is_dir(dirname(__FILE__).'/fonts')) {
  8178. define('K_PATH_FONTS', dirname(__FILE__).'/fonts/');
  8179. }
  8180. return defined('K_PATH_FONTS') ? K_PATH_FONTS : '';
  8181. }
  8182. /**
  8183. * Return an array containing variations for the basic page number alias.
  8184. * @param $a (string) Base alias.
  8185. * @return array of page number aliases
  8186. * @protected
  8187. */
  8188. protected function getInternalPageNumberAliases($a= '') {
  8189. $alias = array();
  8190. // build array of Unicode + ASCII variants (the order is important)
  8191. $alias = array('u' => array(), 'a' => array());
  8192. $u = '{'.$a.'}';
  8193. $alias['u'][] = $this->_escape($u);
  8194. if ($this->isunicode) {
  8195. $alias['u'][] = $this->_escape($this->UTF8ToLatin1($u));
  8196. $alias['u'][] = $this->_escape($this->utf8StrRev($u, false, $this->tmprtl));
  8197. $alias['a'][] = $this->_escape($this->UTF8ToLatin1($a));
  8198. $alias['a'][] = $this->_escape($this->utf8StrRev($a, false, $this->tmprtl));
  8199. }
  8200. $alias['a'][] = $this->_escape($a);
  8201. return $alias;
  8202. }
  8203. /**
  8204. * Return an array containing all internal page aliases.
  8205. * @return array of page number aliases
  8206. * @protected
  8207. */
  8208. protected function getAllInternalPageNumberAliases() {
  8209. $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);
  8210. $pnalias = array();
  8211. foreach($basic_alias as $k => $a) {
  8212. $pnalias[$k] = $this->getInternalPageNumberAliases($a);
  8213. }
  8214. return $pnalias;
  8215. }
  8216. /**
  8217. * Replace page number aliases with number.
  8218. * @param $page (string) Page content.
  8219. * @param $replace (array) Array of replacements (array keys are replacement strings, values are alias arrays).
  8220. * @param $diff (int) If passed, this will be set to the total char number difference between alias and replacements.
  8221. * @return replaced page content and updated $diff parameter as array.
  8222. * @protected
  8223. */
  8224. protected function replacePageNumAliases($page, $replace, $diff=0) {
  8225. foreach ($replace as $rep) {
  8226. foreach ($rep[3] as $a) {
  8227. if (strpos($page, $a) !== false) {
  8228. $page = str_replace($a, $rep[0], $page);
  8229. $diff += ($rep[2] - $rep[1]);
  8230. }
  8231. }
  8232. }
  8233. return array($page, $diff);
  8234. }
  8235. /**
  8236. * Replace right shift page number aliases with spaces to correct right alignment.
  8237. * This works perfectly only when using monospaced fonts.
  8238. * @param $page (string) Page content.
  8239. * @param $aliases (array) Array of page aliases.
  8240. * @param $diff (int) initial difference to add.
  8241. * @return replaced page content.
  8242. * @protected
  8243. */
  8244. protected function replaceRightShiftPageNumAliases($page, $aliases, $diff) {
  8245. foreach ($aliases as $type => $alias) {
  8246. foreach ($alias as $a) {
  8247. // find position of compensation factor
  8248. $startnum = (strpos($a, ':') + 1);
  8249. $a = substr($a, 0, $startnum);
  8250. if (($pos = strpos($page, $a)) !== false) {
  8251. // end of alias
  8252. $endnum = strpos($page, '}', $pos);
  8253. // string to be replaced
  8254. $aa = substr($page, $pos, ($endnum - $pos + 1));
  8255. // get compensation factor
  8256. $ratio = substr($page, ($pos + $startnum), ($endnum - $pos - $startnum));
  8257. $ratio = preg_replace('/[^0-9\.]/', '', $ratio);
  8258. $ratio = floatval($ratio);
  8259. if ($type == 'u') {
  8260. $chrdiff = floor(($diff + 12) * $ratio);
  8261. $shift = str_repeat(' ', $chrdiff);
  8262. $shift = $this->UTF8ToUTF16BE($shift, false);
  8263. } else {
  8264. $chrdiff = floor(($diff + 11) * $ratio);
  8265. $shift = str_repeat(' ', $chrdiff);
  8266. }
  8267. $page = str_replace($aa, $shift, $page);
  8268. }
  8269. }
  8270. }
  8271. return $page;
  8272. }
  8273. /**
  8274. * Output pages (and replace page number aliases).
  8275. * @protected
  8276. */
  8277. protected function _putpages() {
  8278. $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
  8279. // get internal aliases for page numbers
  8280. $pnalias = $this->getAllInternalPageNumberAliases();
  8281. $num_pages = $this->numpages;
  8282. $ptpa = $this->formatPageNumber(($this->starting_page_number + $num_pages - 1));
  8283. $ptpu = $this->UTF8ToUTF16BE($ptpa, false);
  8284. $ptp_num_chars = $this->GetNumChars($ptpa);
  8285. $pagegroupnum = 0;
  8286. $groupnum = 0;
  8287. $ptgu = 1;
  8288. $ptga = 1;
  8289. for ($n = 1; $n <= $num_pages; ++$n) {
  8290. // get current page
  8291. $temppage = $this->getPageBuffer($n);
  8292. $pagelen = strlen($temppage);
  8293. // set replacements for total pages number
  8294. $pnpa = $this->formatPageNumber(($this->starting_page_number + $n - 1));
  8295. $pnpu = $this->UTF8ToUTF16BE($pnpa, false);
  8296. $pnp_num_chars = $this->GetNumChars($pnpa);
  8297. $pdiff = 0; // difference used for right shift alignment of page numbers
  8298. $gdiff = 0; // difference used for right shift alignment of page group numbers
  8299. if (!empty($this->pagegroups)) {
  8300. if (isset($this->newpagegroup[$n])) {
  8301. $pagegroupnum = 0;
  8302. ++$groupnum;
  8303. $ptga = $this->formatPageNumber($this->pagegroups[$groupnum]);
  8304. $ptgu = $this->UTF8ToUTF16BE($ptga, false);
  8305. $ptg_num_chars = $this->GetNumChars($ptga);
  8306. }
  8307. ++$pagegroupnum;
  8308. $pnga = $this->formatPageNumber($pagegroupnum);
  8309. $pngu = $this->UTF8ToUTF16BE($pnga, false);
  8310. $png_num_chars = $this->GetNumChars($pnga);
  8311. // replace page numbers
  8312. $replace = array();
  8313. $replace[] = array($ptgu, $ptg_num_chars, 9, $pnalias[2]['u']);
  8314. $replace[] = array($ptga, $ptg_num_chars, 7, $pnalias[2]['a']);
  8315. $replace[] = array($pngu, $png_num_chars, 9, $pnalias[3]['u']);
  8316. $replace[] = array($pnga, $png_num_chars, 7, $pnalias[3]['a']);
  8317. list($temppage, $gdiff) = $this->replacePageNumAliases($temppage, $replace, $gdiff);
  8318. }
  8319. // replace page numbers
  8320. $replace = array();
  8321. $replace[] = array($ptpu, $ptp_num_chars, 9, $pnalias[0]['u']);
  8322. $replace[] = array($ptpa, $ptp_num_chars, 7, $pnalias[0]['a']);
  8323. $replace[] = array($pnpu, $pnp_num_chars, 9, $pnalias[1]['u']);
  8324. $replace[] = array($pnpa, $pnp_num_chars, 7, $pnalias[1]['a']);
  8325. list($temppage, $pdiff) = $this->replacePageNumAliases($temppage, $replace, $pdiff);
  8326. // replace right shift alias
  8327. $temppage = $this->replaceRightShiftPageNumAliases($temppage, $pnalias[4], max($pdiff, $gdiff));
  8328. // replace EPS marker
  8329. $temppage = str_replace($this->epsmarker, '', $temppage);
  8330. //Page
  8331. $this->page_obj_id[$n] = $this->_newobj();
  8332. $out = '<<';
  8333. $out .= ' /Type /Page';
  8334. $out .= ' /Parent 1 0 R';
  8335. $out .= ' /LastModified '.$this->_datestring();
  8336. $out .= ' /Resources 2 0 R';
  8337. $boxes = array('MediaBox', 'CropBox', 'BleedBox', 'TrimBox', 'ArtBox');
  8338. foreach ($boxes as $box) {
  8339. $out .= ' /'.$box;
  8340. $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']);
  8341. }
  8342. if (isset($this->pagedim[$n]['BoxColorInfo']) AND !empty($this->pagedim[$n]['BoxColorInfo'])) {
  8343. $out .= ' /BoxColorInfo <<';
  8344. foreach ($boxes as $box) {
  8345. if (isset($this->pagedim[$n]['BoxColorInfo'][$box])) {
  8346. $out .= ' /'.$box.' <<';
  8347. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['C'])) {
  8348. $color = $this->pagedim[$n]['BoxColorInfo'][$box]['C'];
  8349. $out .= ' /C [';
  8350. $out .= sprintf(' %.3F %.3F %.3F', $color[0]/255, $color[1]/255, $color[2]/255);
  8351. $out .= ' ]';
  8352. }
  8353. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['W'])) {
  8354. $out .= ' /W '.($this->pagedim[$n]['BoxColorInfo'][$box]['W'] * $this->k);
  8355. }
  8356. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['S'])) {
  8357. $out .= ' /S /'.$this->pagedim[$n]['BoxColorInfo'][$box]['S'];
  8358. }
  8359. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['D'])) {
  8360. $dashes = $this->pagedim[$n]['BoxColorInfo'][$box]['D'];
  8361. $out .= ' /D [';
  8362. foreach ($dashes as $dash) {
  8363. $out .= sprintf(' %.3F', ($dash * $this->k));
  8364. }
  8365. $out .= ' ]';
  8366. }
  8367. $out .= ' >>';
  8368. }
  8369. }
  8370. $out .= ' >>';
  8371. }
  8372. $out .= ' /Contents '.($this->n + 1).' 0 R';
  8373. $out .= ' /Rotate '.$this->pagedim[$n]['Rotate'];
  8374. $out .= ' /Group << /Type /Group /S /Transparency /CS /DeviceRGB >>';
  8375. if (isset($this->pagedim[$n]['trans']) AND !empty($this->pagedim[$n]['trans'])) {
  8376. // page transitions
  8377. if (isset($this->pagedim[$n]['trans']['Dur'])) {
  8378. $out .= ' /Dur '.$this->pagedim[$n]['trans']['Dur'];
  8379. }
  8380. $out .= ' /Trans <<';
  8381. $out .= ' /Type /Trans';
  8382. if (isset($this->pagedim[$n]['trans']['S'])) {
  8383. $out .= ' /S /'.$this->pagedim[$n]['trans']['S'];
  8384. }
  8385. if (isset($this->pagedim[$n]['trans']['D'])) {
  8386. $out .= ' /D '.$this->pagedim[$n]['trans']['D'];
  8387. }
  8388. if (isset($this->pagedim[$n]['trans']['Dm'])) {
  8389. $out .= ' /Dm /'.$this->pagedim[$n]['trans']['Dm'];
  8390. }
  8391. if (isset($this->pagedim[$n]['trans']['M'])) {
  8392. $out .= ' /M /'.$this->pagedim[$n]['trans']['M'];
  8393. }
  8394. if (isset($this->pagedim[$n]['trans']['Di'])) {
  8395. $out .= ' /Di '.$this->pagedim[$n]['trans']['Di'];
  8396. }
  8397. if (isset($this->pagedim[$n]['trans']['SS'])) {
  8398. $out .= ' /SS '.$this->pagedim[$n]['trans']['SS'];
  8399. }
  8400. if (isset($this->pagedim[$n]['trans']['B'])) {
  8401. $out .= ' /B '.$this->pagedim[$n]['trans']['B'];
  8402. }
  8403. $out .= ' >>';
  8404. }
  8405. $out .= $this->_getannotsrefs($n);
  8406. $out .= ' /PZ '.$this->pagedim[$n]['PZ'];
  8407. $out .= ' >>';
  8408. $out .= "\n".'endobj';
  8409. $this->_out($out);
  8410. //Page content
  8411. $p = ($this->compress) ? gzcompress($temppage) : $temppage;
  8412. $this->_newobj();
  8413. $p = $this->_getrawstream($p);
  8414. $this->_out('<<'.$filter.'/Length '.strlen($p).'>> stream'."\n".$p."\n".'endstream'."\n".'endobj');
  8415. if ($this->diskcache) {
  8416. // remove temporary files
  8417. unlink($this->pages[$n]);
  8418. }
  8419. }
  8420. //Pages root
  8421. $out = $this->_getobj(1)."\n";
  8422. $out .= '<< /Type /Pages /Kids [';
  8423. foreach($this->page_obj_id as $page_obj) {
  8424. $out .= ' '.$page_obj.' 0 R';
  8425. }
  8426. $out .= ' ] /Count '.$num_pages.' >>';
  8427. $out .= "\n".'endobj';
  8428. $this->_out($out);
  8429. }
  8430. /**
  8431. * Output references to page annotations
  8432. * @param $n (int) page number
  8433. * @protected
  8434. * @author Nicola Asuni
  8435. * @since 4.7.000 (2008-08-29)
  8436. * @deprecated
  8437. */
  8438. protected function _putannotsrefs($n) {
  8439. $this->_out($this->_getannotsrefs($n));
  8440. }
  8441. /**
  8442. * Get references to page annotations.
  8443. * @param $n (int) page number
  8444. * @return string
  8445. * @protected
  8446. * @author Nicola Asuni
  8447. * @since 5.0.010 (2010-05-17)
  8448. */
  8449. protected function _getannotsrefs($n) {
  8450. if (!(isset($this->PageAnnots[$n]) OR ($this->sign AND isset($this->signature_data['cert_type'])))) {
  8451. return '';
  8452. }
  8453. $out = ' /Annots [';
  8454. if (isset($this->PageAnnots[$n])) {
  8455. foreach ($this->PageAnnots[$n] as $key => $val) {
  8456. if (!in_array($val['n'], $this->radio_groups)) {
  8457. $out .= ' '.$val['n'].' 0 R';
  8458. }
  8459. }
  8460. // add radiobutton groups
  8461. if (isset($this->radiobutton_groups[$n])) {
  8462. foreach ($this->radiobutton_groups[$n] as $key => $data) {
  8463. if (isset($data['n'])) {
  8464. $out .= ' '.$data['n'].' 0 R';
  8465. }
  8466. }
  8467. }
  8468. }
  8469. if ($this->sign AND ($n == $this->signature_appearance['page']) AND isset($this->signature_data['cert_type'])) {
  8470. // set reference for signature object
  8471. $out .= ' '.$this->sig_obj_id.' 0 R';
  8472. }
  8473. if (!empty($this->empty_signature_appearance)) {
  8474. foreach ($this->empty_signature_appearance as $esa) {
  8475. if ($esa['page'] == $n) {
  8476. // set reference for empty signature objects
  8477. $out .= ' '.$esa['objid'].' 0 R';
  8478. }
  8479. }
  8480. }
  8481. $out .= ' ]';
  8482. return $out;
  8483. }
  8484. /**
  8485. * Output annotations objects for all pages.
  8486. * !!! THIS METHOD IS NOT YET COMPLETED !!!
  8487. * See section 12.5 of PDF 32000_2008 reference.
  8488. * @protected
  8489. * @author Nicola Asuni
  8490. * @since 4.0.018 (2008-08-06)
  8491. */
  8492. protected function _putannotsobjs() {
  8493. // reset object counter
  8494. for ($n=1; $n <= $this->numpages; ++$n) {
  8495. if (isset($this->PageAnnots[$n])) {
  8496. // set page annotations
  8497. foreach ($this->PageAnnots[$n] as $key => $pl) {
  8498. $annot_obj_id = $this->PageAnnots[$n][$key]['n'];
  8499. // create annotation object for grouping radiobuttons
  8500. if (isset($this->radiobutton_groups[$n][$pl['txt']]) AND is_array($this->radiobutton_groups[$n][$pl['txt']])) {
  8501. $radio_button_obj_id = $this->radiobutton_groups[$n][$pl['txt']]['n'];
  8502. $annots = '<<';
  8503. $annots .= ' /Type /Annot';
  8504. $annots .= ' /Subtype /Widget';
  8505. $annots .= ' /Rect [0 0 0 0]';
  8506. $annots .= ' /T '.$this->_datastring($pl['txt'], $radio_button_obj_id);
  8507. $annots .= ' /FT /Btn';
  8508. $annots .= ' /Ff 49152';
  8509. $annots .= ' /Kids [';
  8510. foreach ($this->radiobutton_groups[$n][$pl['txt']] as $key => $data) {
  8511. if ($key !== 'n') {
  8512. $annots .= ' '.$data['kid'].' 0 R';
  8513. if ($data['def'] !== 'Off') {
  8514. $defval = $data['def'];
  8515. }
  8516. }
  8517. }
  8518. $annots .= ' ]';
  8519. if (isset($defval)) {
  8520. $annots .= ' /V /'.$defval;
  8521. }
  8522. $annots .= ' >>';
  8523. $this->_out($this->_getobj($radio_button_obj_id)."\n".$annots."\n".'endobj');
  8524. $this->form_obj_id[] = $radio_button_obj_id;
  8525. // store object id to be used on Parent entry of Kids
  8526. $this->radiobutton_groups[$n][$pl['txt']] = $radio_button_obj_id;
  8527. }
  8528. $formfield = false;
  8529. $pl['opt'] = array_change_key_case($pl['opt'], CASE_LOWER);
  8530. $a = $pl['x'] * $this->k;
  8531. $b = $this->pagedim[$n]['h'] - (($pl['y'] + $pl['h']) * $this->k);
  8532. $c = $pl['w'] * $this->k;
  8533. $d = $pl['h'] * $this->k;
  8534. $rect = sprintf('%.2F %.2F %.2F %.2F', $a, $b, $a+$c, $b+$d);
  8535. // create new annotation object
  8536. $annots = '<</Type /Annot';
  8537. $annots .= ' /Subtype /'.$pl['opt']['subtype'];
  8538. $annots .= ' /Rect ['.$rect.']';
  8539. $ft = array('Btn', 'Tx', 'Ch', 'Sig');
  8540. if (isset($pl['opt']['ft']) AND in_array($pl['opt']['ft'], $ft)) {
  8541. $annots .= ' /FT /'.$pl['opt']['ft'];
  8542. $formfield = true;
  8543. }
  8544. $annots .= ' /Contents '.$this->_textstring($pl['txt'], $annot_obj_id);
  8545. $annots .= ' /P '.$this->page_obj_id[$n].' 0 R';
  8546. $annots .= ' /NM '.$this->_datastring(sprintf('%04u-%04u', $n, $key), $annot_obj_id);
  8547. $annots .= ' /M '.$this->_datestring($annot_obj_id);
  8548. if (isset($pl['opt']['f'])) {
  8549. $val = 0;
  8550. if (is_array($pl['opt']['f'])) {
  8551. foreach ($pl['opt']['f'] as $f) {
  8552. switch (strtolower($f)) {
  8553. case 'invisible': {
  8554. $val += 1 << 0;
  8555. break;
  8556. }
  8557. case 'hidden': {
  8558. $val += 1 << 1;
  8559. break;
  8560. }
  8561. case 'print': {
  8562. $val += 1 << 2;
  8563. break;
  8564. }
  8565. case 'nozoom': {
  8566. $val += 1 << 3;
  8567. break;
  8568. }
  8569. case 'norotate': {
  8570. $val += 1 << 4;
  8571. break;
  8572. }
  8573. case 'noview': {
  8574. $val += 1 << 5;
  8575. break;
  8576. }
  8577. case 'readonly': {
  8578. $val += 1 << 6;
  8579. break;
  8580. }
  8581. case 'locked': {
  8582. $val += 1 << 8;
  8583. break;
  8584. }
  8585. case 'togglenoview': {
  8586. $val += 1 << 9;
  8587. break;
  8588. }
  8589. case 'lockedcontents': {
  8590. $val += 1 << 10;
  8591. break;
  8592. }
  8593. default: {
  8594. break;
  8595. }
  8596. }
  8597. }
  8598. } else {
  8599. $val = intval($pl['opt']['f']);
  8600. }
  8601. $annots .= ' /F '.intval($val);
  8602. }
  8603. if (isset($pl['opt']['as']) AND is_string($pl['opt']['as'])) {
  8604. $annots .= ' /AS /'.$pl['opt']['as'];
  8605. }
  8606. if (isset($pl['opt']['ap'])) {
  8607. // appearance stream
  8608. $annots .= ' /AP <<';
  8609. if (is_array($pl['opt']['ap'])) {
  8610. foreach ($pl['opt']['ap'] as $apmode => $apdef) {
  8611. // $apmode can be: n = normal; r = rollover; d = down;
  8612. $annots .= ' /'.strtoupper($apmode);
  8613. if (is_array($apdef)) {
  8614. $annots .= ' <<';
  8615. foreach ($apdef as $apstate => $stream) {
  8616. // reference to XObject that define the appearance for this mode-state
  8617. $apsobjid = $this->_putAPXObject($c, $d, $stream);
  8618. $annots .= ' /'.$apstate.' '.$apsobjid.' 0 R';
  8619. }
  8620. $annots .= ' >>';
  8621. } else {
  8622. // reference to XObject that define the appearance for this mode
  8623. $apsobjid = $this->_putAPXObject($c, $d, $apdef);
  8624. $annots .= ' '.$apsobjid.' 0 R';
  8625. }
  8626. }
  8627. } else {
  8628. $annots .= $pl['opt']['ap'];
  8629. }
  8630. $annots .= ' >>';
  8631. }
  8632. if (isset($pl['opt']['bs']) AND (is_array($pl['opt']['bs']))) {
  8633. $annots .= ' /BS <<';
  8634. $annots .= ' /Type /Border';
  8635. if (isset($pl['opt']['bs']['w'])) {
  8636. $annots .= ' /W '.intval($pl['opt']['bs']['w']);
  8637. }
  8638. $bstyles = array('S', 'D', 'B', 'I', 'U');
  8639. if (isset($pl['opt']['bs']['s']) AND in_array($pl['opt']['bs']['s'], $bstyles)) {
  8640. $annots .= ' /S /'.$pl['opt']['bs']['s'];
  8641. }
  8642. if (isset($pl['opt']['bs']['d']) AND (is_array($pl['opt']['bs']['d']))) {
  8643. $annots .= ' /D [';
  8644. foreach ($pl['opt']['bs']['d'] as $cord) {
  8645. $annots .= ' '.intval($cord);
  8646. }
  8647. $annots .= ']';
  8648. }
  8649. $annots .= ' >>';
  8650. } else {
  8651. $annots .= ' /Border [';
  8652. if (isset($pl['opt']['border']) AND (count($pl['opt']['border']) >= 3)) {
  8653. $annots .= intval($pl['opt']['border'][0]).' ';
  8654. $annots .= intval($pl['opt']['border'][1]).' ';
  8655. $annots .= intval($pl['opt']['border'][2]);
  8656. if (isset($pl['opt']['border'][3]) AND is_array($pl['opt']['border'][3])) {
  8657. $annots .= ' [';
  8658. foreach ($pl['opt']['border'][3] as $dash) {
  8659. $annots .= intval($dash).' ';
  8660. }
  8661. $annots .= ']';
  8662. }
  8663. } else {
  8664. $annots .= '0 0 0';
  8665. }
  8666. $annots .= ']';
  8667. }
  8668. if (isset($pl['opt']['be']) AND (is_array($pl['opt']['be']))) {
  8669. $annots .= ' /BE <<';
  8670. $bstyles = array('S', 'C');
  8671. if (isset($pl['opt']['be']['s']) AND in_array($pl['opt']['be']['s'], $markups)) {
  8672. $annots .= ' /S /'.$pl['opt']['bs']['s'];
  8673. } else {
  8674. $annots .= ' /S /S';
  8675. }
  8676. if (isset($pl['opt']['be']['i']) AND ($pl['opt']['be']['i'] >= 0) AND ($pl['opt']['be']['i'] <= 2)) {
  8677. $annots .= ' /I '.sprintf(' %.4F', $pl['opt']['be']['i']);
  8678. }
  8679. $annots .= '>>';
  8680. }
  8681. if (isset($pl['opt']['c']) AND (is_array($pl['opt']['c'])) AND !empty($pl['opt']['c'])) {
  8682. $annots .= ' /C [';
  8683. foreach ($pl['opt']['c'] as $col) {
  8684. $col = intval($col);
  8685. $color = $col <= 0 ? 0 : ($col >= 255 ? 1 : $col / 255);
  8686. $annots .= sprintf(' %.4F', $color);
  8687. }
  8688. $annots .= ']';
  8689. }
  8690. //$annots .= ' /StructParent ';
  8691. //$annots .= ' /OC ';
  8692. $markups = array('text', 'freetext', 'line', 'square', 'circle', 'polygon', 'polyline', 'highlight', 'underline', 'squiggly', 'strikeout', 'stamp', 'caret', 'ink', 'fileattachment', 'sound');
  8693. if (in_array(strtolower($pl['opt']['subtype']), $markups)) {
  8694. // this is a markup type
  8695. if (isset($pl['opt']['t']) AND is_string($pl['opt']['t'])) {
  8696. $annots .= ' /T '.$this->_textstring($pl['opt']['t'], $annot_obj_id);
  8697. }
  8698. //$annots .= ' /Popup ';
  8699. if (isset($pl['opt']['ca'])) {
  8700. $annots .= ' /CA '.sprintf('%.4F', floatval($pl['opt']['ca']));
  8701. }
  8702. if (isset($pl['opt']['rc'])) {
  8703. $annots .= ' /RC '.$this->_textstring($pl['opt']['rc'], $annot_obj_id);
  8704. }
  8705. $annots .= ' /CreationDate '.$this->_datestring($annot_obj_id);
  8706. //$annots .= ' /IRT ';
  8707. if (isset($pl['opt']['subj'])) {
  8708. $annots .= ' /Subj '.$this->_textstring($pl['opt']['subj'], $annot_obj_id);
  8709. }
  8710. //$annots .= ' /RT ';
  8711. //$annots .= ' /IT ';
  8712. //$annots .= ' /ExData ';
  8713. }
  8714. $lineendings = array('Square', 'Circle', 'Diamond', 'OpenArrow', 'ClosedArrow', 'None', 'Butt', 'ROpenArrow', 'RClosedArrow', 'Slash');
  8715. // Annotation types
  8716. switch (strtolower($pl['opt']['subtype'])) {
  8717. case 'text': {
  8718. if (isset($pl['opt']['open'])) {
  8719. $annots .= ' /Open '. (strtolower($pl['opt']['open']) == 'true' ? 'true' : 'false');
  8720. }
  8721. $iconsapp = array('Comment', 'Help', 'Insert', 'Key', 'NewParagraph', 'Note', 'Paragraph');
  8722. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  8723. $annots .= ' /Name /'.$pl['opt']['name'];
  8724. } else {
  8725. $annots .= ' /Name /Note';
  8726. }
  8727. $statemodels = array('Marked', 'Review');
  8728. if (isset($pl['opt']['statemodel']) AND in_array($pl['opt']['statemodel'], $statemodels)) {
  8729. $annots .= ' /StateModel /'.$pl['opt']['statemodel'];
  8730. } else {
  8731. $pl['opt']['statemodel'] = 'Marked';
  8732. $annots .= ' /StateModel /'.$pl['opt']['statemodel'];
  8733. }
  8734. if ($pl['opt']['statemodel'] == 'Marked') {
  8735. $states = array('Accepted', 'Unmarked');
  8736. } else {
  8737. $states = array('Accepted', 'Rejected', 'Cancelled', 'Completed', 'None');
  8738. }
  8739. if (isset($pl['opt']['state']) AND in_array($pl['opt']['state'], $states)) {
  8740. $annots .= ' /State /'.$pl['opt']['state'];
  8741. } else {
  8742. if ($pl['opt']['statemodel'] == 'Marked') {
  8743. $annots .= ' /State /Unmarked';
  8744. } else {
  8745. $annots .= ' /State /None';
  8746. }
  8747. }
  8748. break;
  8749. }
  8750. case 'link': {
  8751. if (is_string($pl['txt'])) {
  8752. // external URI link
  8753. $annots .= ' /A <</S /URI /URI '.$this->_datastring($this->unhtmlentities($pl['txt']), $annot_obj_id).'>>';
  8754. } else {
  8755. // internal link
  8756. $l = $this->links[$pl['txt']];
  8757. $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)));
  8758. }
  8759. $hmodes = array('N', 'I', 'O', 'P');
  8760. if (isset($pl['opt']['h']) AND in_array($pl['opt']['h'], $hmodes)) {
  8761. $annots .= ' /H /'.$pl['opt']['h'];
  8762. } else {
  8763. $annots .= ' /H /I';
  8764. }
  8765. //$annots .= ' /PA ';
  8766. //$annots .= ' /Quadpoints ';
  8767. break;
  8768. }
  8769. case 'freetext': {
  8770. if (isset($pl['opt']['da']) AND !empty($pl['opt']['da'])) {
  8771. $annots .= ' /DA ('.$pl['opt']['da'].')';
  8772. }
  8773. if (isset($pl['opt']['q']) AND ($pl['opt']['q'] >= 0) AND ($pl['opt']['q'] <= 2)) {
  8774. $annots .= ' /Q '.intval($pl['opt']['q']);
  8775. }
  8776. if (isset($pl['opt']['rc'])) {
  8777. $annots .= ' /RC '.$this->_textstring($pl['opt']['rc'], $annot_obj_id);
  8778. }
  8779. if (isset($pl['opt']['ds'])) {
  8780. $annots .= ' /DS '.$this->_textstring($pl['opt']['ds'], $annot_obj_id);
  8781. }
  8782. if (isset($pl['opt']['cl']) AND is_array($pl['opt']['cl'])) {
  8783. $annots .= ' /CL [';
  8784. foreach ($pl['opt']['cl'] as $cl) {
  8785. $annots .= sprintf('%.4F ', $cl * $this->k);
  8786. }
  8787. $annots .= ']';
  8788. }
  8789. $tfit = array('FreeText', 'FreeTextCallout', 'FreeTextTypeWriter');
  8790. if (isset($pl['opt']['it']) AND in_array($pl['opt']['it'], $tfit)) {
  8791. $annots .= ' /IT /'.$pl['opt']['it'];
  8792. }
  8793. if (isset($pl['opt']['rd']) AND is_array($pl['opt']['rd'])) {
  8794. $l = $pl['opt']['rd'][0] * $this->k;
  8795. $r = $pl['opt']['rd'][1] * $this->k;
  8796. $t = $pl['opt']['rd'][2] * $this->k;
  8797. $b = $pl['opt']['rd'][3] * $this->k;
  8798. $annots .= ' /RD ['.sprintf('%.2F %.2F %.2F %.2F', $l, $r, $t, $b).']';
  8799. }
  8800. if (isset($pl['opt']['le']) AND in_array($pl['opt']['le'], $lineendings)) {
  8801. $annots .= ' /LE /'.$pl['opt']['le'];
  8802. }
  8803. break;
  8804. }
  8805. case 'line': {
  8806. break;
  8807. }
  8808. case 'square': {
  8809. break;
  8810. }
  8811. case 'circle': {
  8812. break;
  8813. }
  8814. case 'polygon': {
  8815. break;
  8816. }
  8817. case 'polyline': {
  8818. break;
  8819. }
  8820. case 'highlight': {
  8821. break;
  8822. }
  8823. case 'underline': {
  8824. break;
  8825. }
  8826. case 'squiggly': {
  8827. break;
  8828. }
  8829. case 'strikeout': {
  8830. break;
  8831. }
  8832. case 'stamp': {
  8833. break;
  8834. }
  8835. case 'caret': {
  8836. break;
  8837. }
  8838. case 'ink': {
  8839. break;
  8840. }
  8841. case 'popup': {
  8842. break;
  8843. }
  8844. case 'fileattachment': {
  8845. if (!isset($pl['opt']['fs'])) {
  8846. break;
  8847. }
  8848. $filename = basename($pl['opt']['fs']);
  8849. if (isset($this->embeddedfiles[$filename]['n'])) {
  8850. $annots .= ' /FS <</Type /Filespec /F '.$this->_datastring($filename, $annot_obj_id).' /EF <</F '.$this->embeddedfiles[$filename]['n'].' 0 R>> >>';
  8851. $iconsapp = array('Graph', 'Paperclip', 'PushPin', 'Tag');
  8852. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  8853. $annots .= ' /Name /'.$pl['opt']['name'];
  8854. } else {
  8855. $annots .= ' /Name /PushPin';
  8856. }
  8857. }
  8858. break;
  8859. }
  8860. case 'sound': {
  8861. if (!isset($pl['opt']['fs'])) {
  8862. break;
  8863. }
  8864. $filename = basename($pl['opt']['fs']);
  8865. if (isset($this->embeddedfiles[$filename]['n'])) {
  8866. // ... TO BE COMPLETED ...
  8867. // /R /C /B /E /CO /CP
  8868. $annots .= ' /Sound <</Type /Filespec /F '.$this->_datastring($filename, $annot_obj_id).' /EF <</F '.$this->embeddedfiles[$filename]['n'].' 0 R>> >>';
  8869. $iconsapp = array('Speaker', 'Mic');
  8870. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  8871. $annots .= ' /Name /'.$pl['opt']['name'];
  8872. } else {
  8873. $annots .= ' /Name /Speaker';
  8874. }
  8875. }
  8876. break;
  8877. }
  8878. case 'movie': {
  8879. break;
  8880. }
  8881. case 'widget': {
  8882. $hmode = array('N', 'I', 'O', 'P', 'T');
  8883. if (isset($pl['opt']['h']) AND in_array($pl['opt']['h'], $hmode)) {
  8884. $annots .= ' /H /'.$pl['opt']['h'];
  8885. }
  8886. if (isset($pl['opt']['mk']) AND (is_array($pl['opt']['mk'])) AND !empty($pl['opt']['mk'])) {
  8887. $annots .= ' /MK <<';
  8888. if (isset($pl['opt']['mk']['r'])) {
  8889. $annots .= ' /R '.$pl['opt']['mk']['r'];
  8890. }
  8891. if (isset($pl['opt']['mk']['bc']) AND (is_array($pl['opt']['mk']['bc']))) {
  8892. $annots .= ' /BC [';
  8893. foreach($pl['opt']['mk']['bc'] AS $col) {
  8894. $col = intval($col);
  8895. $color = $col <= 0 ? 0 : ($col >= 255 ? 1 : $col / 255);
  8896. $annots .= sprintf(' %.2F', $color);
  8897. }
  8898. $annots .= ']';
  8899. }
  8900. if (isset($pl['opt']['mk']['bg']) AND (is_array($pl['opt']['mk']['bg']))) {
  8901. $annots .= ' /BG [';
  8902. foreach($pl['opt']['mk']['bg'] AS $col) {
  8903. $col = intval($col);
  8904. $color = $col <= 0 ? 0 : ($col >= 255 ? 1 : $col / 255);
  8905. $annots .= sprintf(' %.2F', $color);
  8906. }
  8907. $annots .= ']';
  8908. }
  8909. if (isset($pl['opt']['mk']['ca'])) {
  8910. $annots .= ' /CA '.$pl['opt']['mk']['ca'];
  8911. }
  8912. if (isset($pl['opt']['mk']['rc'])) {
  8913. $annots .= ' /RC '.$pl['opt']['mk']['rc'];
  8914. }
  8915. if (isset($pl['opt']['mk']['ac'])) {
  8916. $annots .= ' /AC '.$pl['opt']['mk']['ac'];
  8917. }
  8918. if (isset($pl['opt']['mk']['i'])) {
  8919. $info = $this->getImageBuffer($pl['opt']['mk']['i']);
  8920. if ($info !== false) {
  8921. $annots .= ' /I '.$info['n'].' 0 R';
  8922. }
  8923. }
  8924. if (isset($pl['opt']['mk']['ri'])) {
  8925. $info = $this->getImageBuffer($pl['opt']['mk']['ri']);
  8926. if ($info !== false) {
  8927. $annots .= ' /RI '.$info['n'].' 0 R';
  8928. }
  8929. }
  8930. if (isset($pl['opt']['mk']['ix'])) {
  8931. $info = $this->getImageBuffer($pl['opt']['mk']['ix']);
  8932. if ($info !== false) {
  8933. $annots .= ' /IX '.$info['n'].' 0 R';
  8934. }
  8935. }
  8936. if (isset($pl['opt']['mk']['if']) AND (is_array($pl['opt']['mk']['if'])) AND !empty($pl['opt']['mk']['if'])) {
  8937. $annots .= ' /IF <<';
  8938. $if_sw = array('A', 'B', 'S', 'N');
  8939. if (isset($pl['opt']['mk']['if']['sw']) AND in_array($pl['opt']['mk']['if']['sw'], $if_sw)) {
  8940. $annots .= ' /SW /'.$pl['opt']['mk']['if']['sw'];
  8941. }
  8942. $if_s = array('A', 'P');
  8943. if (isset($pl['opt']['mk']['if']['s']) AND in_array($pl['opt']['mk']['if']['s'], $if_s)) {
  8944. $annots .= ' /S /'.$pl['opt']['mk']['if']['s'];
  8945. }
  8946. if (isset($pl['opt']['mk']['if']['a']) AND (is_array($pl['opt']['mk']['if']['a'])) AND !empty($pl['opt']['mk']['if']['a'])) {
  8947. $annots .= sprintf(' /A [%.2F %.2F]', $pl['opt']['mk']['if']['a'][0], $pl['opt']['mk']['if']['a'][1]);
  8948. }
  8949. if (isset($pl['opt']['mk']['if']['fb']) AND ($pl['opt']['mk']['if']['fb'])) {
  8950. $annots .= ' /FB true';
  8951. }
  8952. $annots .= '>>';
  8953. }
  8954. if (isset($pl['opt']['mk']['tp']) AND ($pl['opt']['mk']['tp'] >= 0) AND ($pl['opt']['mk']['tp'] <= 6)) {
  8955. $annots .= ' /TP '.intval($pl['opt']['mk']['tp']);
  8956. } else {
  8957. $annots .= ' /TP 0';
  8958. }
  8959. $annots .= '>>';
  8960. } // end MK
  8961. // --- Entries for field dictionaries ---
  8962. if (isset($this->radiobutton_groups[$n][$pl['txt']])) {
  8963. // set parent
  8964. $annots .= ' /Parent '.$this->radiobutton_groups[$n][$pl['txt']].' 0 R';
  8965. }
  8966. if (isset($pl['opt']['t']) AND is_string($pl['opt']['t'])) {
  8967. $annots .= ' /T '.$this->_datastring($pl['opt']['t'], $annot_obj_id);
  8968. }
  8969. if (isset($pl['opt']['tu']) AND is_string($pl['opt']['tu'])) {
  8970. $annots .= ' /TU '.$this->_datastring($pl['opt']['tu'], $annot_obj_id);
  8971. }
  8972. if (isset($pl['opt']['tm']) AND is_string($pl['opt']['tm'])) {
  8973. $annots .= ' /TM '.$this->_datastring($pl['opt']['tm'], $annot_obj_id);
  8974. }
  8975. if (isset($pl['opt']['ff'])) {
  8976. if (is_array($pl['opt']['ff'])) {
  8977. // array of bit settings
  8978. $flag = 0;
  8979. foreach($pl['opt']['ff'] as $val) {
  8980. $flag += 1 << ($val - 1);
  8981. }
  8982. } else {
  8983. $flag = intval($pl['opt']['ff']);
  8984. }
  8985. $annots .= ' /Ff '.$flag;
  8986. }
  8987. if (isset($pl['opt']['maxlen'])) {
  8988. $annots .= ' /MaxLen '.intval($pl['opt']['maxlen']);
  8989. }
  8990. if (isset($pl['opt']['v'])) {
  8991. $annots .= ' /V';
  8992. if (is_array($pl['opt']['v'])) {
  8993. foreach ($pl['opt']['v'] AS $optval) {
  8994. if (is_float($optval)) {
  8995. $optval = sprintf('%.2F', $optval);
  8996. }
  8997. $annots .= ' '.$optval;
  8998. }
  8999. } else {
  9000. $annots .= ' '.$this->_textstring($pl['opt']['v'], $annot_obj_id);
  9001. }
  9002. }
  9003. if (isset($pl['opt']['dv'])) {
  9004. $annots .= ' /DV';
  9005. if (is_array($pl['opt']['dv'])) {
  9006. foreach ($pl['opt']['dv'] AS $optval) {
  9007. if (is_float($optval)) {
  9008. $optval = sprintf('%.2F', $optval);
  9009. }
  9010. $annots .= ' '.$optval;
  9011. }
  9012. } else {
  9013. $annots .= ' '.$this->_textstring($pl['opt']['dv'], $annot_obj_id);
  9014. }
  9015. }
  9016. if (isset($pl['opt']['rv'])) {
  9017. $annots .= ' /RV';
  9018. if (is_array($pl['opt']['rv'])) {
  9019. foreach ($pl['opt']['rv'] AS $optval) {
  9020. if (is_float($optval)) {
  9021. $optval = sprintf('%.2F', $optval);
  9022. }
  9023. $annots .= ' '.$optval;
  9024. }
  9025. } else {
  9026. $annots .= ' '.$this->_textstring($pl['opt']['rv'], $annot_obj_id);
  9027. }
  9028. }
  9029. if (isset($pl['opt']['a']) AND !empty($pl['opt']['a'])) {
  9030. $annots .= ' /A << '.$pl['opt']['a'].' >>';
  9031. }
  9032. if (isset($pl['opt']['aa']) AND !empty($pl['opt']['aa'])) {
  9033. $annots .= ' /AA << '.$pl['opt']['aa'].' >>';
  9034. }
  9035. if (isset($pl['opt']['da']) AND !empty($pl['opt']['da'])) {
  9036. $annots .= ' /DA ('.$pl['opt']['da'].')';
  9037. }
  9038. if (isset($pl['opt']['q']) AND ($pl['opt']['q'] >= 0) AND ($pl['opt']['q'] <= 2)) {
  9039. $annots .= ' /Q '.intval($pl['opt']['q']);
  9040. }
  9041. if (isset($pl['opt']['opt']) AND (is_array($pl['opt']['opt'])) AND !empty($pl['opt']['opt'])) {
  9042. $annots .= ' /Opt [';
  9043. foreach($pl['opt']['opt'] AS $copt) {
  9044. if (is_array($copt)) {
  9045. $annots .= ' ['.$this->_textstring($copt[0], $annot_obj_id).' '.$this->_textstring($copt[1], $annot_obj_id).']';
  9046. } else {
  9047. $annots .= ' '.$this->_textstring($copt, $annot_obj_id);
  9048. }
  9049. }
  9050. $annots .= ']';
  9051. }
  9052. if (isset($pl['opt']['ti'])) {
  9053. $annots .= ' /TI '.intval($pl['opt']['ti']);
  9054. }
  9055. if (isset($pl['opt']['i']) AND (is_array($pl['opt']['i'])) AND !empty($pl['opt']['i'])) {
  9056. $annots .= ' /I [';
  9057. foreach($pl['opt']['i'] AS $copt) {
  9058. $annots .= intval($copt).' ';
  9059. }
  9060. $annots .= ']';
  9061. }
  9062. break;
  9063. }
  9064. case 'screen': {
  9065. break;
  9066. }
  9067. case 'printermark': {
  9068. break;
  9069. }
  9070. case 'trapnet': {
  9071. break;
  9072. }
  9073. case 'watermark': {
  9074. break;
  9075. }
  9076. case '3d': {
  9077. break;
  9078. }
  9079. default: {
  9080. break;
  9081. }
  9082. }
  9083. $annots .= '>>';
  9084. // create new annotation object
  9085. $this->_out($this->_getobj($annot_obj_id)."\n".$annots."\n".'endobj');
  9086. if ($formfield AND !isset($this->radiobutton_groups[$n][$pl['txt']])) {
  9087. // store reference of form object
  9088. $this->form_obj_id[] = $annot_obj_id;
  9089. }
  9090. }
  9091. }
  9092. } // end for each page
  9093. }
  9094. /**
  9095. * Put appearance streams XObject used to define annotation's appearance states
  9096. * @param $w (int) annotation width
  9097. * @param $h (int) annotation height
  9098. * @param $stream (string) appearance stream
  9099. * @return int object ID
  9100. * @protected
  9101. * @since 4.8.001 (2009-09-09)
  9102. */
  9103. protected function _putAPXObject($w=0, $h=0, $stream='') {
  9104. $stream = trim($stream);
  9105. $out = $this->_getobj()."\n";
  9106. $this->xobjects['AX'.$this->n] = array('n' => $this->n);
  9107. $out .= '<<';
  9108. $out .= ' /Type /XObject';
  9109. $out .= ' /Subtype /Form';
  9110. $out .= ' /FormType 1';
  9111. if ($this->compress) {
  9112. $stream = gzcompress($stream);
  9113. $out .= ' /Filter /FlateDecode';
  9114. }
  9115. $rect = sprintf('%.2F %.2F', $w, $h);
  9116. $out .= ' /BBox [0 0 '.$rect.']';
  9117. $out .= ' /Matrix [1 0 0 1 0 0]';
  9118. $out .= ' /Resources <<';
  9119. $out .= ' /ProcSet [/PDF /Text]';
  9120. $out .= ' /Font <<';
  9121. foreach ($this->annotation_fonts as $fontkey => $fontid) {
  9122. $out .= ' /F'.$fontid.' '.$this->font_obj_ids[$fontkey].' 0 R';
  9123. }
  9124. $out .= ' >>';
  9125. $out .= ' >>';
  9126. $stream = $this->_getrawstream($stream);
  9127. $out .= ' /Length '.strlen($stream);
  9128. $out .= ' >>';
  9129. $out .= ' stream'."\n".$stream."\n".'endstream';
  9130. $out .= "\n".'endobj';
  9131. $this->_out($out);
  9132. return $this->n;
  9133. }
  9134. /**
  9135. * Get ULONG from string (Big Endian 32-bit unsigned integer).
  9136. * @param $str (string) string from where to extract value
  9137. * @param $offset (int) point from where to read the data
  9138. * @return int 32 bit value
  9139. * @author Nicola Asuni
  9140. * @protected
  9141. * @since 5.2.000 (2010-06-02)
  9142. */
  9143. protected function _getULONG($str, $offset) {
  9144. $v = unpack('Ni', substr($str, $offset, 4));
  9145. return $v['i'];
  9146. }
  9147. /**
  9148. * Get USHORT from string (Big Endian 16-bit unsigned integer).
  9149. * @param $str (string) string from where to extract value
  9150. * @param $offset (int) point from where to read the data
  9151. * @return int 16 bit value
  9152. * @author Nicola Asuni
  9153. * @protected
  9154. * @since 5.2.000 (2010-06-02)
  9155. */
  9156. protected function _getUSHORT($str, $offset) {
  9157. $v = unpack('ni', substr($str, $offset, 2));
  9158. return $v['i'];
  9159. }
  9160. /**
  9161. * Get SHORT from string (Big Endian 16-bit signed integer).
  9162. * @param $str (string) string from where to extract value
  9163. * @param $offset (int) point from where to read the data
  9164. * @return int 16 bit value
  9165. * @author Nicola Asuni
  9166. * @protected
  9167. * @since 5.2.000 (2010-06-02)
  9168. */
  9169. protected function _getSHORT($str, $offset) {
  9170. $v = unpack('si', substr($str, $offset, 2));
  9171. return $v['i'];
  9172. }
  9173. /**
  9174. * Get BYTE from string (8-bit unsigned integer).
  9175. * @param $str (string) string from where to extract value
  9176. * @param $offset (int) point from where to read the data
  9177. * @return int 8 bit value
  9178. * @author Nicola Asuni
  9179. * @protected
  9180. * @since 5.2.000 (2010-06-02)
  9181. */
  9182. protected function _getBYTE($str, $offset) {
  9183. $v = unpack('Ci', substr($str, $offset, 1));
  9184. return $v['i'];
  9185. }
  9186. /**
  9187. * Returns a subset of the TrueType font data without the unused glyphs.
  9188. * @param $font (string) TrueType font data.
  9189. * @param $subsetchars (array) Array of used characters (the glyphs to keep).
  9190. * @return string a subset of TrueType font data without the unused glyphs
  9191. * @author Nicola Asuni
  9192. * @protected
  9193. * @since 5.2.000 (2010-06-02)
  9194. */
  9195. protected function _getTrueTypeFontSubset($font, $subsetchars) {
  9196. ksort($subsetchars);
  9197. $offset = 0; // offset position of the font data
  9198. if ($this->_getULONG($font, $offset) != 0x10000) {
  9199. // sfnt version must be 0x00010000 for TrueType version 1.0.
  9200. return $font;
  9201. }
  9202. $offset += 4;
  9203. // get number of tables
  9204. $numTables = $this->_getUSHORT($font, $offset);
  9205. $offset += 2;
  9206. // skip searchRange, entrySelector and rangeShift
  9207. $offset += 6;
  9208. // tables array
  9209. $table = array();
  9210. // for each table
  9211. for ($i = 0; $i < $numTables; ++$i) {
  9212. // get table info
  9213. $tag = substr($font, $offset, 4);
  9214. $offset += 4;
  9215. $table[$tag] = array();
  9216. $table[$tag]['checkSum'] = $this->_getULONG($font, $offset);
  9217. $offset += 4;
  9218. $table[$tag]['offset'] = $this->_getULONG($font, $offset);
  9219. $offset += 4;
  9220. $table[$tag]['length'] = $this->_getULONG($font, $offset);
  9221. $offset += 4;
  9222. }
  9223. // check magicNumber
  9224. $offset = $table['head']['offset'] + 12;
  9225. if ($this->_getULONG($font, $offset) != 0x5F0F3CF5) {
  9226. // magicNumber must be 0x5F0F3CF5
  9227. return $font;
  9228. }
  9229. $offset += 4;
  9230. // get offset mode (indexToLocFormat : 0 = short, 1 = long)
  9231. $offset = $table['head']['offset'] + 50;
  9232. $short_offset = ($this->_getSHORT($font, $offset) == 0);
  9233. $offset += 2;
  9234. // get the offsets to the locations of the glyphs in the font, relative to the beginning of the glyphData table
  9235. $indexToLoc = array();
  9236. $offset = $table['loca']['offset'];
  9237. if ($short_offset) {
  9238. // short version
  9239. $tot_num_glyphs = ($table['loca']['length'] / 2); // numGlyphs + 1
  9240. for ($i = 0; $i < $tot_num_glyphs; ++$i) {
  9241. $indexToLoc[$i] = $this->_getUSHORT($font, $offset) * 2;
  9242. $offset += 2;
  9243. }
  9244. } else {
  9245. // long version
  9246. $tot_num_glyphs = ($table['loca']['length'] / 4); // numGlyphs + 1
  9247. for ($i = 0; $i < $tot_num_glyphs; ++$i) {
  9248. $indexToLoc[$i] = $this->_getULONG($font, $offset);
  9249. $offset += 4;
  9250. }
  9251. }
  9252. // get glyphs indexes of chars from cmap table
  9253. $subsetglyphs = array(); // glyph IDs on key
  9254. $subsetglyphs[0] = true; // character codes that do not correspond to any glyph in the font should be mapped to glyph index 0
  9255. $offset = $table['cmap']['offset'] + 2;
  9256. $numEncodingTables = $this->_getUSHORT($font, $offset);
  9257. $offset += 2;
  9258. $encodingTables = array();
  9259. for ($i = 0; $i < $numEncodingTables; ++$i) {
  9260. $encodingTables[$i]['platformID'] = $this->_getUSHORT($font, $offset);
  9261. $offset += 2;
  9262. $encodingTables[$i]['encodingID'] = $this->_getUSHORT($font, $offset);
  9263. $offset += 2;
  9264. $encodingTables[$i]['offset'] = $this->_getULONG($font, $offset);
  9265. $offset += 4;
  9266. }
  9267. foreach ($encodingTables as $enctable) {
  9268. if (($enctable['platformID'] == 3) AND ($enctable['encodingID'] == 0)) {
  9269. $modesymbol = true;
  9270. } else {
  9271. $modesymbol = false;
  9272. }
  9273. $offset = $table['cmap']['offset'] + $enctable['offset'];
  9274. $format = $this->_getUSHORT($font, $offset);
  9275. $offset += 2;
  9276. switch ($format) {
  9277. case 0: { // Format 0: Byte encoding table
  9278. $offset += 4; // skip length and version/language
  9279. for ($c = 0; $c < 256; ++$c) {
  9280. if (isset($subsetchars[$c])) {
  9281. $g = $this->_getBYTE($font, $offset);
  9282. $subsetglyphs[$g] = true;
  9283. }
  9284. ++$offset;
  9285. }
  9286. break;
  9287. }
  9288. case 2: { // Format 2: High-byte mapping through table
  9289. $offset += 4; // skip length and version/language
  9290. $numSubHeaders = 0;
  9291. for ($i = 0; $i < 256; ++$i) {
  9292. // Array that maps high bytes to subHeaders: value is subHeader index * 8.
  9293. $subHeaderKeys[$i] = ($this->_getUSHORT($font, $offset) / 8);
  9294. $offset += 2;
  9295. if ($numSubHeaders < $subHeaderKeys[$i]) {
  9296. $numSubHeaders = $subHeaderKeys[$i];
  9297. }
  9298. }
  9299. // the number of subHeaders is equal to the max of subHeaderKeys + 1
  9300. ++$numSubHeaders;
  9301. // read subHeader structures
  9302. $subHeaders = array();
  9303. for ($k = 0; $k < $numSubHeaders; ++$k) {
  9304. $subHeaders[$k]['firstCode'] = $this->_getUSHORT($font, $offset);
  9305. $offset += 2;
  9306. $subHeaders[$k]['entryCount'] = $this->_getUSHORT($font, $offset);
  9307. $offset += 2;
  9308. $subHeaders[$k]['idDelta'] = $this->_getSHORT($font, $offset);
  9309. $offset += 2;
  9310. $subHeaders[$k]['idRangeOffset'] = $this->_getUSHORT($font, $offset);
  9311. $offset += 2;
  9312. $subHeaders[$k]['idRangeOffset'] -= (2 + (($numSubHeaders - $k - 1) * 8));
  9313. $subHeaders[$k]['idRangeOffset'] /= 2;
  9314. $numGlyphIndexArray += $subHeaders[$k]['entryCount'];
  9315. }
  9316. for ($k = 0; $k < $numGlyphIndexArray; ++$k) {
  9317. $glyphIndexArray[$k] = $this->_getUSHORT($font, $offset);
  9318. $offset += 2;
  9319. }
  9320. for ($i = 0; $i < 256; ++$i) {
  9321. $k = $subHeaderKeys[$i];
  9322. if ($k == 0) {
  9323. // one byte code
  9324. $c = $i;
  9325. if (isset($subsetchars[$c])) {
  9326. $g = $glyphIndexArray[0];
  9327. $subsetglyphs[$g] = true;
  9328. }
  9329. } else {
  9330. // two bytes code
  9331. $start_byte = $subHeaders[$k]['firstCode'];
  9332. $end_byte = $start_byte + $subHeaders[$k]['entryCount'];
  9333. for ($j = $start_byte; $j < $end_byte; ++$j) {
  9334. // combine high and low bytes
  9335. $c = (($i << 8) + $j);
  9336. if (isset($subsetchars[$c])) {
  9337. $idRangeOffset = ($subHeaders[$k]['idRangeOffset'] + $j - $subHeaders[$k]['firstCode']);
  9338. $g = $glyphIndexArray[$idRangeOffset];
  9339. $g += ($idDelta[$k] - 65536);
  9340. if ($g < 0) {
  9341. $g = 0;
  9342. }
  9343. $subsetglyphs[$g] = true;
  9344. }
  9345. }
  9346. }
  9347. }
  9348. break;
  9349. }
  9350. case 4: { // Format 4: Segment mapping to delta values
  9351. $length = $this->_getUSHORT($font, $offset);
  9352. $offset += 2;
  9353. $offset += 2; // skip version/language
  9354. $segCount = ($this->_getUSHORT($font, $offset) / 2);
  9355. $offset += 2;
  9356. $offset += 6; // skip searchRange, entrySelector, rangeShift
  9357. $endCount = array(); // array of end character codes for each segment
  9358. for ($k = 0; $k < $segCount; ++$k) {
  9359. $endCount[$k] = $this->_getUSHORT($font, $offset);
  9360. $offset += 2;
  9361. }
  9362. $offset += 2; // skip reservedPad
  9363. $startCount = array(); // array of start character codes for each segment
  9364. for ($k = 0; $k < $segCount; ++$k) {
  9365. $startCount[$k] = $this->_getUSHORT($font, $offset);
  9366. $offset += 2;
  9367. }
  9368. $idDelta = array(); // delta for all character codes in segment
  9369. for ($k = 0; $k < $segCount; ++$k) {
  9370. $idDelta[$k] = $this->_getUSHORT($font, $offset);
  9371. $offset += 2;
  9372. }
  9373. $idRangeOffset = array(); // Offsets into glyphIdArray or 0
  9374. for ($k = 0; $k < $segCount; ++$k) {
  9375. $idRangeOffset[$k] = $this->_getUSHORT($font, $offset);
  9376. $offset += 2;
  9377. }
  9378. $gidlen = ($length / 2) - 8 - (4 * $segCount);
  9379. $glyphIdArray = array(); // glyph index array
  9380. for ($k = 0; $k < $gidlen; ++$k) {
  9381. $glyphIdArray[$k] = $this->_getUSHORT($font, $offset);
  9382. $offset += 2;
  9383. }
  9384. for ($k = 0; $k < $segCount; ++$k) {
  9385. for ($c = $startCount[$k]; $c <= $endCount[$k]; ++$c) {
  9386. if (isset($subsetchars[$c])) {
  9387. if ($idRangeOffset[$k] == 0) {
  9388. $g = $c;
  9389. } else {
  9390. $gid = (($idRangeOffset[$k] / 2) + ($c - $startCount[$k]) - ($segCount - $k));
  9391. $g = $glyphIdArray[$gid];
  9392. }
  9393. $g += ($idDelta[$k] - 65536);
  9394. if ($g < 0) {
  9395. $g = 0;
  9396. }
  9397. $subsetglyphs[$g] = true;
  9398. }
  9399. }
  9400. }
  9401. break;
  9402. }
  9403. case 6: { // Format 6: Trimmed table mapping
  9404. $offset += 4; // skip length and version/language
  9405. $firstCode = $this->_getUSHORT($font, $offset);
  9406. $offset += 2;
  9407. $entryCount = $this->_getUSHORT($font, $offset);
  9408. $offset += 2;
  9409. for ($k = 0; $k < $entryCount; ++$k) {
  9410. $c = ($k + $firstCode);
  9411. if (isset($subsetchars[$c])) {
  9412. $g = $this->_getUSHORT($font, $offset);
  9413. $subsetglyphs[$g] = true;
  9414. }
  9415. $offset += 2;
  9416. }
  9417. break;
  9418. }
  9419. case 8: { // Format 8: Mixed 16-bit and 32-bit coverage
  9420. $offset += 10; // skip reserved, length and version/language
  9421. for ($k = 0; $k < 8192; ++$k) {
  9422. $is32[$k] = $this->_getBYTE($font, $offset);
  9423. ++$offset;
  9424. }
  9425. $nGroups = $this->_getULONG($font, $offset);
  9426. $offset += 4;
  9427. for ($i = 0; $i < $nGroups; ++$i) {
  9428. $startCharCode = $this->_getULONG($font, $offset);
  9429. $offset += 4;
  9430. $endCharCode = $this->_getULONG($font, $offset);
  9431. $offset += 4;
  9432. $startGlyphID = $this->_getULONG($font, $offset);
  9433. $offset += 4;
  9434. for ($k = $startCharCode; $k <= $endCharCode; ++$k) {
  9435. $is32idx = floor($c / 8);
  9436. if ((isset($is32[$is32idx])) AND (($is32[$is32idx] & (1 << (7 - ($c % 8)))) == 0)) {
  9437. $c = $k;
  9438. } else {
  9439. // 32 bit format
  9440. // convert to decimal (http://www.unicode.org/faq//utf_bom.html#utf16-4)
  9441. //LEAD_OFFSET = (0xD800 - (0x10000 >> 10)) = 55232
  9442. //SURROGATE_OFFSET = (0x10000 - (0xD800 << 10) - 0xDC00) = -56613888
  9443. $c = ((55232 + ($k >> 10)) << 10) + (0xDC00 + ($k & 0x3FF)) -56613888;
  9444. }
  9445. if (isset($subsetchars[$c])) {
  9446. $subsetglyphs[$startGlyphID] = true;
  9447. }
  9448. ++$startGlyphID;
  9449. }
  9450. }
  9451. break;
  9452. }
  9453. case 10: { // Format 10: Trimmed array
  9454. $offset += 10; // skip reserved, length and version/language
  9455. $startCharCode = $this->_getULONG($font, $offset);
  9456. $offset += 4;
  9457. $numChars = $this->_getULONG($font, $offset);
  9458. $offset += 4;
  9459. for ($k = 0; $k < $numChars; ++$k) {
  9460. $c = ($k + $startCharCode);
  9461. if (isset($subsetchars[$c])) {
  9462. $g = $this->_getUSHORT($font, $offset);
  9463. $subsetglyphs[$g] = true;
  9464. }
  9465. $offset += 2;
  9466. }
  9467. break;
  9468. }
  9469. case 12: { // Format 12: Segmented coverage
  9470. $offset += 10; // skip length and version/language
  9471. $nGroups = $this->_getULONG($font, $offset);
  9472. $offset += 4;
  9473. for ($k = 0; $k < $nGroups; ++$k) {
  9474. $startCharCode = $this->_getULONG($font, $offset);
  9475. $offset += 4;
  9476. $endCharCode = $this->_getULONG($font, $offset);
  9477. $offset += 4;
  9478. $startGlyphCode = $this->_getULONG($font, $offset);
  9479. $offset += 4;
  9480. for ($c = $startCharCode; $c <= $endCharCode; ++$c) {
  9481. if (isset($subsetchars[$c])) {
  9482. $subsetglyphs[$startGlyphCode] = true;
  9483. }
  9484. ++$startGlyphCode;
  9485. }
  9486. }
  9487. break;
  9488. }
  9489. case 13: { // Format 13: Many-to-one range mappings
  9490. // to be implemented ...
  9491. break;
  9492. }
  9493. case 14: { // Format 14: Unicode Variation Sequences
  9494. // to be implemented ...
  9495. break;
  9496. }
  9497. }
  9498. }
  9499. // include all parts of composite glyphs
  9500. $new_sga = $subsetglyphs;
  9501. while (!empty($new_sga)) {
  9502. $sga = $new_sga;
  9503. $new_sga = array();
  9504. foreach ($sga as $key => $val) {
  9505. if (isset($indexToLoc[$key])) {
  9506. $offset = ($table['glyf']['offset'] + $indexToLoc[$key]);
  9507. $numberOfContours = $this->_getSHORT($font, $offset);
  9508. $offset += 2;
  9509. if ($numberOfContours < 0) { // composite glyph
  9510. $offset += 8; // skip xMin, yMin, xMax, yMax
  9511. do {
  9512. $flags = $this->_getUSHORT($font, $offset);
  9513. $offset += 2;
  9514. $glyphIndex = $this->_getUSHORT($font, $offset);
  9515. $offset += 2;
  9516. if (!isset($subsetglyphs[$glyphIndex])) {
  9517. // add missing glyphs
  9518. $new_sga[$glyphIndex] = true;
  9519. }
  9520. // skip some bytes by case
  9521. if ($flags & 1) {
  9522. $offset += 4;
  9523. } else {
  9524. $offset += 2;
  9525. }
  9526. if ($flags & 8) {
  9527. $offset += 2;
  9528. } elseif ($flags & 64) {
  9529. $offset += 4;
  9530. } elseif ($flags & 128) {
  9531. $offset += 8;
  9532. }
  9533. } while ($flags & 32);
  9534. }
  9535. }
  9536. }
  9537. $subsetglyphs += $new_sga;
  9538. }
  9539. // sort glyphs by key (and remove duplicates)
  9540. ksort($subsetglyphs);
  9541. // build new glyf and loca tables
  9542. $glyf = '';
  9543. $loca = '';
  9544. $offset = 0;
  9545. $glyf_offset = $table['glyf']['offset'];
  9546. for ($i = 0; $i < $tot_num_glyphs; ++$i) {
  9547. if (isset($subsetglyphs[$i])) {
  9548. $length = ($indexToLoc[($i + 1)] - $indexToLoc[$i]);
  9549. $glyf .= substr($font, ($glyf_offset + $indexToLoc[$i]), $length);
  9550. } else {
  9551. $length = 0;
  9552. }
  9553. if ($short_offset) {
  9554. $loca .= pack('n', ($offset / 2));
  9555. } else {
  9556. $loca .= pack('N', $offset);
  9557. }
  9558. $offset += $length;
  9559. }
  9560. // array of table names to preserve (loca and glyf tables will be added later)
  9561. // the cmap table is not needed and shall not be present, since the mapping from character codes to glyph descriptions is provided separately
  9562. $table_names = array ('head', 'hhea', 'hmtx', 'maxp', 'cvt ', 'fpgm', 'prep'); // minimum required table names
  9563. // get the tables to preserve
  9564. $offset = 12;
  9565. foreach ($table as $tag => $val) {
  9566. if (in_array($tag, $table_names)) {
  9567. $table[$tag]['data'] = substr($font, $table[$tag]['offset'], $table[$tag]['length']);
  9568. if ($tag == 'head') {
  9569. // set the checkSumAdjustment to 0
  9570. $table[$tag]['data'] = substr($table[$tag]['data'], 0, 8)."\x0\x0\x0\x0".substr($table[$tag]['data'], 12);
  9571. }
  9572. $pad = 4 - ($table[$tag]['length'] % 4);
  9573. if ($pad != 4) {
  9574. // the length of a table must be a multiple of four bytes
  9575. $table[$tag]['length'] += $pad;
  9576. $table[$tag]['data'] .= str_repeat("\x0", $pad);
  9577. }
  9578. $table[$tag]['offset'] = $offset;
  9579. $offset += $table[$tag]['length'];
  9580. // check sum is not changed (so keep the following line commented)
  9581. //$table[$tag]['checkSum'] = $this->_getTTFtableChecksum($table[$tag]['data'], $table[$tag]['length']);
  9582. } else {
  9583. unset($table[$tag]);
  9584. }
  9585. }
  9586. // add loca
  9587. $table['loca']['data'] = $loca;
  9588. $table['loca']['length'] = strlen($loca);
  9589. $pad = 4 - ($table['loca']['length'] % 4);
  9590. if ($pad != 4) {
  9591. // the length of a table must be a multiple of four bytes
  9592. $table['loca']['length'] += $pad;
  9593. $table['loca']['data'] .= str_repeat("\x0", $pad);
  9594. }
  9595. $table['loca']['offset'] = $offset;
  9596. $table['loca']['checkSum'] = $this->_getTTFtableChecksum($table['loca']['data'], $table['loca']['length']);
  9597. $offset += $table['loca']['length'];
  9598. // add glyf
  9599. $table['glyf']['data'] = $glyf;
  9600. $table['glyf']['length'] = strlen($glyf);
  9601. $pad = 4 - ($table['glyf']['length'] % 4);
  9602. if ($pad != 4) {
  9603. // the length of a table must be a multiple of four bytes
  9604. $table['glyf']['length'] += $pad;
  9605. $table['glyf']['data'] .= str_repeat("\x0", $pad);
  9606. }
  9607. $table['glyf']['offset'] = $offset;
  9608. $table['glyf']['checkSum'] = $this->_getTTFtableChecksum($table['glyf']['data'], $table['glyf']['length']);
  9609. // rebuild font
  9610. $font = '';
  9611. $font .= pack('N', 0x10000); // sfnt version
  9612. $numTables = count($table);
  9613. $font .= pack('n', $numTables); // numTables
  9614. $entrySelector = floor(log($numTables, 2));
  9615. $searchRange = pow(2, $entrySelector) * 16;
  9616. $rangeShift = ($numTables * 16) - $searchRange;
  9617. $font .= pack('n', $searchRange); // searchRange
  9618. $font .= pack('n', $entrySelector); // entrySelector
  9619. $font .= pack('n', $rangeShift); // rangeShift
  9620. $offset = ($numTables * 16);
  9621. foreach ($table as $tag => $data) {
  9622. $font .= $tag; // tag
  9623. $font .= pack('N', $data['checkSum']); // checkSum
  9624. $font .= pack('N', ($data['offset'] + $offset)); // offset
  9625. $font .= pack('N', $data['length']); // length
  9626. }
  9627. foreach ($table as $data) {
  9628. $font .= $data['data'];
  9629. }
  9630. // set checkSumAdjustment on head table
  9631. $checkSumAdjustment = 0xB1B0AFBA - $this->_getTTFtableChecksum($font, strlen($font));
  9632. $font = substr($font, 0, $table['head']['offset'] + 8).pack('N', $checkSumAdjustment).substr($font, $table['head']['offset'] + 12);
  9633. return $font;
  9634. }
  9635. /**
  9636. * Returs the checksum of a TTF table.
  9637. * @param $table (string) table to check
  9638. * @param $length (int) length of table in bytes
  9639. * @return int checksum
  9640. * @author Nicola Asuni
  9641. * @protected
  9642. * @since 5.2.000 (2010-06-02)
  9643. */
  9644. protected function _getTTFtableChecksum($table, $length) {
  9645. $sum = 0;
  9646. $tlen = ($length / 4);
  9647. $offset = 0;
  9648. for ($i = 0; $i < $tlen; ++$i) {
  9649. $v = unpack('Ni', substr($table, $offset, 4));
  9650. $sum += $v['i'];
  9651. $offset += 4;
  9652. }
  9653. $sum = unpack('Ni', pack('N', $sum));
  9654. return $sum['i'];
  9655. }
  9656. /**
  9657. * Outputs font widths
  9658. * @param $font (array) font data
  9659. * @param $cidoffset (int) offset for CID values
  9660. * @return PDF command string for font widths
  9661. * @author Nicola Asuni
  9662. * @protected
  9663. * @since 4.4.000 (2008-12-07)
  9664. */
  9665. protected function _putfontwidths($font, $cidoffset=0) {
  9666. ksort($font['cw']);
  9667. $rangeid = 0;
  9668. $range = array();
  9669. $prevcid = -2;
  9670. $prevwidth = -1;
  9671. $interval = false;
  9672. // for each character
  9673. foreach ($font['cw'] as $cid => $width) {
  9674. $cid -= $cidoffset;
  9675. if ($font['subset'] AND ($cid > 255) AND (!isset($font['subsetchars'][$cid]))) {
  9676. // ignore the unused characters (font subsetting)
  9677. continue;
  9678. }
  9679. if ($width != $font['dw']) {
  9680. if ($cid == ($prevcid + 1)) {
  9681. // consecutive CID
  9682. if ($width == $prevwidth) {
  9683. if ($width == $range[$rangeid][0]) {
  9684. $range[$rangeid][] = $width;
  9685. } else {
  9686. array_pop($range[$rangeid]);
  9687. // new range
  9688. $rangeid = $prevcid;
  9689. $range[$rangeid] = array();
  9690. $range[$rangeid][] = $prevwidth;
  9691. $range[$rangeid][] = $width;
  9692. }
  9693. $interval = true;
  9694. $range[$rangeid]['interval'] = true;
  9695. } else {
  9696. if ($interval) {
  9697. // new range
  9698. $rangeid = $cid;
  9699. $range[$rangeid] = array();
  9700. $range[$rangeid][] = $width;
  9701. } else {
  9702. $range[$rangeid][] = $width;
  9703. }
  9704. $interval = false;
  9705. }
  9706. } else {
  9707. // new range
  9708. $rangeid = $cid;
  9709. $range[$rangeid] = array();
  9710. $range[$rangeid][] = $width;
  9711. $interval = false;
  9712. }
  9713. $prevcid = $cid;
  9714. $prevwidth = $width;
  9715. }
  9716. }
  9717. // optimize ranges
  9718. $prevk = -1;
  9719. $nextk = -1;
  9720. $prevint = false;
  9721. foreach ($range as $k => $ws) {
  9722. $cws = count($ws);
  9723. if (($k == $nextk) AND (!$prevint) AND ((!isset($ws['interval'])) OR ($cws < 4))) {
  9724. if (isset($range[$k]['interval'])) {
  9725. unset($range[$k]['interval']);
  9726. }
  9727. $range[$prevk] = array_merge($range[$prevk], $range[$k]);
  9728. unset($range[$k]);
  9729. } else {
  9730. $prevk = $k;
  9731. }
  9732. $nextk = $k + $cws;
  9733. if (isset($ws['interval'])) {
  9734. if ($cws > 3) {
  9735. $prevint = true;
  9736. } else {
  9737. $prevint = false;
  9738. }
  9739. unset($range[$k]['interval']);
  9740. --$nextk;
  9741. } else {
  9742. $prevint = false;
  9743. }
  9744. }
  9745. // output data
  9746. $w = '';
  9747. foreach ($range as $k => $ws) {
  9748. if (count(array_count_values($ws)) == 1) {
  9749. // interval mode is more compact
  9750. $w .= ' '.$k.' '.($k + count($ws) - 1).' '.$ws[0];
  9751. } else {
  9752. // range mode
  9753. $w .= ' '.$k.' [ '.implode(' ', $ws).' ]';
  9754. }
  9755. }
  9756. return '/W ['.$w.' ]';
  9757. }
  9758. /**
  9759. * Output fonts.
  9760. * @author Nicola Asuni
  9761. * @protected
  9762. */
  9763. protected function _putfonts() {
  9764. $nf = $this->n;
  9765. foreach ($this->diffs as $diff) {
  9766. //Encodings
  9767. $this->_newobj();
  9768. $this->_out('<< /Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.'] >>'."\n".'endobj');
  9769. }
  9770. $mqr = $this->get_mqr();
  9771. $this->set_mqr(false);
  9772. foreach ($this->FontFiles as $file => $info) {
  9773. // search and get font file to embedd
  9774. $fontdir = $info['fontdir'];
  9775. $file = strtolower($file);
  9776. $fontfile = '';
  9777. // search files on various directories
  9778. if (($fontdir !== false) AND file_exists($fontdir.$file)) {
  9779. $fontfile = $fontdir.$file;
  9780. } elseif (file_exists($this->_getfontpath().$file)) {
  9781. $fontfile = $this->_getfontpath().$file;
  9782. } elseif (file_exists($file)) {
  9783. $fontfile = $file;
  9784. }
  9785. if (!$this->empty_string($fontfile)) {
  9786. $font = file_get_contents($fontfile);
  9787. $compressed = (substr($file, -2) == '.z');
  9788. if ((!$compressed) AND (isset($info['length2']))) {
  9789. $header = (ord($font{0}) == 128);
  9790. if ($header) {
  9791. // strip first binary header
  9792. $font = substr($font, 6);
  9793. }
  9794. if ($header AND (ord($font{$info['length1']}) == 128)) {
  9795. // strip second binary header
  9796. $font = substr($font, 0, $info['length1']).substr($font, ($info['length1'] + 6));
  9797. }
  9798. } elseif ($info['subset'] AND ((!$compressed) OR ($compressed AND function_exists('gzcompress')))) {
  9799. if ($compressed) {
  9800. // uncompress font
  9801. $font = gzuncompress($font);
  9802. }
  9803. // merge subset characters
  9804. $subsetchars = array(); // used chars
  9805. foreach ($info['fontkeys'] as $fontkey) {
  9806. $fontinfo = $this->getFontBuffer($fontkey);
  9807. $subsetchars += $fontinfo['subsetchars'];
  9808. }
  9809. // rebuild a font subset
  9810. $font = $this->_getTrueTypeFontSubset($font, $subsetchars);
  9811. // calculate new font length
  9812. $info['length1'] = strlen($font);
  9813. if ($compressed) {
  9814. // recompress font
  9815. $font = gzcompress($font);
  9816. }
  9817. }
  9818. $this->_newobj();
  9819. $this->FontFiles[$file]['n'] = $this->n;
  9820. $stream = $this->_getrawstream($font);
  9821. $out = '<< /Length '.strlen($stream);
  9822. if ($compressed) {
  9823. $out .= ' /Filter /FlateDecode';
  9824. }
  9825. $out .= ' /Length1 '.$info['length1'];
  9826. if (isset($info['length2'])) {
  9827. $out .= ' /Length2 '.$info['length2'].' /Length3 0';
  9828. }
  9829. $out .= ' >>';
  9830. $out .= ' stream'."\n".$stream."\n".'endstream';
  9831. $out .= "\n".'endobj';
  9832. $this->_out($out);
  9833. }
  9834. }
  9835. $this->set_mqr($mqr);
  9836. foreach ($this->fontkeys as $k) {
  9837. //Font objects
  9838. $font = $this->getFontBuffer($k);
  9839. $type = $font['type'];
  9840. $name = $font['name'];
  9841. if ($type == 'core') {
  9842. // standard core font
  9843. $out = $this->_getobj($this->font_obj_ids[$k])."\n";
  9844. $out .= '<</Type /Font';
  9845. $out .= ' /Subtype /Type1';
  9846. $out .= ' /BaseFont /'.$name;
  9847. $out .= ' /Name /F'.$font['i'];
  9848. if ((strtolower($name) != 'symbol') AND (strtolower($name) != 'zapfdingbats')) {
  9849. $out .= ' /Encoding /WinAnsiEncoding';
  9850. }
  9851. if ($k == 'helvetica') {
  9852. // add default font for annotations
  9853. $this->annotation_fonts[$k] = $font['i'];
  9854. }
  9855. $out .= ' >>';
  9856. $out .= "\n".'endobj';
  9857. $this->_out($out);
  9858. } elseif (($type == 'Type1') OR ($type == 'TrueType')) {
  9859. // additional Type1 or TrueType font
  9860. $out = $this->_getobj($this->font_obj_ids[$k])."\n";
  9861. $out .= '<</Type /Font';
  9862. $out .= ' /Subtype /'.$type;
  9863. $out .= ' /BaseFont /'.$name;
  9864. $out .= ' /Name /F'.$font['i'];
  9865. $out .= ' /FirstChar 32 /LastChar 255';
  9866. $out .= ' /Widths '.($this->n + 1).' 0 R';
  9867. $out .= ' /FontDescriptor '.($this->n + 2).' 0 R';
  9868. if ($font['enc']) {
  9869. if (isset($font['diff'])) {
  9870. $out .= ' /Encoding '.($nf + $font['diff']).' 0 R';
  9871. } else {
  9872. $out .= ' /Encoding /WinAnsiEncoding';
  9873. }
  9874. }
  9875. $out .= ' >>';
  9876. $out .= "\n".'endobj';
  9877. $this->_out($out);
  9878. // Widths
  9879. $this->_newobj();
  9880. $s = '[';
  9881. for ($i = 32; $i < 256; ++$i) {
  9882. $s .= $font['cw'][$i].' ';
  9883. }
  9884. $s .= ']';
  9885. $s .= "\n".'endobj';
  9886. $this->_out($s);
  9887. //Descriptor
  9888. $this->_newobj();
  9889. $s = '<</Type /FontDescriptor /FontName /'.$name;
  9890. foreach ($font['desc'] as $fdk => $fdv) {
  9891. if (is_float($fdv)) {
  9892. $fdv = sprintf('%.3F', $fdv);
  9893. }
  9894. $s .= ' /'.$fdk.' '.$fdv.'';
  9895. }
  9896. if (!$this->empty_string($font['file'])) {
  9897. $s .= ' /FontFile'.($type == 'Type1' ? '' : '2').' '.$this->FontFiles[$font['file']]['n'].' 0 R';
  9898. }
  9899. $s .= '>>';
  9900. $s .= "\n".'endobj';
  9901. $this->_out($s);
  9902. } else {
  9903. // additional types
  9904. $mtd = '_put'.strtolower($type);
  9905. if (!method_exists($this, $mtd)) {
  9906. $this->Error('Unsupported font type: '.$type);
  9907. }
  9908. $this->$mtd($font);
  9909. }
  9910. }
  9911. }
  9912. /**
  9913. * Adds unicode fonts.<br>
  9914. * Based on PDF Reference 1.3 (section 5)
  9915. * @param $font (array) font data
  9916. * @protected
  9917. * @author Nicola Asuni
  9918. * @since 1.52.0.TC005 (2005-01-05)
  9919. */
  9920. protected function _puttruetypeunicode($font) {
  9921. $fontname = '';
  9922. if ($font['subset']) {
  9923. // change name for font subsetting
  9924. $subtag = sprintf('%06u', $font['i']);
  9925. $subtag = strtr($subtag, '0123456789', 'ABCDEFGHIJ');
  9926. $fontname .= $subtag.'+';
  9927. }
  9928. $fontname .= $font['name'];
  9929. // Type0 Font
  9930. // A composite font composed of other fonts, organized hierarchically
  9931. $out = $this->_getobj($this->font_obj_ids[$font['fontkey']])."\n";
  9932. $out .= '<< /Type /Font';
  9933. $out .= ' /Subtype /Type0';
  9934. $out .= ' /BaseFont /'.$fontname;
  9935. $out .= ' /Name /F'.$font['i'];
  9936. $out .= ' /Encoding /'.$font['enc'];
  9937. $out .= ' /ToUnicode '.($this->n + 1).' 0 R';
  9938. $out .= ' /DescendantFonts ['.($this->n + 2).' 0 R]';
  9939. $out .= ' >>';
  9940. $out .= "\n".'endobj';
  9941. $this->_out($out);
  9942. // ToUnicode map for Identity-H
  9943. $stream = "/CIDInit /ProcSet findresource begin\n";
  9944. $stream .= "12 dict begin\n";
  9945. $stream .= "begincmap\n";
  9946. $stream .= "/CIDSystemInfo << /Registry (Adobe) /Ordering (UCS) /Supplement 0 >> def\n";
  9947. $stream .= "/CMapName /Adobe-Identity-UCS def\n";
  9948. $stream .= "/CMapType 2 def\n";
  9949. $stream .= "/WMode 0 def\n";
  9950. $stream .= "1 begincodespacerange\n";
  9951. $stream .= "<0000> <FFFF>\n";
  9952. $stream .= "endcodespacerange\n";
  9953. $stream .= "100 beginbfrange\n";
  9954. $stream .= "<0000> <00ff> <0000>\n";
  9955. $stream .= "<0100> <01ff> <0100>\n";
  9956. $stream .= "<0200> <02ff> <0200>\n";
  9957. $stream .= "<0300> <03ff> <0300>\n";
  9958. $stream .= "<0400> <04ff> <0400>\n";
  9959. $stream .= "<0500> <05ff> <0500>\n";
  9960. $stream .= "<0600> <06ff> <0600>\n";
  9961. $stream .= "<0700> <07ff> <0700>\n";
  9962. $stream .= "<0800> <08ff> <0800>\n";
  9963. $stream .= "<0900> <09ff> <0900>\n";
  9964. $stream .= "<0a00> <0aff> <0a00>\n";
  9965. $stream .= "<0b00> <0bff> <0b00>\n";
  9966. $stream .= "<0c00> <0cff> <0c00>\n";
  9967. $stream .= "<0d00> <0dff> <0d00>\n";
  9968. $stream .= "<0e00> <0eff> <0e00>\n";
  9969. $stream .= "<0f00> <0fff> <0f00>\n";
  9970. $stream .= "<1000> <10ff> <1000>\n";
  9971. $stream .= "<1100> <11ff> <1100>\n";
  9972. $stream .= "<1200> <12ff> <1200>\n";
  9973. $stream .= "<1300> <13ff> <1300>\n";
  9974. $stream .= "<1400> <14ff> <1400>\n";
  9975. $stream .= "<1500> <15ff> <1500>\n";
  9976. $stream .= "<1600> <16ff> <1600>\n";
  9977. $stream .= "<1700> <17ff> <1700>\n";
  9978. $stream .= "<1800> <18ff> <1800>\n";
  9979. $stream .= "<1900> <19ff> <1900>\n";
  9980. $stream .= "<1a00> <1aff> <1a00>\n";
  9981. $stream .= "<1b00> <1bff> <1b00>\n";
  9982. $stream .= "<1c00> <1cff> <1c00>\n";
  9983. $stream .= "<1d00> <1dff> <1d00>\n";
  9984. $stream .= "<1e00> <1eff> <1e00>\n";
  9985. $stream .= "<1f00> <1fff> <1f00>\n";
  9986. $stream .= "<2000> <20ff> <2000>\n";
  9987. $stream .= "<2100> <21ff> <2100>\n";
  9988. $stream .= "<2200> <22ff> <2200>\n";
  9989. $stream .= "<2300> <23ff> <2300>\n";
  9990. $stream .= "<2400> <24ff> <2400>\n";
  9991. $stream .= "<2500> <25ff> <2500>\n";
  9992. $stream .= "<2600> <26ff> <2600>\n";
  9993. $stream .= "<2700> <27ff> <2700>\n";
  9994. $stream .= "<2800> <28ff> <2800>\n";
  9995. $stream .= "<2900> <29ff> <2900>\n";
  9996. $stream .= "<2a00> <2aff> <2a00>\n";
  9997. $stream .= "<2b00> <2bff> <2b00>\n";
  9998. $stream .= "<2c00> <2cff> <2c00>\n";
  9999. $stream .= "<2d00> <2dff> <2d00>\n";
  10000. $stream .= "<2e00> <2eff> <2e00>\n";
  10001. $stream .= "<2f00> <2fff> <2f00>\n";
  10002. $stream .= "<3000> <30ff> <3000>\n";
  10003. $stream .= "<3100> <31ff> <3100>\n";
  10004. $stream .= "<3200> <32ff> <3200>\n";
  10005. $stream .= "<3300> <33ff> <3300>\n";
  10006. $stream .= "<3400> <34ff> <3400>\n";
  10007. $stream .= "<3500> <35ff> <3500>\n";
  10008. $stream .= "<3600> <36ff> <3600>\n";
  10009. $stream .= "<3700> <37ff> <3700>\n";
  10010. $stream .= "<3800> <38ff> <3800>\n";
  10011. $stream .= "<3900> <39ff> <3900>\n";
  10012. $stream .= "<3a00> <3aff> <3a00>\n";
  10013. $stream .= "<3b00> <3bff> <3b00>\n";
  10014. $stream .= "<3c00> <3cff> <3c00>\n";
  10015. $stream .= "<3d00> <3dff> <3d00>\n";
  10016. $stream .= "<3e00> <3eff> <3e00>\n";
  10017. $stream .= "<3f00> <3fff> <3f00>\n";
  10018. $stream .= "<4000> <40ff> <4000>\n";
  10019. $stream .= "<4100> <41ff> <4100>\n";
  10020. $stream .= "<4200> <42ff> <4200>\n";
  10021. $stream .= "<4300> <43ff> <4300>\n";
  10022. $stream .= "<4400> <44ff> <4400>\n";
  10023. $stream .= "<4500> <45ff> <4500>\n";
  10024. $stream .= "<4600> <46ff> <4600>\n";
  10025. $stream .= "<4700> <47ff> <4700>\n";
  10026. $stream .= "<4800> <48ff> <4800>\n";
  10027. $stream .= "<4900> <49ff> <4900>\n";
  10028. $stream .= "<4a00> <4aff> <4a00>\n";
  10029. $stream .= "<4b00> <4bff> <4b00>\n";
  10030. $stream .= "<4c00> <4cff> <4c00>\n";
  10031. $stream .= "<4d00> <4dff> <4d00>\n";
  10032. $stream .= "<4e00> <4eff> <4e00>\n";
  10033. $stream .= "<4f00> <4fff> <4f00>\n";
  10034. $stream .= "<5000> <50ff> <5000>\n";
  10035. $stream .= "<5100> <51ff> <5100>\n";
  10036. $stream .= "<5200> <52ff> <5200>\n";
  10037. $stream .= "<5300> <53ff> <5300>\n";
  10038. $stream .= "<5400> <54ff> <5400>\n";
  10039. $stream .= "<5500> <55ff> <5500>\n";
  10040. $stream .= "<5600> <56ff> <5600>\n";
  10041. $stream .= "<5700> <57ff> <5700>\n";
  10042. $stream .= "<5800> <58ff> <5800>\n";
  10043. $stream .= "<5900> <59ff> <5900>\n";
  10044. $stream .= "<5a00> <5aff> <5a00>\n";
  10045. $stream .= "<5b00> <5bff> <5b00>\n";
  10046. $stream .= "<5c00> <5cff> <5c00>\n";
  10047. $stream .= "<5d00> <5dff> <5d00>\n";
  10048. $stream .= "<5e00> <5eff> <5e00>\n";
  10049. $stream .= "<5f00> <5fff> <5f00>\n";
  10050. $stream .= "<6000> <60ff> <6000>\n";
  10051. $stream .= "<6100> <61ff> <6100>\n";
  10052. $stream .= "<6200> <62ff> <6200>\n";
  10053. $stream .= "<6300> <63ff> <6300>\n";
  10054. $stream .= "endbfrange\n";
  10055. $stream .= "100 beginbfrange\n";
  10056. $stream .= "<6400> <64ff> <6400>\n";
  10057. $stream .= "<6500> <65ff> <6500>\n";
  10058. $stream .= "<6600> <66ff> <6600>\n";
  10059. $stream .= "<6700> <67ff> <6700>\n";
  10060. $stream .= "<6800> <68ff> <6800>\n";
  10061. $stream .= "<6900> <69ff> <6900>\n";
  10062. $stream .= "<6a00> <6aff> <6a00>\n";
  10063. $stream .= "<6b00> <6bff> <6b00>\n";
  10064. $stream .= "<6c00> <6cff> <6c00>\n";
  10065. $stream .= "<6d00> <6dff> <6d00>\n";
  10066. $stream .= "<6e00> <6eff> <6e00>\n";
  10067. $stream .= "<6f00> <6fff> <6f00>\n";
  10068. $stream .= "<7000> <70ff> <7000>\n";
  10069. $stream .= "<7100> <71ff> <7100>\n";
  10070. $stream .= "<7200> <72ff> <7200>\n";
  10071. $stream .= "<7300> <73ff> <7300>\n";
  10072. $stream .= "<7400> <74ff> <7400>\n";
  10073. $stream .= "<7500> <75ff> <7500>\n";
  10074. $stream .= "<7600> <76ff> <7600>\n";
  10075. $stream .= "<7700> <77ff> <7700>\n";
  10076. $stream .= "<7800> <78ff> <7800>\n";
  10077. $stream .= "<7900> <79ff> <7900>\n";
  10078. $stream .= "<7a00> <7aff> <7a00>\n";
  10079. $stream .= "<7b00> <7bff> <7b00>\n";
  10080. $stream .= "<7c00> <7cff> <7c00>\n";
  10081. $stream .= "<7d00> <7dff> <7d00>\n";
  10082. $stream .= "<7e00> <7eff> <7e00>\n";
  10083. $stream .= "<7f00> <7fff> <7f00>\n";
  10084. $stream .= "<8000> <80ff> <8000>\n";
  10085. $stream .= "<8100> <81ff> <8100>\n";
  10086. $stream .= "<8200> <82ff> <8200>\n";
  10087. $stream .= "<8300> <83ff> <8300>\n";
  10088. $stream .= "<8400> <84ff> <8400>\n";
  10089. $stream .= "<8500> <85ff> <8500>\n";
  10090. $stream .= "<8600> <86ff> <8600>\n";
  10091. $stream .= "<8700> <87ff> <8700>\n";
  10092. $stream .= "<8800> <88ff> <8800>\n";
  10093. $stream .= "<8900> <89ff> <8900>\n";
  10094. $stream .= "<8a00> <8aff> <8a00>\n";
  10095. $stream .= "<8b00> <8bff> <8b00>\n";
  10096. $stream .= "<8c00> <8cff> <8c00>\n";
  10097. $stream .= "<8d00> <8dff> <8d00>\n";
  10098. $stream .= "<8e00> <8eff> <8e00>\n";
  10099. $stream .= "<8f00> <8fff> <8f00>\n";
  10100. $stream .= "<9000> <90ff> <9000>\n";
  10101. $stream .= "<9100> <91ff> <9100>\n";
  10102. $stream .= "<9200> <92ff> <9200>\n";
  10103. $stream .= "<9300> <93ff> <9300>\n";
  10104. $stream .= "<9400> <94ff> <9400>\n";
  10105. $stream .= "<9500> <95ff> <9500>\n";
  10106. $stream .= "<9600> <96ff> <9600>\n";
  10107. $stream .= "<9700> <97ff> <9700>\n";
  10108. $stream .= "<9800> <98ff> <9800>\n";
  10109. $stream .= "<9900> <99ff> <9900>\n";
  10110. $stream .= "<9a00> <9aff> <9a00>\n";
  10111. $stream .= "<9b00> <9bff> <9b00>\n";
  10112. $stream .= "<9c00> <9cff> <9c00>\n";
  10113. $stream .= "<9d00> <9dff> <9d00>\n";
  10114. $stream .= "<9e00> <9eff> <9e00>\n";
  10115. $stream .= "<9f00> <9fff> <9f00>\n";
  10116. $stream .= "<a000> <a0ff> <a000>\n";
  10117. $stream .= "<a100> <a1ff> <a100>\n";
  10118. $stream .= "<a200> <a2ff> <a200>\n";
  10119. $stream .= "<a300> <a3ff> <a300>\n";
  10120. $stream .= "<a400> <a4ff> <a400>\n";
  10121. $stream .= "<a500> <a5ff> <a500>\n";
  10122. $stream .= "<a600> <a6ff> <a600>\n";
  10123. $stream .= "<a700> <a7ff> <a700>\n";
  10124. $stream .= "<a800> <a8ff> <a800>\n";
  10125. $stream .= "<a900> <a9ff> <a900>\n";
  10126. $stream .= "<aa00> <aaff> <aa00>\n";
  10127. $stream .= "<ab00> <abff> <ab00>\n";
  10128. $stream .= "<ac00> <acff> <ac00>\n";
  10129. $stream .= "<ad00> <adff> <ad00>\n";
  10130. $stream .= "<ae00> <aeff> <ae00>\n";
  10131. $stream .= "<af00> <afff> <af00>\n";
  10132. $stream .= "<b000> <b0ff> <b000>\n";
  10133. $stream .= "<b100> <b1ff> <b100>\n";
  10134. $stream .= "<b200> <b2ff> <b200>\n";
  10135. $stream .= "<b300> <b3ff> <b300>\n";
  10136. $stream .= "<b400> <b4ff> <b400>\n";
  10137. $stream .= "<b500> <b5ff> <b500>\n";
  10138. $stream .= "<b600> <b6ff> <b600>\n";
  10139. $stream .= "<b700> <b7ff> <b700>\n";
  10140. $stream .= "<b800> <b8ff> <b800>\n";
  10141. $stream .= "<b900> <b9ff> <b900>\n";
  10142. $stream .= "<ba00> <baff> <ba00>\n";
  10143. $stream .= "<bb00> <bbff> <bb00>\n";
  10144. $stream .= "<bc00> <bcff> <bc00>\n";
  10145. $stream .= "<bd00> <bdff> <bd00>\n";
  10146. $stream .= "<be00> <beff> <be00>\n";
  10147. $stream .= "<bf00> <bfff> <bf00>\n";
  10148. $stream .= "<c000> <c0ff> <c000>\n";
  10149. $stream .= "<c100> <c1ff> <c100>\n";
  10150. $stream .= "<c200> <c2ff> <c200>\n";
  10151. $stream .= "<c300> <c3ff> <c300>\n";
  10152. $stream .= "<c400> <c4ff> <c400>\n";
  10153. $stream .= "<c500> <c5ff> <c500>\n";
  10154. $stream .= "<c600> <c6ff> <c600>\n";
  10155. $stream .= "<c700> <c7ff> <c700>\n";
  10156. $stream .= "endbfrange\n";
  10157. $stream .= "56 beginbfrange\n";
  10158. $stream .= "<c800> <c8ff> <c800>\n";
  10159. $stream .= "<c900> <c9ff> <c900>\n";
  10160. $stream .= "<ca00> <caff> <ca00>\n";
  10161. $stream .= "<cb00> <cbff> <cb00>\n";
  10162. $stream .= "<cc00> <ccff> <cc00>\n";
  10163. $stream .= "<cd00> <cdff> <cd00>\n";
  10164. $stream .= "<ce00> <ceff> <ce00>\n";
  10165. $stream .= "<cf00> <cfff> <cf00>\n";
  10166. $stream .= "<d000> <d0ff> <d000>\n";
  10167. $stream .= "<d100> <d1ff> <d100>\n";
  10168. $stream .= "<d200> <d2ff> <d200>\n";
  10169. $stream .= "<d300> <d3ff> <d300>\n";
  10170. $stream .= "<d400> <d4ff> <d400>\n";
  10171. $stream .= "<d500> <d5ff> <d500>\n";
  10172. $stream .= "<d600> <d6ff> <d600>\n";
  10173. $stream .= "<d700> <d7ff> <d700>\n";
  10174. $stream .= "<d800> <d8ff> <d800>\n";
  10175. $stream .= "<d900> <d9ff> <d900>\n";
  10176. $stream .= "<da00> <daff> <da00>\n";
  10177. $stream .= "<db00> <dbff> <db00>\n";
  10178. $stream .= "<dc00> <dcff> <dc00>\n";
  10179. $stream .= "<dd00> <ddff> <dd00>\n";
  10180. $stream .= "<de00> <deff> <de00>\n";
  10181. $stream .= "<df00> <dfff> <df00>\n";
  10182. $stream .= "<e000> <e0ff> <e000>\n";
  10183. $stream .= "<e100> <e1ff> <e100>\n";
  10184. $stream .= "<e200> <e2ff> <e200>\n";
  10185. $stream .= "<e300> <e3ff> <e300>\n";
  10186. $stream .= "<e400> <e4ff> <e400>\n";
  10187. $stream .= "<e500> <e5ff> <e500>\n";
  10188. $stream .= "<e600> <e6ff> <e600>\n";
  10189. $stream .= "<e700> <e7ff> <e700>\n";
  10190. $stream .= "<e800> <e8ff> <e800>\n";
  10191. $stream .= "<e900> <e9ff> <e900>\n";
  10192. $stream .= "<ea00> <eaff> <ea00>\n";
  10193. $stream .= "<eb00> <ebff> <eb00>\n";
  10194. $stream .= "<ec00> <ecff> <ec00>\n";
  10195. $stream .= "<ed00> <edff> <ed00>\n";
  10196. $stream .= "<ee00> <eeff> <ee00>\n";
  10197. $stream .= "<ef00> <efff> <ef00>\n";
  10198. $stream .= "<f000> <f0ff> <f000>\n";
  10199. $stream .= "<f100> <f1ff> <f100>\n";
  10200. $stream .= "<f200> <f2ff> <f200>\n";
  10201. $stream .= "<f300> <f3ff> <f300>\n";
  10202. $stream .= "<f400> <f4ff> <f400>\n";
  10203. $stream .= "<f500> <f5ff> <f500>\n";
  10204. $stream .= "<f600> <f6ff> <f600>\n";
  10205. $stream .= "<f700> <f7ff> <f700>\n";
  10206. $stream .= "<f800> <f8ff> <f800>\n";
  10207. $stream .= "<f900> <f9ff> <f900>\n";
  10208. $stream .= "<fa00> <faff> <fa00>\n";
  10209. $stream .= "<fb00> <fbff> <fb00>\n";
  10210. $stream .= "<fc00> <fcff> <fc00>\n";
  10211. $stream .= "<fd00> <fdff> <fd00>\n";
  10212. $stream .= "<fe00> <feff> <fe00>\n";
  10213. $stream .= "<ff00> <ffff> <ff00>\n";
  10214. $stream .= "endbfrange\n";
  10215. $stream .= "endcmap\n";
  10216. $stream .= "CMapName currentdict /CMap defineresource pop\n";
  10217. $stream .= "end\n";
  10218. $stream .= "end";
  10219. // ToUnicode Object
  10220. $this->_newobj();
  10221. $stream = ($this->compress) ? gzcompress($stream) : $stream;
  10222. $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
  10223. $stream = $this->_getrawstream($stream);
  10224. $this->_out('<<'.$filter.'/Length '.strlen($stream).'>> stream'."\n".$stream."\n".'endstream'."\n".'endobj');
  10225. // CIDFontType2
  10226. // A CIDFont whose glyph descriptions are based on TrueType font technology
  10227. $oid = $this->_newobj();
  10228. $out = '<< /Type /Font';
  10229. $out .= ' /Subtype /CIDFontType2';
  10230. $out .= ' /BaseFont /'.$fontname;
  10231. // A dictionary containing entries that define the character collection of the CIDFont.
  10232. $cidinfo = '/Registry '.$this->_datastring($font['cidinfo']['Registry'], $oid);
  10233. $cidinfo .= ' /Ordering '.$this->_datastring($font['cidinfo']['Ordering'], $oid);
  10234. $cidinfo .= ' /Supplement '.$font['cidinfo']['Supplement'];
  10235. $out .= ' /CIDSystemInfo << '.$cidinfo.' >>';
  10236. $out .= ' /FontDescriptor '.($this->n + 1).' 0 R';
  10237. $out .= ' /DW '.$font['dw']; // default width
  10238. $out .= "\n".$this->_putfontwidths($font, 0);
  10239. if (isset($font['ctg']) AND (!$this->empty_string($font['ctg']))) {
  10240. $out .= "\n".'/CIDToGIDMap '.($this->n + 2).' 0 R';
  10241. }
  10242. $out .= ' >>';
  10243. $out .= "\n".'endobj';
  10244. $this->_out($out);
  10245. // Font descriptor
  10246. // A font descriptor describing the CIDFont default metrics other than its glyph widths
  10247. $this->_newobj();
  10248. $out = '<< /Type /FontDescriptor';
  10249. $out .= ' /FontName /'.$fontname;
  10250. foreach ($font['desc'] as $key => $value) {
  10251. if (is_float($value)) {
  10252. $value = sprintf('%.3F', $value);
  10253. }
  10254. $out .= ' /'.$key.' '.$value;
  10255. }
  10256. $fontdir = false;
  10257. if (!$this->empty_string($font['file'])) {
  10258. // A stream containing a TrueType font
  10259. $out .= ' /FontFile2 '.$this->FontFiles[$font['file']]['n'].' 0 R';
  10260. $fontdir = $this->FontFiles[$font['file']]['fontdir'];
  10261. }
  10262. $out .= ' >>';
  10263. $out .= "\n".'endobj';
  10264. $this->_out($out);
  10265. if (isset($font['ctg']) AND (!$this->empty_string($font['ctg']))) {
  10266. $this->_newobj();
  10267. // Embed CIDToGIDMap
  10268. // A specification of the mapping from CIDs to glyph indices
  10269. // search and get CTG font file to embedd
  10270. $ctgfile = strtolower($font['ctg']);
  10271. // search and get ctg font file to embedd
  10272. $fontfile = '';
  10273. // search files on various directories
  10274. if (($fontdir !== false) AND file_exists($fontdir.$ctgfile)) {
  10275. $fontfile = $fontdir.$ctgfile;
  10276. } elseif (file_exists($this->_getfontpath().$ctgfile)) {
  10277. $fontfile = $this->_getfontpath().$ctgfile;
  10278. } elseif (file_exists($ctgfile)) {
  10279. $fontfile = $ctgfile;
  10280. }
  10281. if ($this->empty_string($fontfile)) {
  10282. $this->Error('Font file not found: '.$ctgfile);
  10283. }
  10284. $stream = $this->_getrawstream(file_get_contents($fontfile));
  10285. $out = '<< /Length '.strlen($stream).'';
  10286. if (substr($fontfile, -2) == '.z') { // check file extension
  10287. // Decompresses data encoded using the public-domain
  10288. // zlib/deflate compression method, reproducing the
  10289. // original text or binary data
  10290. $out .= ' /Filter /FlateDecode';
  10291. }
  10292. $out .= ' >>';
  10293. $out .= ' stream'."\n".$stream."\n".'endstream';
  10294. $out .= "\n".'endobj';
  10295. $this->_out($out);
  10296. }
  10297. }
  10298. /**
  10299. * Output CID-0 fonts.
  10300. * A Type 0 CIDFont contains glyph descriptions based on the Adobe Type 1 font format
  10301. * @param $font (array) font data
  10302. * @protected
  10303. * @author Andrew Whitehead, Nicola Asuni, Yukihiro Nakadaira
  10304. * @since 3.2.000 (2008-06-23)
  10305. */
  10306. protected function _putcidfont0($font) {
  10307. $cidoffset = 0;
  10308. if (!isset($font['cw'][1])) {
  10309. $cidoffset = 31;
  10310. }
  10311. if (isset($font['cidinfo']['uni2cid'])) {
  10312. // convert unicode to cid.
  10313. $uni2cid = $font['cidinfo']['uni2cid'];
  10314. $cw = array();
  10315. foreach ($font['cw'] as $uni => $width) {
  10316. if (isset($uni2cid[$uni])) {
  10317. $cw[($uni2cid[$uni] + $cidoffset)] = $width;
  10318. } elseif ($uni < 256) {
  10319. $cw[$uni] = $width;
  10320. } // else unknown character
  10321. }
  10322. $font = array_merge($font, array('cw' => $cw));
  10323. }
  10324. $name = $font['name'];
  10325. $enc = $font['enc'];
  10326. if ($enc) {
  10327. $longname = $name.'-'.$enc;
  10328. } else {
  10329. $longname = $name;
  10330. }
  10331. $out = $this->_getobj($this->font_obj_ids[$font['fontkey']])."\n";
  10332. $out .= '<</Type /Font';
  10333. $out .= ' /Subtype /Type0';
  10334. $out .= ' /BaseFont /'.$longname;
  10335. $out .= ' /Name /F'.$font['i'];
  10336. if ($enc) {
  10337. $out .= ' /Encoding /'.$enc;
  10338. }
  10339. $out .= ' /DescendantFonts ['.($this->n + 1).' 0 R]';
  10340. $out .= ' >>';
  10341. $out .= "\n".'endobj';
  10342. $this->_out($out);
  10343. $oid = $this->_newobj();
  10344. $out = '<</Type /Font';
  10345. $out .= ' /Subtype /CIDFontType0';
  10346. $out .= ' /BaseFont /'.$name;
  10347. $cidinfo = '/Registry '.$this->_datastring($font['cidinfo']['Registry'], $oid);
  10348. $cidinfo .= ' /Ordering '.$this->_datastring($font['cidinfo']['Ordering'], $oid);
  10349. $cidinfo .= ' /Supplement '.$font['cidinfo']['Supplement'];
  10350. $out .= ' /CIDSystemInfo <<'.$cidinfo.'>>';
  10351. $out .= ' /FontDescriptor '.($this->n + 1).' 0 R';
  10352. $out .= ' /DW '.$font['dw'];
  10353. $out .= "\n".$this->_putfontwidths($font, $cidoffset);
  10354. $out .= ' >>';
  10355. $out .= "\n".'endobj';
  10356. $this->_out($out);
  10357. $this->_newobj();
  10358. $s = '<</Type /FontDescriptor /FontName /'.$name;
  10359. foreach ($font['desc'] as $k => $v) {
  10360. if ($k != 'Style') {
  10361. if (is_float($v)) {
  10362. $v = sprintf('%.3F', $v);
  10363. }
  10364. $s .= ' /'.$k.' '.$v.'';
  10365. }
  10366. }
  10367. $s .= '>>';
  10368. $s .= "\n".'endobj';
  10369. $this->_out($s);
  10370. }
  10371. /**
  10372. * Output images.
  10373. * @protected
  10374. */
  10375. protected function _putimages() {
  10376. $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
  10377. foreach ($this->imagekeys as $file) {
  10378. $info = $this->getImageBuffer($file);
  10379. // set object for alternate images array
  10380. if (!empty($info['altimgs'])) {
  10381. $altoid = $this->_newobj();
  10382. $out = '[';
  10383. foreach ($info['altimgs'] as $altimage) {
  10384. if (isset($this->xobjects['I'.$altimage[0]]['n'])) {
  10385. $out .= ' << /Image '.$this->xobjects['I'.$altimage[0]]['n'].' 0 R';
  10386. $out .= ' /DefaultForPrinting';
  10387. if ($altimage[1] === true) {
  10388. $out .= ' true';
  10389. } else {
  10390. $out .= ' false';
  10391. }
  10392. $out .= ' >>';
  10393. }
  10394. }
  10395. $out .= ' ]';
  10396. $out .= "\n".'endobj';
  10397. $this->_out($out);
  10398. }
  10399. // set image object
  10400. $oid = $this->_newobj();
  10401. $this->xobjects['I'.$info['i']] = array('n' => $oid);
  10402. $this->setImageSubBuffer($file, 'n', $this->n);
  10403. $out = '<</Type /XObject';
  10404. $out .= ' /Subtype /Image';
  10405. $out .= ' /Width '.$info['w'];
  10406. $out .= ' /Height '.$info['h'];
  10407. if (array_key_exists('masked', $info)) {
  10408. $out .= ' /SMask '.($this->n - 1).' 0 R';
  10409. }
  10410. // set color space
  10411. $icc = false;
  10412. if (isset($info['icc']) AND ($info['icc'] !== false)) {
  10413. // ICC Colour Space
  10414. $icc = true;
  10415. $out .= ' /ColorSpace [/ICCBased '.($this->n + 1).' 0 R]';
  10416. } elseif ($info['cs'] == 'Indexed') {
  10417. // Indexed Colour Space
  10418. $out .= ' /ColorSpace [/Indexed /DeviceRGB '.((strlen($info['pal']) / 3) - 1).' '.($this->n + 1).' 0 R]';
  10419. } else {
  10420. // Device Colour Space
  10421. $out .= ' /ColorSpace /'.$info['cs'];
  10422. }
  10423. if ($info['cs'] == 'DeviceCMYK') {
  10424. $out .= ' /Decode [1 0 1 0 1 0 1 0]';
  10425. }
  10426. $out .= ' /BitsPerComponent '.$info['bpc'];
  10427. if (isset($altoid) AND ($altoid > 0)) {
  10428. // reference to alternate images dictionary
  10429. $out .= ' /Alternates '.$altoid.' 0 R';
  10430. }
  10431. if (isset($info['exurl']) AND !empty($info['exurl'])) {
  10432. // external stream
  10433. $out .= ' /Length 0';
  10434. $out .= ' /F << /FS /URL /F '.$this->_datastring($info['exurl'], $oid).' >>';
  10435. if (isset($info['f'])) {
  10436. $out .= ' /FFilter /'.$info['f'];
  10437. }
  10438. $out .= ' >>';
  10439. $out .= ' stream'."\n".'endstream';
  10440. } else {
  10441. if (isset($info['f'])) {
  10442. $out .= ' /Filter /'.$info['f'];
  10443. }
  10444. if (isset($info['parms'])) {
  10445. $out .= ' '.$info['parms'];
  10446. }
  10447. if (isset($info['trns']) AND is_array($info['trns'])) {
  10448. $trns = '';
  10449. $count_info = count($info['trns']);
  10450. for ($i=0; $i < $count_info; ++$i) {
  10451. $trns .= $info['trns'][$i].' '.$info['trns'][$i].' ';
  10452. }
  10453. $out .= ' /Mask ['.$trns.']';
  10454. }
  10455. $stream = $this->_getrawstream($info['data']);
  10456. $out .= ' /Length '.strlen($stream).' >>';
  10457. $out .= ' stream'."\n".$stream."\n".'endstream';
  10458. }
  10459. $out .= "\n".'endobj';
  10460. $this->_out($out);
  10461. if ($icc) {
  10462. // ICC colour profile
  10463. $this->_newobj();
  10464. $icc = ($this->compress) ? gzcompress($info['icc']) : $info['icc'];
  10465. $icc = $this->_getrawstream($icc);
  10466. $this->_out('<</N '.$info['ch'].' /Alternate /'.$info['cs'].' '.$filter.'/Length '.strlen($icc).'>> stream'."\n".$icc."\n".'endstream'."\n".'endobj');
  10467. } elseif ($info['cs'] == 'Indexed') {
  10468. // colour palette
  10469. $this->_newobj();
  10470. $pal = ($this->compress) ? gzcompress($info['pal']) : $info['pal'];
  10471. $pal = $this->_getrawstream($pal);
  10472. $this->_out('<<'.$filter.'/Length '.strlen($pal).'>> stream'."\n".$pal."\n".'endstream'."\n".'endobj');
  10473. }
  10474. }
  10475. }
  10476. /**
  10477. * Output Form XObjects Templates.
  10478. * @author Nicola Asuni
  10479. * @since 5.8.017 (2010-08-24)
  10480. * @protected
  10481. * @see startTemplate(), endTemplate(), printTemplate()
  10482. */
  10483. protected function _putxobjects() {
  10484. foreach ($this->xobjects as $key => $data) {
  10485. if (isset($data['outdata'])) {
  10486. $stream = trim($data['outdata']);
  10487. $out = $this->_getobj($data['n'])."\n";
  10488. $out .= '<<';
  10489. $out .= ' /Type /XObject';
  10490. $out .= ' /Subtype /Form';
  10491. $out .= ' /FormType 1';
  10492. if ($this->compress) {
  10493. $stream = gzcompress($stream);
  10494. $out .= ' /Filter /FlateDecode';
  10495. }
  10496. $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));
  10497. $out .= ' /Matrix [1 0 0 1 0 0]';
  10498. $out .= ' /Resources <<';
  10499. $out .= ' /ProcSet [/PDF /Text /ImageB /ImageC /ImageI]';
  10500. // fonts
  10501. if (!empty($data['fonts'])) {
  10502. $out .= ' /Font <<';
  10503. foreach ($data['fonts'] as $fontkey => $fontid) {
  10504. $out .= ' /F'.$fontid.' '.$this->font_obj_ids[$fontkey].' 0 R';
  10505. }
  10506. $out .= ' >>';
  10507. }
  10508. // images or nested xobjects
  10509. if (!empty($data['images']) OR !empty($data['xobjects'])) {
  10510. $out .= ' /XObject <<';
  10511. foreach ($data['images'] as $imgid) {
  10512. $out .= ' /I'.$imgid.' '.$this->xobjects['I'.$imgid]['n'].' 0 R';
  10513. }
  10514. foreach ($data['xobjects'] as $sub_id => $sub_objid) {
  10515. $out .= ' /'.$sub_id.' '.$sub_objid['n'].' 0 R';
  10516. }
  10517. $out .= ' >>';
  10518. }
  10519. $out .= ' >>'; //end resources
  10520. if (isset($data['group']) AND ($data['group'] !== false)) {
  10521. // set transparency group
  10522. $out .= ' /Group << /Type /Group /S /Transparency';
  10523. if (is_array($data['group'])) {
  10524. if (isset($data['group']['CS']) AND !empty($data['group']['CS'])) {
  10525. $out .= ' /CS /'.$data['group']['CS'];
  10526. }
  10527. if (isset($data['group']['I'])) {
  10528. $out .= ' /I /'.($data['group']['I']===true?'true':'false');
  10529. }
  10530. if (isset($data['group']['K'])) {
  10531. $out .= ' /K /'.($data['group']['K']===true?'true':'false');
  10532. }
  10533. }
  10534. $out .= ' >>';
  10535. }
  10536. $stream = $this->_getrawstream($stream, $data['n']);
  10537. $out .= ' /Length '.strlen($stream);
  10538. $out .= ' >>';
  10539. $out .= ' stream'."\n".$stream."\n".'endstream';
  10540. $out .= "\n".'endobj';
  10541. $this->_out($out);
  10542. }
  10543. }
  10544. }
  10545. /**
  10546. * Output Spot Colors Resources.
  10547. * @protected
  10548. * @since 4.0.024 (2008-09-12)
  10549. */
  10550. protected function _putspotcolors() {
  10551. foreach ($this->spot_colors as $name => $color) {
  10552. $this->_newobj();
  10553. $this->spot_colors[$name]['n'] = $this->n;
  10554. $out = '[/Separation /'.str_replace(' ', '#20', $name);
  10555. $out .= ' /DeviceCMYK <<';
  10556. $out .= ' /Range [0 1 0 1 0 1 0 1] /C0 [0 0 0 0]';
  10557. $out .= ' '.sprintf('/C1 [%.4F %.4F %.4F %.4F] ', ($color['c'] / 100), ($color['m'] / 100), ($color['y'] / 100), ($color['k'] / 100));
  10558. $out .= ' /FunctionType 2 /Domain [0 1] /N 1>>]';
  10559. $out .= "\n".'endobj';
  10560. $this->_out($out);
  10561. }
  10562. }
  10563. /**
  10564. * Return XObjects Dictionary.
  10565. * @return string XObjects dictionary
  10566. * @protected
  10567. * @since 5.8.014 (2010-08-23)
  10568. */
  10569. protected function _getxobjectdict() {
  10570. $out = '';
  10571. foreach ($this->xobjects as $id => $objid) {
  10572. $out .= ' /'.$id.' '.$objid['n'].' 0 R';
  10573. }
  10574. return $out;
  10575. }
  10576. /**
  10577. * Output Resources Dictionary.
  10578. * @protected
  10579. */
  10580. protected function _putresourcedict() {
  10581. $out = $this->_getobj(2)."\n";
  10582. $out .= '<< /ProcSet [/PDF /Text /ImageB /ImageC /ImageI]';
  10583. $out .= ' /Font <<';
  10584. foreach ($this->fontkeys as $fontkey) {
  10585. $font = $this->getFontBuffer($fontkey);
  10586. $out .= ' /F'.$font['i'].' '.$font['n'].' 0 R';
  10587. }
  10588. $out .= ' >>';
  10589. $out .= ' /XObject <<';
  10590. $out .= $this->_getxobjectdict();
  10591. $out .= ' >>';
  10592. // layers
  10593. if (!empty($this->pdflayers)) {
  10594. $out .= ' /Properties <<';
  10595. foreach ($this->pdflayers as $layer) {
  10596. $out .= ' /'.$layer['layer'].' '.$layer['objid'].' 0 R';
  10597. }
  10598. $out .= ' >>';
  10599. }
  10600. // transparency
  10601. $out .= ' /ExtGState <<';
  10602. foreach ($this->extgstates as $k => $extgstate) {
  10603. if (isset($extgstate['name'])) {
  10604. $out .= ' /'.$extgstate['name'];
  10605. } else {
  10606. $out .= ' /GS'.$k;
  10607. }
  10608. $out .= ' '.$extgstate['n'].' 0 R';
  10609. }
  10610. $out .= ' >>';
  10611. // gradient patterns
  10612. if (isset($this->gradients) AND (count($this->gradients) > 0)) {
  10613. $out .= ' /Pattern <<';
  10614. foreach ($this->gradients as $id => $grad) {
  10615. $out .= ' /p'.$id.' '.$grad['pattern'].' 0 R';
  10616. }
  10617. $out .= ' >>';
  10618. }
  10619. // gradient shadings
  10620. if (isset($this->gradients) AND (count($this->gradients) > 0)) {
  10621. $out .= ' /Shading <<';
  10622. foreach ($this->gradients as $id => $grad) {
  10623. $out .= ' /Sh'.$id.' '.$grad['id'].' 0 R';
  10624. }
  10625. $out .= ' >>';
  10626. }
  10627. // spot colors
  10628. if (isset($this->spot_colors) AND (count($this->spot_colors) > 0)) {
  10629. $out .= ' /ColorSpace <<';
  10630. foreach ($this->spot_colors as $color) {
  10631. $out .= ' /CS'.$color['i'].' '.$color['n'].' 0 R';
  10632. }
  10633. $out .= ' >>';
  10634. }
  10635. $out .= ' >>';
  10636. $out .= "\n".'endobj';
  10637. $this->_out($out);
  10638. }
  10639. /**
  10640. * Output Resources.
  10641. * @protected
  10642. */
  10643. protected function _putresources() {
  10644. $this->_putextgstates();
  10645. $this->_putocg();
  10646. $this->_putfonts();
  10647. $this->_putimages();
  10648. $this->_putxobjects();
  10649. $this->_putspotcolors();
  10650. $this->_putshaders();
  10651. $this->_putresourcedict();
  10652. $this->_putdests();
  10653. $this->_putbookmarks();
  10654. $this->_putEmbeddedFiles();
  10655. $this->_putannotsobjs();
  10656. $this->_putjavascript();
  10657. $this->_putencryption();
  10658. }
  10659. /**
  10660. * Adds some Metadata information (Document Information Dictionary)
  10661. * (see Chapter 14.3.3 Document Information Dictionary of PDF32000_2008.pdf Reference)
  10662. * @return int object id
  10663. * @protected
  10664. */
  10665. protected function _putinfo() {
  10666. $oid = $this->_newobj();
  10667. $out = '<<';
  10668. // store current isunicode value
  10669. $prev_isunicode = $this->isunicode;
  10670. if ($this->docinfounicode) {
  10671. $this->isunicode = true;
  10672. }
  10673. if (!$this->empty_string($this->title)) {
  10674. // The document's title.
  10675. $out .= ' /Title '.$this->_textstring($this->title, $oid);
  10676. }
  10677. if (!$this->empty_string($this->author)) {
  10678. // The name of the person who created the document.
  10679. $out .= ' /Author '.$this->_textstring($this->author, $oid);
  10680. }
  10681. if (!$this->empty_string($this->subject)) {
  10682. // The subject of the document.
  10683. $out .= ' /Subject '.$this->_textstring($this->subject, $oid);
  10684. }
  10685. if (!$this->empty_string($this->keywords)) {
  10686. // Keywords associated with the document.
  10687. $out .= ' /Keywords '.$this->_textstring($this->keywords.' TCPDF', $oid);
  10688. }
  10689. if (!$this->empty_string($this->creator)) {
  10690. // 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.
  10691. $out .= ' /Creator '.$this->_textstring($this->creator, $oid);
  10692. }
  10693. // restore previous isunicode value
  10694. $this->isunicode = $prev_isunicode;
  10695. // default producer
  10696. $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);
  10697. // The date and time the document was created, in human-readable form
  10698. $out .= ' /CreationDate '.$this->_datestring();
  10699. // The date and time the document was most recently modified, in human-readable form
  10700. $out .= ' /ModDate '.$this->_datestring();
  10701. // A name object indicating whether the document has been modified to include trapping information
  10702. $out .= ' /Trapped /False';
  10703. $out .= ' >>';
  10704. $out .= "\n".'endobj';
  10705. $this->_out($out);
  10706. return $oid;
  10707. }
  10708. /**
  10709. * Output Catalog.
  10710. * @return int object id
  10711. * @protected
  10712. */
  10713. protected function _putcatalog() {
  10714. $oid = $this->_newobj();
  10715. $out = '<< /Type /Catalog';
  10716. $out .= ' /Version /'.$this->PDFVersion;
  10717. //$out .= ' /Extensions <<>>';
  10718. $out .= ' /Pages 1 0 R';
  10719. //$out .= ' /PageLabels ' //...;
  10720. $out .= ' /Names <<';
  10721. if ((!empty($this->javascript)) OR (!empty($this->js_objects))) {
  10722. $out .= ' /JavaScript '.($this->n_js).' 0 R';
  10723. }
  10724. $out .= ' >>';
  10725. if (!empty($this->dests)) {
  10726. $out .= ' /Dests '.$this->n_dests.' 0 R';
  10727. }
  10728. $out .= $this->_putviewerpreferences();
  10729. if (isset($this->LayoutMode) AND (!$this->empty_string($this->LayoutMode))) {
  10730. $out .= ' /PageLayout /'.$this->LayoutMode;
  10731. }
  10732. if (isset($this->PageMode) AND (!$this->empty_string($this->PageMode))) {
  10733. $out .= ' /PageMode /'.$this->PageMode;
  10734. }
  10735. if (count($this->outlines) > 0) {
  10736. $out .= ' /Outlines '.$this->OutlineRoot.' 0 R';
  10737. $out .= ' /PageMode /UseOutlines';
  10738. }
  10739. //$out .= ' /Threads []';
  10740. if ($this->ZoomMode == 'fullpage') {
  10741. $out .= ' /OpenAction ['.$this->page_obj_id[1].' 0 R /Fit]';
  10742. } elseif ($this->ZoomMode == 'fullwidth') {
  10743. $out .= ' /OpenAction ['.$this->page_obj_id[1].' 0 R /FitH null]';
  10744. } elseif ($this->ZoomMode == 'real') {
  10745. $out .= ' /OpenAction ['.$this->page_obj_id[1].' 0 R /XYZ null null 1]';
  10746. } elseif (!is_string($this->ZoomMode)) {
  10747. $out .= sprintf(' /OpenAction ['.$this->page_obj_id[1].' 0 R /XYZ null null %.2F]', ($this->ZoomMode / 100));
  10748. }
  10749. //$out .= ' /AA <<>>';
  10750. //$out .= ' /URI <<>>';
  10751. //$out .= ' /Metadata X Y R';
  10752. //$out .= ' /StructTreeRoot <<>>';
  10753. //$out .= ' /MarkInfo <<>>';
  10754. if (isset($this->l['a_meta_language'])) {
  10755. $out .= ' /Lang '.$this->_textstring($this->l['a_meta_language'], $oid);
  10756. }
  10757. //$out .= ' /SpiderInfo <<>>';
  10758. //$out .= ' /OutputIntents []';
  10759. //$out .= ' /PieceInfo <<>>';
  10760. if (!empty($this->pdflayers)) {
  10761. $lyrobjs = '';
  10762. $lyrobjs_print = '';
  10763. $lyrobjs_view = '';
  10764. foreach ($this->pdflayers as $layer) {
  10765. $lyrobjs .= ' '.$layer['objid'].' 0 R';
  10766. if ($layer['print']) {
  10767. $lyrobjs_print .= ' '.$layer['objid'].' 0 R';
  10768. }
  10769. if ($layer['view']) {
  10770. $lyrobjs_view .= ' '.$layer['objid'].' 0 R';
  10771. }
  10772. }
  10773. $out .= ' /OCProperties << /OCGs ['.$lyrobjs.']';
  10774. $out .= ' /D << /ON ['.$lyrobjs_print.']';
  10775. $out .= ' /OFF ['.$lyrobjs_view.']';
  10776. $out .= ' /AS [';
  10777. $out .= '<< /Event /Print /OCGs ['.$lyrobjs.'] /Category [/Print] >> << /Event /View /OCGs ['.$lyrobjs.'] /Category [/View] >>';
  10778. $out .= ' ]';
  10779. $out .= ' >> >>';
  10780. }
  10781. // AcroForm
  10782. if (!empty($this->form_obj_id) OR ($this->sign AND isset($this->signature_data['cert_type']))) {
  10783. $out .= ' /AcroForm <<';
  10784. $objrefs = '';
  10785. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  10786. // set reference for signature object
  10787. $objrefs .= $this->sig_obj_id.' 0 R';
  10788. }
  10789. if (!empty($this->empty_signature_appearance)) {
  10790. foreach ($this->empty_signature_appearance as $esa) {
  10791. // set reference for empty signature objects
  10792. $objrefs .= ' '.$esa['objid'].' 0 R';
  10793. }
  10794. }
  10795. if (!empty($this->form_obj_id)) {
  10796. foreach($this->form_obj_id as $objid) {
  10797. $objrefs .= ' '.$objid.' 0 R';
  10798. }
  10799. }
  10800. $out .= ' /Fields ['.$objrefs.']';
  10801. if (!empty($this->form_obj_id) AND !$this->sign) {
  10802. // It's better to turn off this value and set the appearance stream for each annotation (/AP) to avoid conflicts with signature fields.
  10803. $out .= ' /NeedAppearances true';
  10804. }
  10805. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  10806. if ($this->signature_data['cert_type'] > 0) {
  10807. $out .= ' /SigFlags 3';
  10808. } else {
  10809. $out .= ' /SigFlags 1';
  10810. }
  10811. }
  10812. //$out .= ' /CO ';
  10813. if (isset($this->annotation_fonts) AND !empty($this->annotation_fonts)) {
  10814. $out .= ' /DR <<';
  10815. $out .= ' /Font <<';
  10816. foreach ($this->annotation_fonts as $fontkey => $fontid) {
  10817. $out .= ' /F'.$fontid.' '.$this->font_obj_ids[$fontkey].' 0 R';
  10818. }
  10819. $out .= ' >> >>';
  10820. }
  10821. $font = $this->getFontBuffer('helvetica');
  10822. $out .= ' /DA (/F'.$font['i'].' 0 Tf 0 g)';
  10823. $out .= ' /Q '.(($this->rtl)?'2':'0');
  10824. //$out .= ' /XFA ';
  10825. $out .= ' >>';
  10826. // signatures
  10827. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  10828. if ($this->signature_data['cert_type'] > 0) {
  10829. $out .= ' /Perms << /DocMDP '.($this->sig_obj_id + 1).' 0 R >>';
  10830. } else {
  10831. $out .= ' /Perms << /UR3 '.($this->sig_obj_id + 1).' 0 R >>';
  10832. }
  10833. }
  10834. }
  10835. //$out .= ' /Legal <<>>';
  10836. //$out .= ' /Requirements []';
  10837. //$out .= ' /Collection <<>>';
  10838. //$out .= ' /NeedsRendering true';
  10839. $out .= ' >>';
  10840. $out .= "\n".'endobj';
  10841. $this->_out($out);
  10842. return $oid;
  10843. }
  10844. /**
  10845. * Output viewer preferences.
  10846. * @return string for viewer preferences
  10847. * @author Nicola asuni
  10848. * @since 3.1.000 (2008-06-09)
  10849. * @protected
  10850. */
  10851. protected function _putviewerpreferences() {
  10852. $out = ' /ViewerPreferences <<';
  10853. if ($this->rtl) {
  10854. $out .= ' /Direction /R2L';
  10855. } else {
  10856. $out .= ' /Direction /L2R';
  10857. }
  10858. if (isset($this->viewer_preferences['HideToolbar']) AND ($this->viewer_preferences['HideToolbar'])) {
  10859. $out .= ' /HideToolbar true';
  10860. }
  10861. if (isset($this->viewer_preferences['HideMenubar']) AND ($this->viewer_preferences['HideMenubar'])) {
  10862. $out .= ' /HideMenubar true';
  10863. }
  10864. if (isset($this->viewer_preferences['HideWindowUI']) AND ($this->viewer_preferences['HideWindowUI'])) {
  10865. $out .= ' /HideWindowUI true';
  10866. }
  10867. if (isset($this->viewer_preferences['FitWindow']) AND ($this->viewer_preferences['FitWindow'])) {
  10868. $out .= ' /FitWindow true';
  10869. }
  10870. if (isset($this->viewer_preferences['CenterWindow']) AND ($this->viewer_preferences['CenterWindow'])) {
  10871. $out .= ' /CenterWindow true';
  10872. }
  10873. if (isset($this->viewer_preferences['DisplayDocTitle']) AND ($this->viewer_preferences['DisplayDocTitle'])) {
  10874. $out .= ' /DisplayDocTitle true';
  10875. }
  10876. if (isset($this->viewer_preferences['NonFullScreenPageMode'])) {
  10877. $out .= ' /NonFullScreenPageMode /'.$this->viewer_preferences['NonFullScreenPageMode'];
  10878. }
  10879. if (isset($this->viewer_preferences['ViewArea'])) {
  10880. $out .= ' /ViewArea /'.$this->viewer_preferences['ViewArea'];
  10881. }
  10882. if (isset($this->viewer_preferences['ViewClip'])) {
  10883. $out .= ' /ViewClip /'.$this->viewer_preferences['ViewClip'];
  10884. }
  10885. if (isset($this->viewer_preferences['PrintArea'])) {
  10886. $out .= ' /PrintArea /'.$this->viewer_preferences['PrintArea'];
  10887. }
  10888. if (isset($this->viewer_preferences['PrintClip'])) {
  10889. $out .= ' /PrintClip /'.$this->viewer_preferences['PrintClip'];
  10890. }
  10891. if (isset($this->viewer_preferences['PrintScaling'])) {
  10892. $out .= ' /PrintScaling /'.$this->viewer_preferences['PrintScaling'];
  10893. }
  10894. if (isset($this->viewer_preferences['Duplex']) AND (!$this->empty_string($this->viewer_preferences['Duplex']))) {
  10895. $out .= ' /Duplex /'.$this->viewer_preferences['Duplex'];
  10896. }
  10897. if (isset($this->viewer_preferences['PickTrayByPDFSize'])) {
  10898. if ($this->viewer_preferences['PickTrayByPDFSize']) {
  10899. $out .= ' /PickTrayByPDFSize true';
  10900. } else {
  10901. $out .= ' /PickTrayByPDFSize false';
  10902. }
  10903. }
  10904. if (isset($this->viewer_preferences['PrintPageRange'])) {
  10905. $PrintPageRangeNum = '';
  10906. foreach ($this->viewer_preferences['PrintPageRange'] as $k => $v) {
  10907. $PrintPageRangeNum .= ' '.($v - 1).'';
  10908. }
  10909. $out .= ' /PrintPageRange ['.substr($PrintPageRangeNum,1).']';
  10910. }
  10911. if (isset($this->viewer_preferences['NumCopies'])) {
  10912. $out .= ' /NumCopies '.intval($this->viewer_preferences['NumCopies']);
  10913. }
  10914. $out .= ' >>';
  10915. return $out;
  10916. }
  10917. /**
  10918. * Output PDF header.
  10919. * @protected
  10920. */
  10921. protected function _putheader() {
  10922. $this->_out('%PDF-'.$this->PDFVersion);
  10923. }
  10924. /**
  10925. * Output end of document (EOF).
  10926. * @protected
  10927. */
  10928. protected function _enddoc() {
  10929. $this->state = 1;
  10930. $this->_putheader();
  10931. $this->_putpages();
  10932. $this->_putresources();
  10933. // empty signature fields
  10934. if (!empty($this->empty_signature_appearance)) {
  10935. foreach ($this->empty_signature_appearance as $key => $esa) {
  10936. // widget annotation for empty signature
  10937. $out = $this->_getobj($esa['objid'])."\n";
  10938. $out .= '<< /Type /Annot';
  10939. $out .= ' /Subtype /Widget';
  10940. $out .= ' /Rect ['.$esa['rect'].']';
  10941. $out .= ' /P '.$this->page_obj_id[($esa['page'])].' 0 R'; // link to signature appearance page
  10942. $out .= ' /F 4';
  10943. $out .= ' /FT /Sig';
  10944. $signame = sprintf('Signature_%03d', ($key + 1));
  10945. $out .= ' /T '.$this->_textstring($signame, $esa['objid']);
  10946. $out .= ' /Ff 0';
  10947. $out .= ' >>';
  10948. $out .= "\n".'endobj';
  10949. $this->_out($out);
  10950. }
  10951. }
  10952. // Signature
  10953. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  10954. // widget annotation for signature
  10955. $out = $this->_getobj($this->sig_obj_id)."\n";
  10956. $out .= '<< /Type /Annot';
  10957. $out .= ' /Subtype /Widget';
  10958. $out .= ' /Rect ['.$this->signature_appearance['rect'].']';
  10959. $out .= ' /P '.$this->page_obj_id[($this->signature_appearance['page'])].' 0 R'; // link to signature appearance page
  10960. $out .= ' /F 4';
  10961. $out .= ' /FT /Sig';
  10962. $out .= ' /T '.$this->_textstring('Signature_000', $this->sig_obj_id);
  10963. $out .= ' /Ff 0';
  10964. $out .= ' /V '.($this->sig_obj_id + 1).' 0 R';
  10965. $out .= ' >>';
  10966. $out .= "\n".'endobj';
  10967. $this->_out($out);
  10968. // signature
  10969. $this->_putsignature();
  10970. }
  10971. // Info
  10972. $objid_info = $this->_putinfo();
  10973. // Catalog
  10974. $objid_catalog = $this->_putcatalog();
  10975. // Cross-ref
  10976. $o = $this->bufferlen;
  10977. // XREF section
  10978. $this->_out('xref');
  10979. $this->_out('0 '.($this->n + 1));
  10980. $this->_out('0000000000 65535 f ');
  10981. for ($i=1; $i <= $this->n; ++$i) {
  10982. if (!isset($this->offsets[$i]) AND ($i > 1)) {
  10983. $this->offsets[$i] = $this->offsets[($i - 1)];
  10984. }
  10985. $this->_out(sprintf('%010d 00000 n ', $this->offsets[$i]));
  10986. }
  10987. // TRAILER
  10988. $out = 'trailer <<';
  10989. $out .= ' /Size '.($this->n + 1);
  10990. $out .= ' /Root '.$objid_catalog.' 0 R';
  10991. $out .= ' /Info '.$objid_info.' 0 R';
  10992. if ($this->encrypted) {
  10993. $out .= ' /Encrypt '.$this->encryptdata['objid'].' 0 R';
  10994. }
  10995. $out .= ' /ID [ <'.$this->file_id.'> <'.$this->file_id.'> ]';
  10996. $out .= ' >>';
  10997. $this->_out($out);
  10998. $this->_out('startxref');
  10999. $this->_out($o);
  11000. $this->_out('%%EOF');
  11001. $this->state = 3; // end-of-doc
  11002. if ($this->diskcache) {
  11003. // remove temporary files used for images
  11004. foreach ($this->imagekeys as $key) {
  11005. // remove temporary files
  11006. unlink($this->images[$key]);
  11007. }
  11008. foreach ($this->fontkeys as $key) {
  11009. // remove temporary files
  11010. unlink($this->fonts[$key]);
  11011. }
  11012. }
  11013. }
  11014. /**
  11015. * Initialize a new page.
  11016. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  11017. * @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().
  11018. * @protected
  11019. * @see getPageSizeFromFormat(), setPageFormat()
  11020. */
  11021. protected function _beginpage($orientation='', $format='') {
  11022. ++$this->page;
  11023. $this->setPageBuffer($this->page, '');
  11024. // initialize array for graphics tranformation positions inside a page buffer
  11025. $this->transfmrk[$this->page] = array();
  11026. $this->state = 2;
  11027. if ($this->empty_string($orientation)) {
  11028. if (isset($this->CurOrientation)) {
  11029. $orientation = $this->CurOrientation;
  11030. } elseif ($this->fwPt > $this->fhPt) {
  11031. // landscape
  11032. $orientation = 'L';
  11033. } else {
  11034. // portrait
  11035. $orientation = 'P';
  11036. }
  11037. }
  11038. if ($this->empty_string($format)) {
  11039. $this->pagedim[$this->page] = $this->pagedim[($this->page - 1)];
  11040. $this->setPageOrientation($orientation);
  11041. } else {
  11042. $this->setPageFormat($format, $orientation);
  11043. }
  11044. if ($this->rtl) {
  11045. $this->x = $this->w - $this->rMargin;
  11046. } else {
  11047. $this->x = $this->lMargin;
  11048. }
  11049. $this->y = $this->tMargin;
  11050. if (isset($this->newpagegroup[$this->page])) {
  11051. // start a new group
  11052. $this->currpagegroup = $this->newpagegroup[$this->page];
  11053. $this->pagegroups[$this->currpagegroup] = 1;
  11054. } elseif (isset($this->currpagegroup) AND ($this->currpagegroup > 0)) {
  11055. ++$this->pagegroups[$this->currpagegroup];
  11056. }
  11057. }
  11058. /**
  11059. * Mark end of page.
  11060. * @protected
  11061. */
  11062. protected function _endpage() {
  11063. $this->setVisibility('all');
  11064. $this->state = 1;
  11065. }
  11066. /**
  11067. * Begin a new object and return the object number.
  11068. * @return int object number
  11069. * @protected
  11070. */
  11071. protected function _newobj() {
  11072. $this->_out($this->_getobj());
  11073. return $this->n;
  11074. }
  11075. /**
  11076. * Return the starting object string for the selected object ID.
  11077. * @param $objid (int) Object ID (leave empty to get a new ID).
  11078. * @return string the starting object string
  11079. * @protected
  11080. * @since 5.8.009 (2010-08-20)
  11081. */
  11082. protected function _getobj($objid='') {
  11083. if ($objid === '') {
  11084. ++$this->n;
  11085. $objid = $this->n;
  11086. }
  11087. $this->offsets[$objid] = $this->bufferlen;
  11088. return $objid.' 0 obj';
  11089. }
  11090. /**
  11091. * Underline text.
  11092. * @param $x (int) X coordinate
  11093. * @param $y (int) Y coordinate
  11094. * @param $txt (string) text to underline
  11095. * @protected
  11096. */
  11097. protected function _dounderline($x, $y, $txt) {
  11098. $w = $this->GetStringWidth($txt);
  11099. return $this->_dounderlinew($x, $y, $w);
  11100. }
  11101. /**
  11102. * Underline for rectangular text area.
  11103. * @param $x (int) X coordinate
  11104. * @param $y (int) Y coordinate
  11105. * @param $w (int) width to underline
  11106. * @protected
  11107. * @since 4.8.008 (2009-09-29)
  11108. */
  11109. protected function _dounderlinew($x, $y, $w) {
  11110. $linew = - $this->CurrentFont['ut'] / 1000 * $this->FontSizePt;
  11111. return sprintf('%.2F %.2F %.2F %.2F re f', $x * $this->k, ((($this->h - $y) * $this->k) + $linew), $w * $this->k, $linew);
  11112. }
  11113. /**
  11114. * Line through text.
  11115. * @param $x (int) X coordinate
  11116. * @param $y (int) Y coordinate
  11117. * @param $txt (string) text to linethrough
  11118. * @protected
  11119. */
  11120. protected function _dolinethrough($x, $y, $txt) {
  11121. $w = $this->GetStringWidth($txt);
  11122. return $this->_dolinethroughw($x, $y, $w);
  11123. }
  11124. /**
  11125. * Line through for rectangular text area.
  11126. * @param $x (int) X coordinate
  11127. * @param $y (int) Y coordinate
  11128. * @param $w (int) line length (width)
  11129. * @protected
  11130. * @since 4.9.008 (2009-09-29)
  11131. */
  11132. protected function _dolinethroughw($x, $y, $w) {
  11133. $linew = - $this->CurrentFont['ut'] / 1000 * $this->FontSizePt;
  11134. return sprintf('%.2F %.2F %.2F %.2F re f', $x * $this->k, ((($this->h - $y) * $this->k) + $linew + ($this->FontSizePt / 3)), $w * $this->k, $linew);
  11135. }
  11136. /**
  11137. * Overline text.
  11138. * @param $x (int) X coordinate
  11139. * @param $y (int) Y coordinate
  11140. * @param $txt (string) text to overline
  11141. * @protected
  11142. * @since 4.9.015 (2010-04-19)
  11143. */
  11144. protected function _dooverline($x, $y, $txt) {
  11145. $w = $this->GetStringWidth($txt);
  11146. return $this->_dooverlinew($x, $y, $w);
  11147. }
  11148. /**
  11149. * Overline for rectangular text area.
  11150. * @param $x (int) X coordinate
  11151. * @param $y (int) Y coordinate
  11152. * @param $w (int) width to overline
  11153. * @protected
  11154. * @since 4.9.015 (2010-04-19)
  11155. */
  11156. protected function _dooverlinew($x, $y, $w) {
  11157. $linew = - $this->CurrentFont['ut'] / 1000 * $this->FontSizePt;
  11158. return sprintf('%.2F %.2F %.2F %.2F re f', $x * $this->k, (($this->h - $y + $this->FontAscent) * $this->k) - $linew, $w * $this->k, $linew);
  11159. }
  11160. /**
  11161. * Read a 4-byte (32 bit) integer from file.
  11162. * @param $f (string) file name.
  11163. * @return 4-byte integer
  11164. * @protected
  11165. */
  11166. protected function _freadint($f) {
  11167. $a = unpack('Ni', fread($f, 4));
  11168. return $a['i'];
  11169. }
  11170. /**
  11171. * Add "\" before "\", "(" and ")"
  11172. * @param $s (string) string to escape.
  11173. * @return string escaped string.
  11174. * @protected
  11175. */
  11176. protected function _escape($s) {
  11177. // the chr(13) substitution fixes the Bugs item #1421290.
  11178. return strtr($s, array(')' => '\\)', '(' => '\\(', '\\' => '\\\\', chr(13) => '\r'));
  11179. }
  11180. /**
  11181. * Format a data string for meta information
  11182. * @param $s (string) data string to escape.
  11183. * @param $n (int) object ID
  11184. * @return string escaped string.
  11185. * @protected
  11186. */
  11187. protected function _datastring($s, $n=0) {
  11188. if ($n == 0) {
  11189. $n = $this->n;
  11190. }
  11191. $s = $this->_encrypt_data($n, $s);
  11192. return '('. $this->_escape($s).')';
  11193. }
  11194. /**
  11195. * Returns a formatted date for meta information
  11196. * @param $n (int) object ID
  11197. * @return string escaped date string.
  11198. * @protected
  11199. * @since 4.6.028 (2009-08-25)
  11200. */
  11201. protected function _datestring($n=0) {
  11202. $current_time = substr_replace(date('YmdHisO'), '\'', (0 - 2), 0).'\'';
  11203. return $this->_datastring('D:'.$current_time, $n);
  11204. }
  11205. /**
  11206. * Format a text string for meta information
  11207. * @param $s (string) string to escape.
  11208. * @param $n (int) object ID
  11209. * @return string escaped string.
  11210. * @protected
  11211. */
  11212. protected function _textstring($s, $n=0) {
  11213. if ($this->isunicode) {
  11214. //Convert string to UTF-16BE
  11215. $s = $this->UTF8ToUTF16BE($s, true);
  11216. }
  11217. return $this->_datastring($s, $n);
  11218. }
  11219. /**
  11220. * THIS METHOD IS DEPRECATED
  11221. * Format a text string
  11222. * @param $s (string) string to escape.
  11223. * @return string escaped string.
  11224. * @protected
  11225. * @deprecated
  11226. */
  11227. protected function _escapetext($s) {
  11228. if ($this->isunicode) {
  11229. if (($this->CurrentFont['type'] == 'core') OR ($this->CurrentFont['type'] == 'TrueType') OR ($this->CurrentFont['type'] == 'Type1')) {
  11230. $s = $this->UTF8ToLatin1($s);
  11231. } else {
  11232. //Convert string to UTF-16BE and reverse RTL language
  11233. $s = $this->utf8StrRev($s, false, $this->tmprtl);
  11234. }
  11235. }
  11236. return $this->_escape($s);
  11237. }
  11238. /**
  11239. * get raw output stream.
  11240. * @param $s (string) string to output.
  11241. * @param $n (int) object reference for encryption mode
  11242. * @protected
  11243. * @author Nicola Asuni
  11244. * @since 5.5.000 (2010-06-22)
  11245. */
  11246. protected function _getrawstream($s, $n=0) {
  11247. if ($n <= 0) {
  11248. // default to current object
  11249. $n = $this->n;
  11250. }
  11251. return $this->_encrypt_data($n, $s);
  11252. }
  11253. /**
  11254. * Format output stream (DEPRECATED).
  11255. * @param $s (string) string to output.
  11256. * @param $n (int) object reference for encryption mode
  11257. * @protected
  11258. * @deprecated
  11259. */
  11260. protected function _getstream($s, $n=0) {
  11261. return 'stream'."\n".$this->_getrawstream($s, $n)."\n".'endstream';
  11262. }
  11263. /**
  11264. * Output a stream (DEPRECATED).
  11265. * @param $s (string) string to output.
  11266. * @param $n (int) object reference for encryption mode
  11267. * @protected
  11268. * @deprecated
  11269. */
  11270. protected function _putstream($s, $n=0) {
  11271. $this->_out($this->_getstream($s, $n));
  11272. }
  11273. /**
  11274. * Output a string to the document.
  11275. * @param $s (string) string to output.
  11276. * @protected
  11277. */
  11278. protected function _out($s) {
  11279. if ($this->state == 2) {
  11280. if ($this->inxobj) {
  11281. // we are inside an XObject template
  11282. $this->xobjects[$this->xobjid]['outdata'] .= $s."\n";
  11283. } elseif ((!$this->InFooter) AND isset($this->footerlen[$this->page]) AND ($this->footerlen[$this->page] > 0)) {
  11284. // puts data before page footer
  11285. $pagebuff = $this->getPageBuffer($this->page);
  11286. $page = substr($pagebuff, 0, -$this->footerlen[$this->page]);
  11287. $footer = substr($pagebuff, -$this->footerlen[$this->page]);
  11288. $this->setPageBuffer($this->page, $page.$s."\n".$footer);
  11289. // update footer position
  11290. $this->footerpos[$this->page] += strlen($s."\n");
  11291. } else {
  11292. $this->setPageBuffer($this->page, $s."\n", true);
  11293. }
  11294. } else {
  11295. $this->setBuffer($s."\n");
  11296. }
  11297. }
  11298. /**
  11299. * Converts UTF-8 strings to codepoints array.<br>
  11300. * Invalid byte sequences will be replaced with 0xFFFD (replacement character)<br>
  11301. * Based on: http://www.faqs.org/rfcs/rfc3629.html
  11302. * <pre>
  11303. * Char. number range | UTF-8 octet sequence
  11304. * (hexadecimal) | (binary)
  11305. * --------------------+-----------------------------------------------
  11306. * 0000 0000-0000 007F | 0xxxxxxx
  11307. * 0000 0080-0000 07FF | 110xxxxx 10xxxxxx
  11308. * 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
  11309. * 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  11310. * ---------------------------------------------------------------------
  11311. *
  11312. * ABFN notation:
  11313. * ---------------------------------------------------------------------
  11314. * UTF8-octets = *( UTF8-char )
  11315. * UTF8-char = UTF8-1 / UTF8-2 / UTF8-3 / UTF8-4
  11316. * UTF8-1 = %x00-7F
  11317. * UTF8-2 = %xC2-DF UTF8-tail
  11318. *
  11319. * UTF8-3 = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /
  11320. * %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )
  11321. * UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /
  11322. * %xF4 %x80-8F 2( UTF8-tail )
  11323. * UTF8-tail = %x80-BF
  11324. * ---------------------------------------------------------------------
  11325. * </pre>
  11326. * @param $str (string) string to process.
  11327. * @return array containing codepoints (UTF-8 characters values)
  11328. * @protected
  11329. * @author Nicola Asuni
  11330. * @since 1.53.0.TC005 (2005-01-05)
  11331. */
  11332. protected function UTF8StringToArray($str) {
  11333. // build a unique string key
  11334. $strkey = md5($str);
  11335. if (isset($this->cache_UTF8StringToArray[$strkey])) {
  11336. // return cached value
  11337. $chrarray = $this->cache_UTF8StringToArray[$strkey]['s'];
  11338. if (!isset($this->cache_UTF8StringToArray[$strkey]['f'][$this->CurrentFont['fontkey']])) {
  11339. if ($this->isunicode) {
  11340. foreach ($chrarray as $chr) {
  11341. // store this char for font subsetting
  11342. $this->CurrentFont['subsetchars'][$chr] = true;
  11343. }
  11344. // update font subsetchars
  11345. $this->setFontSubBuffer($this->CurrentFont['fontkey'], 'subsetchars', $this->CurrentFont['subsetchars']);
  11346. }
  11347. $this->cache_UTF8StringToArray[$strkey]['f'][$this->CurrentFont['fontkey']] = true;
  11348. }
  11349. return $chrarray;
  11350. }
  11351. // check cache size
  11352. if ($this->cache_size_UTF8StringToArray >= $this->cache_maxsize_UTF8StringToArray) {
  11353. // remove first element
  11354. array_shift($this->cache_UTF8StringToArray);
  11355. }
  11356. // new cache array for selected string
  11357. $this->cache_UTF8StringToArray[$strkey] = array('s' => array(), 'f' => array());
  11358. ++$this->cache_size_UTF8StringToArray;
  11359. if (!$this->isunicode) {
  11360. // split string into array of equivalent codes
  11361. $strarr = array();
  11362. $strlen = strlen($str);
  11363. for ($i=0; $i < $strlen; ++$i) {
  11364. $strarr[] = ord($str{$i});
  11365. }
  11366. // insert new value on cache
  11367. $this->cache_UTF8StringToArray[$strkey]['s'] = $strarr;
  11368. $this->cache_UTF8StringToArray[$strkey]['f'][$this->CurrentFont['fontkey']] = true;
  11369. return $strarr;
  11370. }
  11371. $unichar = -1; // last unicode char
  11372. $unicode = array(); // array containing unicode values
  11373. $bytes = array(); // array containing single character byte sequences
  11374. $numbytes = 1; // number of octetc needed to represent the UTF-8 character
  11375. $str .= ''; // force $str to be a string
  11376. $length = strlen($str);
  11377. for ($i = 0; $i < $length; ++$i) {
  11378. $char = ord($str{$i}); // get one string character at time
  11379. if (count($bytes) == 0) { // get starting octect
  11380. if ($char <= 0x7F) {
  11381. $unichar = $char; // use the character "as is" because is ASCII
  11382. $numbytes = 1;
  11383. } elseif (($char >> 0x05) == 0x06) { // 2 bytes character (0x06 = 110 BIN)
  11384. $bytes[] = ($char - 0xC0) << 0x06;
  11385. $numbytes = 2;
  11386. } elseif (($char >> 0x04) == 0x0E) { // 3 bytes character (0x0E = 1110 BIN)
  11387. $bytes[] = ($char - 0xE0) << 0x0C;
  11388. $numbytes = 3;
  11389. } elseif (($char >> 0x03) == 0x1E) { // 4 bytes character (0x1E = 11110 BIN)
  11390. $bytes[] = ($char - 0xF0) << 0x12;
  11391. $numbytes = 4;
  11392. } else {
  11393. // use replacement character for other invalid sequences
  11394. $unichar = 0xFFFD;
  11395. $bytes = array();
  11396. $numbytes = 1;
  11397. }
  11398. } elseif (($char >> 0x06) == 0x02) { // bytes 2, 3 and 4 must start with 0x02 = 10 BIN
  11399. $bytes[] = $char - 0x80;
  11400. if (count($bytes) == $numbytes) {
  11401. // compose UTF-8 bytes to a single unicode value
  11402. $char = $bytes[0];
  11403. for ($j = 1; $j < $numbytes; ++$j) {
  11404. $char += ($bytes[$j] << (($numbytes - $j - 1) * 0x06));
  11405. }
  11406. if ((($char >= 0xD800) AND ($char <= 0xDFFF)) OR ($char >= 0x10FFFF)) {
  11407. /* The definition of UTF-8 prohibits encoding character numbers between
  11408. U+D800 and U+DFFF, which are reserved for use with the UTF-16
  11409. encoding form (as surrogate pairs) and do not directly represent
  11410. characters. */
  11411. $unichar = 0xFFFD; // use replacement character
  11412. } else {
  11413. $unichar = $char; // add char to array
  11414. }
  11415. // reset data for next char
  11416. $bytes = array();
  11417. $numbytes = 1;
  11418. }
  11419. } else {
  11420. // use replacement character for other invalid sequences
  11421. $unichar = 0xFFFD;
  11422. $bytes = array();
  11423. $numbytes = 1;
  11424. }
  11425. if ($unichar >= 0) {
  11426. // insert unicode value into array
  11427. $unicode[] = $unichar;
  11428. // store this char for font subsetting
  11429. $this->CurrentFont['subsetchars'][$unichar] = true;
  11430. $unichar = -1;
  11431. }
  11432. }
  11433. // update font subsetchars
  11434. $this->setFontSubBuffer($this->CurrentFont['fontkey'], 'subsetchars', $this->CurrentFont['subsetchars']);
  11435. // insert new value on cache
  11436. $this->cache_UTF8StringToArray[$strkey]['s'] = $unicode;
  11437. $this->cache_UTF8StringToArray[$strkey]['f'][$this->CurrentFont['fontkey']] = true;
  11438. return $unicode;
  11439. }
  11440. /**
  11441. * Converts UTF-8 strings to UTF16-BE.<br>
  11442. * @param $str (string) string to process.
  11443. * @param $setbom (boolean) if true set the Byte Order Mark (BOM = 0xFEFF)
  11444. * @return string
  11445. * @author Nicola Asuni
  11446. * @since 1.53.0.TC005 (2005-01-05)
  11447. * @see UTF8StringToArray(), arrUTF8ToUTF16BE()
  11448. * @protected
  11449. */
  11450. protected function UTF8ToUTF16BE($str, $setbom=false) {
  11451. if (!$this->isunicode) {
  11452. return $str; // string is not in unicode
  11453. }
  11454. $unicode = $this->UTF8StringToArray($str); // array containing UTF-8 unicode values
  11455. return $this->arrUTF8ToUTF16BE($unicode, $setbom);
  11456. }
  11457. /**
  11458. * Converts UTF-8 strings to Latin1 when using the standard 14 core fonts.<br>
  11459. * @param $str (string) string to process.
  11460. * @return string
  11461. * @author Andrew Whitehead, Nicola Asuni
  11462. * @protected
  11463. * @since 3.2.000 (2008-06-23)
  11464. */
  11465. protected function UTF8ToLatin1($str) {
  11466. if (!$this->isunicode) {
  11467. return $str; // string is not in unicode
  11468. }
  11469. $outstr = ''; // string to be returned
  11470. $unicode = $this->UTF8StringToArray($str); // array containing UTF-8 unicode values
  11471. foreach ($unicode as $char) {
  11472. if ($char < 256) {
  11473. $outstr .= chr($char);
  11474. } elseif (array_key_exists($char, $this->unicode->uni_utf8tolatin)) {
  11475. // map from UTF-8
  11476. $outstr .= chr($this->unicode->uni_utf8tolatin[$char]);
  11477. } elseif ($char == 0xFFFD) {
  11478. // skip
  11479. } else {
  11480. $outstr .= '?';
  11481. }
  11482. }
  11483. return $outstr;
  11484. }
  11485. /**
  11486. * Converts UTF-8 characters array to array of Latin1 characters<br>
  11487. * @param $unicode (array) array containing UTF-8 unicode values
  11488. * @return array
  11489. * @author Nicola Asuni
  11490. * @protected
  11491. * @since 4.8.023 (2010-01-15)
  11492. */
  11493. protected function UTF8ArrToLatin1($unicode) {
  11494. if ((!$this->isunicode) OR $this->isUnicodeFont()) {
  11495. return $unicode;
  11496. }
  11497. $outarr = array(); // array to be returned
  11498. foreach ($unicode as $char) {
  11499. if ($char < 256) {
  11500. $outarr[] = $char;
  11501. } elseif (array_key_exists($char, $this->unicode->uni_utf8tolatin)) {
  11502. // map from UTF-8
  11503. $outarr[] = $this->unicode->uni_utf8tolatin[$char];
  11504. } elseif ($char == 0xFFFD) {
  11505. // skip
  11506. } else {
  11507. $outarr[] = 63; // '?' character
  11508. }
  11509. }
  11510. return $outarr;
  11511. }
  11512. /**
  11513. * Converts array of UTF-8 characters to UTF16-BE string.<br>
  11514. * Based on: http://www.faqs.org/rfcs/rfc2781.html
  11515. * <pre>
  11516. * Encoding UTF-16:
  11517. *
  11518. * Encoding of a single character from an ISO 10646 character value to
  11519. * UTF-16 proceeds as follows. Let U be the character number, no greater
  11520. * than 0x10FFFF.
  11521. *
  11522. * 1) If U < 0x10000, encode U as a 16-bit unsigned integer and
  11523. * terminate.
  11524. *
  11525. * 2) Let U' = U - 0x10000. Because U is less than or equal to 0x10FFFF,
  11526. * U' must be less than or equal to 0xFFFFF. That is, U' can be
  11527. * represented in 20 bits.
  11528. *
  11529. * 3) Initialize two 16-bit unsigned integers, W1 and W2, to 0xD800 and
  11530. * 0xDC00, respectively. These integers each have 10 bits free to
  11531. * encode the character value, for a total of 20 bits.
  11532. *
  11533. * 4) Assign the 10 high-order bits of the 20-bit U' to the 10 low-order
  11534. * bits of W1 and the 10 low-order bits of U' to the 10 low-order
  11535. * bits of W2. Terminate.
  11536. *
  11537. * Graphically, steps 2 through 4 look like:
  11538. * U' = yyyyyyyyyyxxxxxxxxxx
  11539. * W1 = 110110yyyyyyyyyy
  11540. * W2 = 110111xxxxxxxxxx
  11541. * </pre>
  11542. * @param $unicode (array) array containing UTF-8 unicode values
  11543. * @param $setbom (boolean) if true set the Byte Order Mark (BOM = 0xFEFF)
  11544. * @return string
  11545. * @protected
  11546. * @author Nicola Asuni
  11547. * @since 2.1.000 (2008-01-08)
  11548. * @see UTF8ToUTF16BE()
  11549. */
  11550. protected function arrUTF8ToUTF16BE($unicode, $setbom=false) {
  11551. $outstr = ''; // string to be returned
  11552. if ($setbom) {
  11553. $outstr .= "\xFE\xFF"; // Byte Order Mark (BOM)
  11554. }
  11555. foreach ($unicode as $char) {
  11556. if ($char == 0x200b) {
  11557. // skip Unicode Character 'ZERO WIDTH SPACE' (DEC:8203, U+200B)
  11558. } elseif ($char == 0xFFFD) {
  11559. $outstr .= "\xFF\xFD"; // replacement character
  11560. } elseif ($char < 0x10000) {
  11561. $outstr .= chr($char >> 0x08);
  11562. $outstr .= chr($char & 0xFF);
  11563. } else {
  11564. $char -= 0x10000;
  11565. $w1 = 0xD800 | ($char >> 0x0a);
  11566. $w2 = 0xDC00 | ($char & 0x3FF);
  11567. $outstr .= chr($w1 >> 0x08);
  11568. $outstr .= chr($w1 & 0xFF);
  11569. $outstr .= chr($w2 >> 0x08);
  11570. $outstr .= chr($w2 & 0xFF);
  11571. }
  11572. }
  11573. return $outstr;
  11574. }
  11575. // ====================================================
  11576. /**
  11577. * Set header font.
  11578. * @param $font (array) font
  11579. * @public
  11580. * @since 1.1
  11581. */
  11582. public function setHeaderFont($font) {
  11583. $this->header_font = $font;
  11584. }
  11585. /**
  11586. * Get header font.
  11587. * @return array()
  11588. * @public
  11589. * @since 4.0.012 (2008-07-24)
  11590. */
  11591. public function getHeaderFont() {
  11592. return $this->header_font;
  11593. }
  11594. /**
  11595. * Set footer font.
  11596. * @param $font (array) font
  11597. * @public
  11598. * @since 1.1
  11599. */
  11600. public function setFooterFont($font) {
  11601. $this->footer_font = $font;
  11602. }
  11603. /**
  11604. * Get Footer font.
  11605. * @return array()
  11606. * @public
  11607. * @since 4.0.012 (2008-07-24)
  11608. */
  11609. public function getFooterFont() {
  11610. return $this->footer_font;
  11611. }
  11612. /**
  11613. * Set language array.
  11614. * @param $language (array)
  11615. * @public
  11616. * @since 1.1
  11617. */
  11618. public function setLanguageArray($language) {
  11619. $this->l = $language;
  11620. if (isset($this->l['a_meta_dir'])) {
  11621. $this->rtl = $this->l['a_meta_dir']=='rtl' ? true : false;
  11622. } else {
  11623. $this->rtl = false;
  11624. }
  11625. }
  11626. /**
  11627. * Returns the PDF data.
  11628. * @public
  11629. */
  11630. public function getPDFData() {
  11631. if ($this->state < 3) {
  11632. $this->Close();
  11633. }
  11634. return $this->buffer;
  11635. }
  11636. /**
  11637. * Output anchor link.
  11638. * @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;)
  11639. * @param $name (string) link name
  11640. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  11641. * @param $firstline (boolean) if true prints only the first line and return the remaining string.
  11642. * @param $color (array) array of RGB text color
  11643. * @param $style (string) font style (U, D, B, I)
  11644. * @param $firstblock (boolean) if true the string is the starting of a line.
  11645. * @return the number of cells used or the remaining text if $firstline = true;
  11646. * @public
  11647. */
  11648. public function addHtmlLink($url, $name, $fill=false, $firstline=false, $color='', $style=-1, $firstblock=false) {
  11649. if (!$this->empty_string($url) AND ($url{0} == '#')) {
  11650. // convert url to internal link
  11651. $lnkdata = explode(',', $url);
  11652. if (isset($lnkdata[0])) {
  11653. $page = intval(substr($lnkdata[0], 1));
  11654. if (empty($page) OR ($page <= 0)) {
  11655. $page = $this->page;
  11656. }
  11657. if (isset($lnkdata[1]) AND (strlen($lnkdata[1]) > 0)) {
  11658. $lnky = floatval($lnkdata[1]);
  11659. } else {
  11660. $lnky = 0;
  11661. }
  11662. $url = $this->AddLink();
  11663. $this->SetLink($url, $lnky, $page);
  11664. }
  11665. }
  11666. // store current settings
  11667. $prevcolor = $this->fgcolor;
  11668. $prevstyle = $this->FontStyle;
  11669. if (empty($color)) {
  11670. $this->SetTextColorArray($this->htmlLinkColorArray);
  11671. } else {
  11672. $this->SetTextColorArray($color);
  11673. }
  11674. if ($style == -1) {
  11675. $this->SetFont('', $this->FontStyle.$this->htmlLinkFontStyle);
  11676. } else {
  11677. $this->SetFont('', $this->FontStyle.$style);
  11678. }
  11679. $ret = $this->Write($this->lasth, $name, $url, $fill, '', false, 0, $firstline, $firstblock, 0);
  11680. // restore settings
  11681. $this->SetFont('', $prevstyle);
  11682. $this->SetTextColorArray($prevcolor);
  11683. return $ret;
  11684. }
  11685. /**
  11686. * 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 representation.
  11687. * @param $hcolor (string) html color
  11688. * @return array RGB or CMYK color, or false in case of error.
  11689. * @public
  11690. */
  11691. public function convertHTMLColorToDec($hcolor='#FFFFFF') {
  11692. $returncolor = false;
  11693. $color = preg_replace('/[\s]*/', '', $hcolor); // remove extra spaces
  11694. $color = strtolower($color);
  11695. if (($dotpos = strpos($color, '.')) !== false) {
  11696. // remove class parent (i.e.: color.red)
  11697. $color = substr($color, ($dotpos + 1));
  11698. }
  11699. if (strlen($color) == 0) {
  11700. return false;
  11701. }
  11702. // RGB ARRAY
  11703. if (substr($color, 0, 3) == 'rgb') {
  11704. $codes = substr($color, 4);
  11705. $codes = str_replace(')', '', $codes);
  11706. $returncolor = explode(',', $codes);
  11707. foreach ($returncolor as $key => $val) {
  11708. if (strpos($val, '%') > 0) {
  11709. // percentage
  11710. $returncolor[$key] = (255 * intval($val) / 100);
  11711. } else {
  11712. $returncolor[$key] = intval($val);
  11713. }
  11714. // normalize value
  11715. $returncolor[$key] = max(0, min(255, $returncolor[$key]));
  11716. }
  11717. return $returncolor;
  11718. }
  11719. // CMYK ARRAY
  11720. if (substr($color, 0, 4) == 'cmyk') {
  11721. $codes = substr($color, 5);
  11722. $codes = str_replace(')', '', $codes);
  11723. $returncolor = explode(',', $codes);
  11724. foreach ($returncolor as $key => $val) {
  11725. if (strpos($val, '%') !== false) {
  11726. // percentage
  11727. $returncolor[$key] = (100 * intval($val) / 100);
  11728. } else {
  11729. $returncolor[$key] = intval($val);
  11730. }
  11731. // normalize value
  11732. $returncolor[$key] = max(0, min(100, $returncolor[$key]));
  11733. }
  11734. return $returncolor;
  11735. }
  11736. // COLOR NAME
  11737. if (substr($color, 0, 1) != '#') {
  11738. // decode color name
  11739. if (isset($this->webcolor[$color])) {
  11740. // web color
  11741. $color_code = $this->webcolor[$color];
  11742. } elseif (isset($this->spot_colors[$hcolor])) {
  11743. // custom defined spot color
  11744. return array($this->spot_colors[$hcolor]['c'], $this->spot_colors[$hcolor]['m'], $this->spot_colors[$hcolor]['y'], $this->spot_colors[$hcolor]['k'], $hcolor);
  11745. } elseif (isset($this->spotcolor[$color])) {
  11746. // spot color from configuration file
  11747. return $this->spotcolor[$color];
  11748. } else {
  11749. return false;
  11750. }
  11751. } else {
  11752. $color_code = substr($color, 1);
  11753. }
  11754. // RGB VALUE
  11755. switch (strlen($color_code)) {
  11756. case 3: {
  11757. // three-digit hexadecimal representation
  11758. $r = substr($color_code, 0, 1);
  11759. $g = substr($color_code, 1, 1);
  11760. $b = substr($color_code, 2, 1);
  11761. $returncolor = array();
  11762. $returncolor['R'] = max(0, min(255, hexdec($r.$r)));
  11763. $returncolor['G'] = max(0, min(255, hexdec($g.$g)));
  11764. $returncolor['B'] = max(0, min(255, hexdec($b.$b)));
  11765. break;
  11766. }
  11767. case 6: {
  11768. // six-digit hexadecimal representation
  11769. $returncolor = array();
  11770. $returncolor['R'] = max(0, min(255, hexdec(substr($color_code, 0, 2))));
  11771. $returncolor['G'] = max(0, min(255, hexdec(substr($color_code, 2, 2))));
  11772. $returncolor['B'] = max(0, min(255, hexdec(substr($color_code, 4, 2))));
  11773. break;
  11774. }
  11775. }
  11776. return $returncolor;
  11777. }
  11778. /**
  11779. * Converts pixels to User's Units.
  11780. * @param $px (int) pixels
  11781. * @return float value in user's unit
  11782. * @public
  11783. * @see setImageScale(), getImageScale()
  11784. */
  11785. public function pixelsToUnits($px) {
  11786. return ($px / ($this->imgscale * $this->k));
  11787. }
  11788. /**
  11789. * Reverse function for htmlentities.
  11790. * Convert entities in UTF-8.
  11791. * @param $text_to_convert (string) Text to convert.
  11792. * @return string converted text string
  11793. * @public
  11794. */
  11795. public function unhtmlentities($text_to_convert) {
  11796. return @html_entity_decode($text_to_convert, ENT_QUOTES, $this->encoding);
  11797. }
  11798. // ENCRYPTION METHODS ----------------------------------
  11799. /**
  11800. * Returns a string containing random data to be used as a seed for encryption methods.
  11801. * @param $seed (string) starting seed value
  11802. * @return string containing random data
  11803. * @author Nicola Asuni
  11804. * @since 5.9.006 (2010-10-19)
  11805. * @protected
  11806. */
  11807. protected function getRandomSeed($seed='') {
  11808. $seed .= microtime();
  11809. if (function_exists('openssl_random_pseudo_bytes') AND (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN')) {
  11810. // this is not used on windows systems because it is very slow for a know bug
  11811. $seed .= openssl_random_pseudo_bytes(512);
  11812. } else {
  11813. for ($i = 0; $i < 23; ++$i) {
  11814. $seed .= uniqid('', true);
  11815. }
  11816. }
  11817. $seed .= uniqid('', true);
  11818. $seed .= rand();
  11819. $seed .= getmypid();
  11820. $seed .= __FILE__;
  11821. $seed .= $this->bufferlen;
  11822. if (isset($_SERVER['REMOTE_ADDR'])) {
  11823. $seed .= $_SERVER['REMOTE_ADDR'];
  11824. }
  11825. if (isset($_SERVER['HTTP_USER_AGENT'])) {
  11826. $seed .= $_SERVER['HTTP_USER_AGENT'];
  11827. }
  11828. if (isset($_SERVER['HTTP_ACCEPT'])) {
  11829. $seed .= $_SERVER['HTTP_ACCEPT'];
  11830. }
  11831. if (isset($_SERVER['HTTP_ACCEPT_ENCODING'])) {
  11832. $seed .= $_SERVER['HTTP_ACCEPT_ENCODING'];
  11833. }
  11834. if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
  11835. $seed .= $_SERVER['HTTP_ACCEPT_LANGUAGE'];
  11836. }
  11837. if (isset($_SERVER['HTTP_ACCEPT_CHARSET'])) {
  11838. $seed .= $_SERVER['HTTP_ACCEPT_CHARSET'];
  11839. }
  11840. $seed .= rand();
  11841. $seed .= uniqid('', true);
  11842. $seed .= microtime();
  11843. return $seed;
  11844. }
  11845. /**
  11846. * Compute encryption key depending on object number where the encrypted data is stored.
  11847. * This is used for all strings and streams without crypt filter specifier.
  11848. * @param $n (int) object number
  11849. * @return int object key
  11850. * @protected
  11851. * @author Nicola Asuni
  11852. * @since 2.0.000 (2008-01-02)
  11853. */
  11854. protected function _objectkey($n) {
  11855. $objkey = $this->encryptdata['key'].pack('VXxx', $n);
  11856. if ($this->encryptdata['mode'] == 2) { // AES-128
  11857. // AES padding
  11858. $objkey .= "\x73\x41\x6C\x54"; // sAlT
  11859. }
  11860. $objkey = substr($this->_md5_16($objkey), 0, (($this->encryptdata['Length'] / 8) + 5));
  11861. $objkey = substr($objkey, 0, 16);
  11862. return $objkey;
  11863. }
  11864. /**
  11865. * Encrypt the input string.
  11866. * @param $n (int) object number
  11867. * @param $s (string) data string to encrypt
  11868. * @return encrypted string
  11869. * @protected
  11870. * @author Nicola Asuni
  11871. * @since 5.0.005 (2010-05-11)
  11872. */
  11873. protected function _encrypt_data($n, $s) {
  11874. if (!$this->encrypted) {
  11875. return $s;
  11876. }
  11877. switch ($this->encryptdata['mode']) {
  11878. case 0: // RC4-40
  11879. case 1: { // RC4-128
  11880. $s = $this->_RC4($this->_objectkey($n), $s);
  11881. break;
  11882. }
  11883. case 2: { // AES-128
  11884. $s = $this->_AES($this->_objectkey($n), $s);
  11885. break;
  11886. }
  11887. case 3: { // AES-256
  11888. $s = $this->_AES($this->encryptdata['key'], $s);
  11889. break;
  11890. }
  11891. }
  11892. return $s;
  11893. }
  11894. /**
  11895. * Put encryption on PDF document.
  11896. * @protected
  11897. * @author Nicola Asuni
  11898. * @since 2.0.000 (2008-01-02)
  11899. */
  11900. protected function _putencryption() {
  11901. if (!$this->encrypted) {
  11902. return;
  11903. }
  11904. $this->encryptdata['objid'] = $this->_newobj();
  11905. $out = '<<';
  11906. if (!isset($this->encryptdata['Filter']) OR empty($this->encryptdata['Filter'])) {
  11907. $this->encryptdata['Filter'] = 'Standard';
  11908. }
  11909. $out .= ' /Filter /'.$this->encryptdata['Filter'];
  11910. if (isset($this->encryptdata['SubFilter']) AND !empty($this->encryptdata['SubFilter'])) {
  11911. $out .= ' /SubFilter /'.$this->encryptdata['SubFilter'];
  11912. }
  11913. if (!isset($this->encryptdata['V']) OR empty($this->encryptdata['V'])) {
  11914. $this->encryptdata['V'] = 1;
  11915. }
  11916. // V is a code specifying the algorithm to be used in encrypting and decrypting the document
  11917. $out .= ' /V '.$this->encryptdata['V'];
  11918. if (isset($this->encryptdata['Length']) AND !empty($this->encryptdata['Length'])) {
  11919. // The length of the encryption key, in bits. The value shall be a multiple of 8, in the range 40 to 256
  11920. $out .= ' /Length '.$this->encryptdata['Length'];
  11921. } else {
  11922. $out .= ' /Length 40';
  11923. }
  11924. if ($this->encryptdata['V'] >= 4) {
  11925. if (!isset($this->encryptdata['StmF']) OR empty($this->encryptdata['StmF'])) {
  11926. $this->encryptdata['StmF'] = 'Identity';
  11927. }
  11928. if (!isset($this->encryptdata['StrF']) OR empty($this->encryptdata['StrF'])) {
  11929. // The name of the crypt filter that shall be used when decrypting all strings in the document.
  11930. $this->encryptdata['StrF'] = 'Identity';
  11931. }
  11932. // A dictionary whose keys shall be crypt filter names and whose values shall be the corresponding crypt filter dictionaries.
  11933. if (isset($this->encryptdata['CF']) AND !empty($this->encryptdata['CF'])) {
  11934. $out .= ' /CF <<';
  11935. $out .= ' /'.$this->encryptdata['StmF'].' <<';
  11936. $out .= ' /Type /CryptFilter';
  11937. if (isset($this->encryptdata['CF']['CFM']) AND !empty($this->encryptdata['CF']['CFM'])) {
  11938. // The method used
  11939. $out .= ' /CFM /'.$this->encryptdata['CF']['CFM'];
  11940. if ($this->encryptdata['pubkey']) {
  11941. $out .= ' /Recipients [';
  11942. foreach ($this->encryptdata['Recipients'] as $rec) {
  11943. $out .= ' <'.$rec.'>';
  11944. }
  11945. $out .= ' ]';
  11946. if (isset($this->encryptdata['CF']['EncryptMetadata']) AND (!$this->encryptdata['CF']['EncryptMetadata'])) {
  11947. $out .= ' /EncryptMetadata false';
  11948. } else {
  11949. $out .= ' /EncryptMetadata true';
  11950. }
  11951. }
  11952. } else {
  11953. $out .= ' /CFM /None';
  11954. }
  11955. if (isset($this->encryptdata['CF']['AuthEvent']) AND !empty($this->encryptdata['CF']['AuthEvent'])) {
  11956. // The event to be used to trigger the authorization that is required to access encryption keys used by this filter.
  11957. $out .= ' /AuthEvent /'.$this->encryptdata['CF']['AuthEvent'];
  11958. } else {
  11959. $out .= ' /AuthEvent /DocOpen';
  11960. }
  11961. if (isset($this->encryptdata['CF']['Length']) AND !empty($this->encryptdata['CF']['Length'])) {
  11962. // The bit length of the encryption key.
  11963. $out .= ' /Length '.$this->encryptdata['CF']['Length'];
  11964. }
  11965. $out .= ' >> >>';
  11966. }
  11967. // The name of the crypt filter that shall be used by default when decrypting streams.
  11968. $out .= ' /StmF /'.$this->encryptdata['StmF'];
  11969. // The name of the crypt filter that shall be used when decrypting all strings in the document.
  11970. $out .= ' /StrF /'.$this->encryptdata['StrF'];
  11971. if (isset($this->encryptdata['EFF']) AND !empty($this->encryptdata['EFF'])) {
  11972. // The name of the crypt filter that shall be used when encrypting embedded file streams that do not have their own crypt filter specifier.
  11973. $out .= ' /EFF /'.$this->encryptdata[''];
  11974. }
  11975. }
  11976. // Additional encryption dictionary entries for the standard security handler
  11977. if ($this->encryptdata['pubkey']) {
  11978. if (($this->encryptdata['V'] < 4) AND isset($this->encryptdata['Recipients']) AND !empty($this->encryptdata['Recipients'])) {
  11979. $out .= ' /Recipients [';
  11980. foreach ($this->encryptdata['Recipients'] as $rec) {
  11981. $out .= ' <'.$rec.'>';
  11982. }
  11983. $out .= ' ]';
  11984. }
  11985. } else {
  11986. $out .= ' /R';
  11987. if ($this->encryptdata['V'] == 5) { // AES-256
  11988. $out .= ' 5';
  11989. $out .= ' /OE ('.$this->_escape($this->encryptdata['OE']).')';
  11990. $out .= ' /UE ('.$this->_escape($this->encryptdata['UE']).')';
  11991. $out .= ' /Perms ('.$this->_escape($this->encryptdata['perms']).')';
  11992. } elseif ($this->encryptdata['V'] == 4) { // AES-128
  11993. $out .= ' 4';
  11994. } elseif ($this->encryptdata['V'] < 2) { // RC-40
  11995. $out .= ' 2';
  11996. } else { // RC-128
  11997. $out .= ' 3';
  11998. }
  11999. $out .= ' /O ('.$this->_escape($this->encryptdata['O']).')';
  12000. $out .= ' /U ('.$this->_escape($this->encryptdata['U']).')';
  12001. $out .= ' /P '.$this->encryptdata['P'];
  12002. if (isset($this->encryptdata['EncryptMetadata']) AND (!$this->encryptdata['EncryptMetadata'])) {
  12003. $out .= ' /EncryptMetadata false';
  12004. } else {
  12005. $out .= ' /EncryptMetadata true';
  12006. }
  12007. }
  12008. $out .= ' >>';
  12009. $out .= "\n".'endobj';
  12010. $this->_out($out);
  12011. }
  12012. /**
  12013. * Returns the input text encrypted using RC4 algorithm and the specified key.
  12014. * RC4 is the standard encryption algorithm used in PDF format
  12015. * @param $key (string) encryption key
  12016. * @param $text (String) input text to be encrypted
  12017. * @return String encrypted text
  12018. * @protected
  12019. * @since 2.0.000 (2008-01-02)
  12020. * @author Klemen Vodopivec, Nicola Asuni
  12021. */
  12022. protected function _RC4($key, $text) {
  12023. if (function_exists('mcrypt_decrypt') AND ($out = @mcrypt_decrypt(MCRYPT_ARCFOUR, $key, $text, MCRYPT_MODE_STREAM, ''))) {
  12024. // try to use mcrypt function if exist
  12025. return $out;
  12026. }
  12027. if ($this->last_enc_key != $key) {
  12028. $k = str_repeat($key, ((256 / strlen($key)) + 1));
  12029. $rc4 = range(0, 255);
  12030. $j = 0;
  12031. for ($i = 0; $i < 256; ++$i) {
  12032. $t = $rc4[$i];
  12033. $j = ($j + $t + ord($k{$i})) % 256;
  12034. $rc4[$i] = $rc4[$j];
  12035. $rc4[$j] = $t;
  12036. }
  12037. $this->last_enc_key = $key;
  12038. $this->last_enc_key_c = $rc4;
  12039. } else {
  12040. $rc4 = $this->last_enc_key_c;
  12041. }
  12042. $len = strlen($text);
  12043. $a = 0;
  12044. $b = 0;
  12045. $out = '';
  12046. for ($i = 0; $i < $len; ++$i) {
  12047. $a = ($a + 1) % 256;
  12048. $t = $rc4[$a];
  12049. $b = ($b + $t) % 256;
  12050. $rc4[$a] = $rc4[$b];
  12051. $rc4[$b] = $t;
  12052. $k = $rc4[($rc4[$a] + $rc4[$b]) % 256];
  12053. $out .= chr(ord($text{$i}) ^ $k);
  12054. }
  12055. return $out;
  12056. }
  12057. /**
  12058. * Returns the input text exrypted using AES algorithm and the specified key.
  12059. * This method requires mcrypt.
  12060. * @param $key (string) encryption key
  12061. * @param $text (String) input text to be encrypted
  12062. * @return String encrypted text
  12063. * @protected
  12064. * @author Nicola Asuni
  12065. * @since 5.0.005 (2010-05-11)
  12066. */
  12067. protected function _AES($key, $text) {
  12068. // padding (RFC 2898, PKCS #5: Password-Based Cryptography Specification Version 2.0)
  12069. $padding = 16 - (strlen($text) % 16);
  12070. $text .= str_repeat(chr($padding), $padding);
  12071. $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), MCRYPT_RAND);
  12072. $text = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $text, MCRYPT_MODE_CBC, $iv);
  12073. $text = $iv.$text;
  12074. return $text;
  12075. }
  12076. /**
  12077. * Encrypts a string using MD5 and returns it's value as a binary string.
  12078. * @param $str (string) input string
  12079. * @return String MD5 encrypted binary string
  12080. * @protected
  12081. * @since 2.0.000 (2008-01-02)
  12082. * @author Klemen Vodopivec
  12083. */
  12084. protected function _md5_16($str) {
  12085. return pack('H*', md5($str));
  12086. }
  12087. /**
  12088. * Compute U value (used for encryption)
  12089. * @return string U value
  12090. * @protected
  12091. * @since 2.0.000 (2008-01-02)
  12092. * @author Nicola Asuni
  12093. */
  12094. protected function _Uvalue() {
  12095. if ($this->encryptdata['mode'] == 0) { // RC4-40
  12096. return $this->_RC4($this->encryptdata['key'], $this->enc_padding);
  12097. } elseif ($this->encryptdata['mode'] < 3) { // RC4-128, AES-128
  12098. $tmp = $this->_md5_16($this->enc_padding.$this->encryptdata['fileid']);
  12099. $enc = $this->_RC4($this->encryptdata['key'], $tmp);
  12100. $len = strlen($tmp);
  12101. for ($i = 1; $i <= 19; ++$i) {
  12102. $ek = '';
  12103. for ($j = 0; $j < $len; ++$j) {
  12104. $ek .= chr(ord($this->encryptdata['key']{$j}) ^ $i);
  12105. }
  12106. $enc = $this->_RC4($ek, $enc);
  12107. }
  12108. $enc .= str_repeat("\x00", 16);
  12109. return substr($enc, 0, 32);
  12110. } elseif ($this->encryptdata['mode'] == 3) { // AES-256
  12111. $seed = $this->_md5_16($this->getRandomSeed());
  12112. // User Validation Salt
  12113. $this->encryptdata['UVS'] = substr($seed, 0, 8);
  12114. // User Key Salt
  12115. $this->encryptdata['UKS'] = substr($seed, 8, 16);
  12116. return hash('sha256', $this->encryptdata['user_password'].$this->encryptdata['UVS'], true).$this->encryptdata['UVS'].$this->encryptdata['UKS'];
  12117. }
  12118. }
  12119. /**
  12120. * Compute UE value (used for encryption)
  12121. * @return string UE value
  12122. * @protected
  12123. * @since 5.9.006 (2010-10-19)
  12124. * @author Nicola Asuni
  12125. */
  12126. protected function _UEvalue() {
  12127. $hashkey = hash('sha256', $this->encryptdata['user_password'].$this->encryptdata['UKS'], true);
  12128. $iv = str_repeat("\x00", mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC));
  12129. return mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $hashkey, $this->encryptdata['key'], MCRYPT_MODE_CBC, $iv);
  12130. }
  12131. /**
  12132. * Compute O value (used for encryption)
  12133. * @return string O value
  12134. * @protected
  12135. * @since 2.0.000 (2008-01-02)
  12136. * @author Nicola Asuni
  12137. */
  12138. protected function _Ovalue() {
  12139. if ($this->encryptdata['mode'] < 3) { // RC4-40, RC4-128, AES-128
  12140. $tmp = $this->_md5_16($this->encryptdata['owner_password']);
  12141. if ($this->encryptdata['mode'] > 0) {
  12142. for ($i = 0; $i < 50; ++$i) {
  12143. $tmp = $this->_md5_16($tmp);
  12144. }
  12145. }
  12146. $owner_key = substr($tmp, 0, ($this->encryptdata['Length'] / 8));
  12147. $enc = $this->_RC4($owner_key, $this->encryptdata['user_password']);
  12148. if ($this->encryptdata['mode'] > 0) {
  12149. $len = strlen($owner_key);
  12150. for ($i = 1; $i <= 19; ++$i) {
  12151. $ek = '';
  12152. for ($j = 0; $j < $len; ++$j) {
  12153. $ek .= chr(ord($owner_key{$j}) ^ $i);
  12154. }
  12155. $enc = $this->_RC4($ek, $enc);
  12156. }
  12157. }
  12158. return $enc;
  12159. } elseif ($this->encryptdata['mode'] == 3) { // AES-256
  12160. $seed = $this->_md5_16($this->getRandomSeed());
  12161. // Owner Validation Salt
  12162. $this->encryptdata['OVS'] = substr($seed, 0, 8);
  12163. // Owner Key Salt
  12164. $this->encryptdata['OKS'] = substr($seed, 8, 16);
  12165. return hash('sha256', $this->encryptdata['owner_password'].$this->encryptdata['OVS'].$this->encryptdata['U'], true).$this->encryptdata['OVS'].$this->encryptdata['OKS'];
  12166. }
  12167. }
  12168. /**
  12169. * Compute OE value (used for encryption)
  12170. * @return string OE value
  12171. * @protected
  12172. * @since 5.9.006 (2010-10-19)
  12173. * @author Nicola Asuni
  12174. */
  12175. protected function _OEvalue() {
  12176. $hashkey = hash('sha256', $this->encryptdata['owner_password'].$this->encryptdata['OKS'].$this->encryptdata['U'], true);
  12177. $iv = str_repeat("\x00", mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC));
  12178. return mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $hashkey, $this->encryptdata['key'], MCRYPT_MODE_CBC, $iv);
  12179. }
  12180. /**
  12181. * Convert password for AES-256 encryption mode
  12182. * @param $password (string) password
  12183. * @return string password
  12184. * @protected
  12185. * @since 5.9.006 (2010-10-19)
  12186. * @author Nicola Asuni
  12187. */
  12188. protected function _fixAES256Password($password) {
  12189. $psw = ''; // password to be returned
  12190. $psw_array = $this->utf8Bidi($this->UTF8StringToArray($password), $password, $this->rtl);
  12191. foreach ($psw_array as $c) {
  12192. $psw .= $this->unichr($c);
  12193. }
  12194. return substr($psw, 0, 127);
  12195. }
  12196. /**
  12197. * Compute encryption key
  12198. * @protected
  12199. * @since 2.0.000 (2008-01-02)
  12200. * @author Nicola Asuni
  12201. */
  12202. protected function _generateencryptionkey() {
  12203. $keybytelen = ($this->encryptdata['Length'] / 8);
  12204. if (!$this->encryptdata['pubkey']) { // standard mode
  12205. if ($this->encryptdata['mode'] == 3) { // AES-256
  12206. // generate 256 bit random key
  12207. $this->encryptdata['key'] = substr(hash('sha256', $this->getRandomSeed(), true), 0, $keybytelen);
  12208. // truncate passwords
  12209. $this->encryptdata['user_password'] = $this->_fixAES256Password($this->encryptdata['user_password']);
  12210. $this->encryptdata['owner_password'] = $this->_fixAES256Password($this->encryptdata['owner_password']);
  12211. // Compute U value
  12212. $this->encryptdata['U'] = $this->_Uvalue();
  12213. // Compute UE value
  12214. $this->encryptdata['UE'] = $this->_UEvalue();
  12215. // Compute O value
  12216. $this->encryptdata['O'] = $this->_Ovalue();
  12217. // Compute OE value
  12218. $this->encryptdata['OE'] = $this->_OEvalue();
  12219. // Compute P value
  12220. $this->encryptdata['P'] = $this->encryptdata['protection'];
  12221. // Computing the encryption dictionary's Perms (permissions) value
  12222. $perms = $this->getEncPermissionsString($this->encryptdata['protection']); // bytes 0-3
  12223. $perms .= chr(255).chr(255).chr(255).chr(255); // bytes 4-7
  12224. if (isset($this->encryptdata['CF']['EncryptMetadata']) AND (!$this->encryptdata['CF']['EncryptMetadata'])) { // byte 8
  12225. $perms .= 'F';
  12226. } else {
  12227. $perms .= 'T';
  12228. }
  12229. $perms .= 'adb'; // bytes 9-11
  12230. $perms .= 'nick'; // bytes 12-15
  12231. $iv = str_repeat("\x00", mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB));
  12232. $this->encryptdata['perms'] = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $this->encryptdata['key'], $perms, MCRYPT_MODE_ECB, $iv);
  12233. } else { // RC4-40, RC4-128, AES-128
  12234. // Pad passwords
  12235. $this->encryptdata['user_password'] = substr($this->encryptdata['user_password'].$this->enc_padding, 0, 32);
  12236. $this->encryptdata['owner_password'] = substr($this->encryptdata['owner_password'].$this->enc_padding, 0, 32);
  12237. // Compute O value
  12238. $this->encryptdata['O'] = $this->_Ovalue();
  12239. // get default permissions (reverse byte order)
  12240. $permissions = $this->getEncPermissionsString($this->encryptdata['protection']);
  12241. // Compute encryption key
  12242. $tmp = $this->_md5_16($this->encryptdata['user_password'].$this->encryptdata['O'].$permissions.$this->encryptdata['fileid']);
  12243. if ($this->encryptdata['mode'] > 0) {
  12244. for ($i = 0; $i < 50; ++$i) {
  12245. $tmp = $this->_md5_16(substr($tmp, 0, $keybytelen));
  12246. }
  12247. }
  12248. $this->encryptdata['key'] = substr($tmp, 0, $keybytelen);
  12249. // Compute U value
  12250. $this->encryptdata['U'] = $this->_Uvalue();
  12251. // Compute P value
  12252. $this->encryptdata['P'] = $this->encryptdata['protection'];
  12253. }
  12254. } else { // Public-Key mode
  12255. // random 20-byte seed
  12256. $seed = sha1($this->getRandomSeed(), true);
  12257. $recipient_bytes = '';
  12258. foreach ($this->encryptdata['pubkeys'] as $pubkey) {
  12259. // for each public certificate
  12260. if (isset($pubkey['p'])) {
  12261. $pkprotection = $this->getUserPermissionCode($pubkey['p'], $this->encryptdata['mode']);
  12262. } else {
  12263. $pkprotection = $this->encryptdata['protection'];
  12264. }
  12265. // get default permissions (reverse byte order)
  12266. $pkpermissions = $this->getEncPermissionsString($pkprotection);
  12267. // envelope data
  12268. $envelope = $seed.$pkpermissions;
  12269. // write the envelope data to a temporary file
  12270. $tempkeyfile = tempnam(K_PATH_CACHE, 'tmpkey_');
  12271. $f = fopen($tempkeyfile, 'wb');
  12272. if (!$f) {
  12273. $this->Error('Unable to create temporary key file: '.$tempkeyfile);
  12274. }
  12275. $envelope_length = strlen($envelope);
  12276. fwrite($f, $envelope, $envelope_length);
  12277. fclose($f);
  12278. $tempencfile = tempnam(K_PATH_CACHE, 'tmpenc_');
  12279. if (!openssl_pkcs7_encrypt($tempkeyfile, $tempencfile, $pubkey['c'], array(), PKCS7_DETACHED | PKCS7_BINARY)) {
  12280. $this->Error('Unable to encrypt the file: '.$tempkeyfile);
  12281. }
  12282. unlink($tempkeyfile);
  12283. // read encryption signature
  12284. $signature = file_get_contents($tempencfile, false, null, $envelope_length);
  12285. unlink($tempencfile);
  12286. // extract signature
  12287. $signature = substr($signature, strpos($signature, 'Content-Disposition'));
  12288. $tmparr = explode("\n\n", $signature);
  12289. $signature = trim($tmparr[1]);
  12290. unset($tmparr);
  12291. // decode signature
  12292. $signature = base64_decode($signature);
  12293. // convert signature to hex
  12294. $hexsignature = current(unpack('H*', $signature));
  12295. // store signature on recipients array
  12296. $this->encryptdata['Recipients'][] = $hexsignature;
  12297. // The bytes of each item in the Recipients array of PKCS#7 objects in the order in which they appear in the array
  12298. $recipient_bytes .= $signature;
  12299. }
  12300. // calculate encryption key
  12301. if ($this->encryptdata['mode'] == 3) { // AES-256
  12302. $this->encryptdata['key'] = substr(hash('sha256', $seed.$recipient_bytes, true), 0, $keybytelen);
  12303. } else { // RC4-40, RC4-128, AES-128
  12304. $this->encryptdata['key'] = substr(sha1($seed.$recipient_bytes, true), 0, $keybytelen);
  12305. }
  12306. }
  12307. }
  12308. /**
  12309. * Return the premission code used on encryption (P value).
  12310. * @param $permissions (Array) the set of permissions (specify the ones you want to block).
  12311. * @param $mode (int) encryption strength: 0 = RC4 40 bit; 1 = RC4 128 bit; 2 = AES 128 bit; 3 = AES 256 bit.
  12312. * @protected
  12313. * @since 5.0.005 (2010-05-12)
  12314. * @author Nicola Asuni
  12315. */
  12316. protected function getUserPermissionCode($permissions, $mode=0) {
  12317. $options = array(
  12318. 'owner' => 2, // bit 2 -- inverted logic: cleared by default
  12319. 'print' => 4, // bit 3
  12320. 'modify' => 8, // bit 4
  12321. 'copy' => 16, // bit 5
  12322. 'annot-forms' => 32, // bit 6
  12323. 'fill-forms' => 256, // bit 9
  12324. 'extract' => 512, // bit 10
  12325. 'assemble' => 1024,// bit 11
  12326. 'print-high' => 2048 // bit 12
  12327. );
  12328. $protection = 2147422012; // 32 bit: (01111111 11111111 00001111 00111100)
  12329. foreach ($permissions as $permission) {
  12330. if (!isset($options[$permission])) {
  12331. $this->Error('Incorrect permission: '.$permission);
  12332. }
  12333. if (($mode > 0) OR ($options[$permission] <= 32)) {
  12334. // set only valid permissions
  12335. if ($options[$permission] == 2) {
  12336. // the logic for bit 2 is inverted (cleared by default)
  12337. $protection += $options[$permission];
  12338. } else {
  12339. $protection -= $options[$permission];
  12340. }
  12341. }
  12342. }
  12343. return $protection;
  12344. }
  12345. /**
  12346. * Set document protection
  12347. * Remark: the protection against modification is for people who have the full Acrobat product.
  12348. * If you don't set any password, the document will open as usual. If you set a user password, the PDF viewer will ask for it before displaying the document. The master password, if different from the user one, can be used to get full access.
  12349. * Note: protecting a document requires to encrypt it, which increases the processing time a lot. This can cause a PHP time-out in some cases, especially if the document contains images or fonts.
  12350. * @param $permissions (Array) the set of permissions (specify the ones you want to block):<ul><li>print : Print the document;</li><li>modify : Modify the contents of the document by operations other than those controlled by 'fill-forms', 'extract' and 'assemble';</li><li>copy : Copy or otherwise extract text and graphics from the document;</li><li>annot-forms : Add or modify text annotations, fill in interactive form fields, and, if 'modify' is also set, create or modify interactive form fields (including signature fields);</li><li>fill-forms : Fill in existing interactive form fields (including signature fields), even if 'annot-forms' is not specified;</li><li>extract : Extract text and graphics (in support of accessibility to users with disabilities or for other purposes);</li><li>assemble : Assemble the document (insert, rotate, or delete pages and create bookmarks or thumbnail images), even if 'modify' is not set;</li><li>print-high : Print the document to a representation from which a faithful digital copy of the PDF content could be generated. When this is not set, printing is limited to a low-level representation of the appearance, possibly of degraded quality.</li><li>owner : (inverted logic - only for public-key) when set permits change of encryption and enables all other permissions.</li></ul>
  12351. * @param $user_pass (String) user password. Empty by default.
  12352. * @param $owner_pass (String) owner password. If not specified, a random value is used.
  12353. * @param $mode (int) encryption strength: 0 = RC4 40 bit; 1 = RC4 128 bit; 2 = AES 128 bit; 3 = AES 256 bit.
  12354. * @param $pubkeys (String) array of recipients containing public-key certificates ('c') and permissions ('p'). For example: array(array('c' => 'file://../tcpdf.crt', 'p' => array('print')))
  12355. * @public
  12356. * @since 2.0.000 (2008-01-02)
  12357. * @author Nicola Asuni
  12358. */
  12359. public function SetProtection($permissions=array('print', 'modify', 'copy', 'annot-forms', 'fill-forms', 'extract', 'assemble', 'print-high'), $user_pass='', $owner_pass=null, $mode=0, $pubkeys=null) {
  12360. $this->encryptdata['protection'] = $this->getUserPermissionCode($permissions, $mode);
  12361. if (($pubkeys !== null) AND (is_array($pubkeys))) {
  12362. // public-key mode
  12363. $this->encryptdata['pubkeys'] = $pubkeys;
  12364. if ($mode == 0) {
  12365. // public-Key Security requires at least 128 bit
  12366. $mode = 1;
  12367. }
  12368. if (!function_exists('openssl_pkcs7_encrypt')) {
  12369. $this->Error('Public-Key Security requires openssl library.');
  12370. }
  12371. // Set Public-Key filter (availabe are: Entrust.PPKEF, Adobe.PPKLite, Adobe.PubSec)
  12372. $this->encryptdata['pubkey'] = true;
  12373. $this->encryptdata['Filter'] = 'Adobe.PubSec';
  12374. $this->encryptdata['StmF'] = 'DefaultCryptFilter';
  12375. $this->encryptdata['StrF'] = 'DefaultCryptFilter';
  12376. } else {
  12377. // standard mode (password mode)
  12378. $this->encryptdata['pubkey'] = false;
  12379. $this->encryptdata['Filter'] = 'Standard';
  12380. $this->encryptdata['StmF'] = 'StdCF';
  12381. $this->encryptdata['StrF'] = 'StdCF';
  12382. }
  12383. if ($mode > 1) { // AES
  12384. if (!extension_loaded('mcrypt')) {
  12385. $this->Error('AES encryption requires mcrypt library (http://www.php.net/manual/en/mcrypt.requirements.php).');
  12386. }
  12387. if (mcrypt_get_cipher_name(MCRYPT_RIJNDAEL_128) === false) {
  12388. $this->Error('AES encryption requires MCRYPT_RIJNDAEL_128 cypher.');
  12389. }
  12390. if (($mode == 3) AND !function_exists('hash')) {
  12391. // the Hash extension requires no external libraries and is enabled by default as of PHP 5.1.2.
  12392. $this->Error('AES 256 encryption requires HASH Message Digest Framework (http://www.php.net/manual/en/book.hash.php).');
  12393. }
  12394. }
  12395. if ($owner_pass === null) {
  12396. $owner_pass = md5($this->getRandomSeed());
  12397. }
  12398. $this->encryptdata['user_password'] = $user_pass;
  12399. $this->encryptdata['owner_password'] = $owner_pass;
  12400. $this->encryptdata['mode'] = $mode;
  12401. switch ($mode) {
  12402. case 0: { // RC4 40 bit
  12403. $this->encryptdata['V'] = 1;
  12404. $this->encryptdata['Length'] = 40;
  12405. $this->encryptdata['CF']['CFM'] = 'V2';
  12406. break;
  12407. }
  12408. case 1: { // RC4 128 bit
  12409. $this->encryptdata['V'] = 2;
  12410. $this->encryptdata['Length'] = 128;
  12411. $this->encryptdata['CF']['CFM'] = 'V2';
  12412. if ($this->encryptdata['pubkey']) {
  12413. $this->encryptdata['SubFilter'] = 'adbe.pkcs7.s4';
  12414. $this->encryptdata['Recipients'] = array();
  12415. }
  12416. break;
  12417. }
  12418. case 2: { // AES 128 bit
  12419. $this->encryptdata['V'] = 4;
  12420. $this->encryptdata['Length'] = 128;
  12421. $this->encryptdata['CF']['CFM'] = 'AESV2';
  12422. $this->encryptdata['CF']['Length'] = 128;
  12423. if ($this->encryptdata['pubkey']) {
  12424. $this->encryptdata['SubFilter'] = 'adbe.pkcs7.s5';
  12425. $this->encryptdata['Recipients'] = array();
  12426. }
  12427. break;
  12428. }
  12429. case 3: { // AES 256 bit
  12430. $this->encryptdata['V'] = 5;
  12431. $this->encryptdata['Length'] = 256;
  12432. $this->encryptdata['CF']['CFM'] = 'AESV3';
  12433. $this->encryptdata['CF']['Length'] = 256;
  12434. if ($this->encryptdata['pubkey']) {
  12435. $this->encryptdata['SubFilter'] = 'adbe.pkcs7.s5';
  12436. $this->encryptdata['Recipients'] = array();
  12437. }
  12438. break;
  12439. }
  12440. }
  12441. $this->encrypted = true;
  12442. $this->encryptdata['fileid'] = $this->convertHexStringToString($this->file_id);
  12443. $this->_generateencryptionkey();
  12444. }
  12445. /**
  12446. * Convert hexadecimal string to string
  12447. * @param $bs (string) byte-string to convert
  12448. * @return String
  12449. * @protected
  12450. * @since 5.0.005 (2010-05-12)
  12451. * @author Nicola Asuni
  12452. */
  12453. protected function convertHexStringToString($bs) {
  12454. $string = ''; // string to be returned
  12455. $bslength = strlen($bs);
  12456. if (($bslength % 2) != 0) {
  12457. // padding
  12458. $bs .= '0';
  12459. ++$bslength;
  12460. }
  12461. for ($i = 0; $i < $bslength; $i += 2) {
  12462. $string .= chr(hexdec($bs{$i}.$bs{($i + 1)}));
  12463. }
  12464. return $string;
  12465. }
  12466. /**
  12467. * Convert string to hexadecimal string (byte string)
  12468. * @param $s (string) string to convert
  12469. * @return byte string
  12470. * @protected
  12471. * @since 5.0.010 (2010-05-17)
  12472. * @author Nicola Asuni
  12473. */
  12474. protected function convertStringToHexString($s) {
  12475. $bs = '';
  12476. $chars = preg_split('//', $s, -1, PREG_SPLIT_NO_EMPTY);
  12477. foreach ($chars as $c) {
  12478. $bs .= sprintf('%02s', dechex(ord($c)));
  12479. }
  12480. return $bs;
  12481. }
  12482. /**
  12483. * Convert encryption P value to a string of bytes, low-order byte first.
  12484. * @param $protection (string) 32bit encryption permission value (P value)
  12485. * @return String
  12486. * @protected
  12487. * @since 5.0.005 (2010-05-12)
  12488. * @author Nicola Asuni
  12489. */
  12490. protected function getEncPermissionsString($protection) {
  12491. $binprot = sprintf('%032b', $protection);
  12492. $str = chr(bindec(substr($binprot, 24, 8)));
  12493. $str .= chr(bindec(substr($binprot, 16, 8)));
  12494. $str .= chr(bindec(substr($binprot, 8, 8)));
  12495. $str .= chr(bindec(substr($binprot, 0, 8)));
  12496. return $str;
  12497. }
  12498. // END OF ENCRYPTION FUNCTIONS -------------------------
  12499. // START TRANSFORMATIONS SECTION -----------------------
  12500. /**
  12501. * Starts a 2D tranformation saving current graphic state.
  12502. * This function must be called before scaling, mirroring, translation, rotation and skewing.
  12503. * Use StartTransform() before, and StopTransform() after the transformations to restore the normal behavior.
  12504. * @public
  12505. * @since 2.1.000 (2008-01-07)
  12506. * @see StartTransform(), StopTransform()
  12507. */
  12508. public function StartTransform() {
  12509. $this->_out('q');
  12510. if ($this->inxobj) {
  12511. // we are inside an XObject template
  12512. $this->xobjects[$this->xobjid]['transfmrk'][] = strlen($this->xobjects[$this->xobjid]['outdata']);
  12513. } else {
  12514. $this->transfmrk[$this->page][] = $this->pagelen[$this->page];
  12515. }
  12516. ++$this->transfmatrix_key;
  12517. $this->transfmatrix[$this->transfmatrix_key] = array();
  12518. }
  12519. /**
  12520. * Stops a 2D tranformation restoring previous graphic state.
  12521. * This function must be called after scaling, mirroring, translation, rotation and skewing.
  12522. * Use StartTransform() before, and StopTransform() after the transformations to restore the normal behavior.
  12523. * @public
  12524. * @since 2.1.000 (2008-01-07)
  12525. * @see StartTransform(), StopTransform()
  12526. */
  12527. public function StopTransform() {
  12528. $this->_out('Q');
  12529. if (isset($this->transfmatrix[$this->transfmatrix_key])) {
  12530. array_pop($this->transfmatrix[$this->transfmatrix_key]);
  12531. --$this->transfmatrix_key;
  12532. }
  12533. if ($this->inxobj) {
  12534. // we are inside an XObject template
  12535. array_pop($this->xobjects[$this->xobjid]['transfmrk']);
  12536. } else {
  12537. array_pop($this->transfmrk[$this->page]);
  12538. }
  12539. }
  12540. /**
  12541. * Horizontal Scaling.
  12542. * @param $s_x (float) scaling factor for width as percent. 0 is not allowed.
  12543. * @param $x (int) abscissa of the scaling center. Default is current x position
  12544. * @param $y (int) ordinate of the scaling center. Default is current y position
  12545. * @public
  12546. * @since 2.1.000 (2008-01-07)
  12547. * @see StartTransform(), StopTransform()
  12548. */
  12549. public function ScaleX($s_x, $x='', $y='') {
  12550. $this->Scale($s_x, 100, $x, $y);
  12551. }
  12552. /**
  12553. * Vertical Scaling.
  12554. * @param $s_y (float) scaling factor for height as percent. 0 is not allowed.
  12555. * @param $x (int) abscissa of the scaling center. Default is current x position
  12556. * @param $y (int) ordinate of the scaling center. Default is current y position
  12557. * @public
  12558. * @since 2.1.000 (2008-01-07)
  12559. * @see StartTransform(), StopTransform()
  12560. */
  12561. public function ScaleY($s_y, $x='', $y='') {
  12562. $this->Scale(100, $s_y, $x, $y);
  12563. }
  12564. /**
  12565. * Vertical and horizontal proportional Scaling.
  12566. * @param $s (float) scaling factor for width and height as percent. 0 is not allowed.
  12567. * @param $x (int) abscissa of the scaling center. Default is current x position
  12568. * @param $y (int) ordinate of the scaling center. Default is current y position
  12569. * @public
  12570. * @since 2.1.000 (2008-01-07)
  12571. * @see StartTransform(), StopTransform()
  12572. */
  12573. public function ScaleXY($s, $x='', $y='') {
  12574. $this->Scale($s, $s, $x, $y);
  12575. }
  12576. /**
  12577. * Vertical and horizontal non-proportional Scaling.
  12578. * @param $s_x (float) scaling factor for width as percent. 0 is not allowed.
  12579. * @param $s_y (float) scaling factor for height as percent. 0 is not allowed.
  12580. * @param $x (int) abscissa of the scaling center. Default is current x position
  12581. * @param $y (int) ordinate of the scaling center. Default is current y position
  12582. * @public
  12583. * @since 2.1.000 (2008-01-07)
  12584. * @see StartTransform(), StopTransform()
  12585. */
  12586. public function Scale($s_x, $s_y, $x='', $y='') {
  12587. if ($x === '') {
  12588. $x = $this->x;
  12589. }
  12590. if ($y === '') {
  12591. $y = $this->y;
  12592. }
  12593. if (($s_x == 0) OR ($s_y == 0)) {
  12594. $this->Error('Please do not use values equal to zero for scaling');
  12595. }
  12596. $y = ($this->h - $y) * $this->k;
  12597. $x *= $this->k;
  12598. //calculate elements of transformation matrix
  12599. $s_x /= 100;
  12600. $s_y /= 100;
  12601. $tm = array();
  12602. $tm[0] = $s_x;
  12603. $tm[1] = 0;
  12604. $tm[2] = 0;
  12605. $tm[3] = $s_y;
  12606. $tm[4] = $x * (1 - $s_x);
  12607. $tm[5] = $y * (1 - $s_y);
  12608. //scale the coordinate system
  12609. $this->Transform($tm);
  12610. }
  12611. /**
  12612. * Horizontal Mirroring.
  12613. * @param $x (int) abscissa of the point. Default is current x position
  12614. * @public
  12615. * @since 2.1.000 (2008-01-07)
  12616. * @see StartTransform(), StopTransform()
  12617. */
  12618. public function MirrorH($x='') {
  12619. $this->Scale(-100, 100, $x);
  12620. }
  12621. /**
  12622. * Verical Mirroring.
  12623. * @param $y (int) ordinate of the point. Default is current y position
  12624. * @public
  12625. * @since 2.1.000 (2008-01-07)
  12626. * @see StartTransform(), StopTransform()
  12627. */
  12628. public function MirrorV($y='') {
  12629. $this->Scale(100, -100, '', $y);
  12630. }
  12631. /**
  12632. * Point reflection mirroring.
  12633. * @param $x (int) abscissa of the point. Default is current x position
  12634. * @param $y (int) ordinate of the point. Default is current y position
  12635. * @public
  12636. * @since 2.1.000 (2008-01-07)
  12637. * @see StartTransform(), StopTransform()
  12638. */
  12639. public function MirrorP($x='',$y='') {
  12640. $this->Scale(-100, -100, $x, $y);
  12641. }
  12642. /**
  12643. * Reflection against a straight line through point (x, y) with the gradient angle (angle).
  12644. * @param $angle (float) gradient angle of the straight line. Default is 0 (horizontal line).
  12645. * @param $x (int) abscissa of the point. Default is current x position
  12646. * @param $y (int) ordinate of the point. Default is current y position
  12647. * @public
  12648. * @since 2.1.000 (2008-01-07)
  12649. * @see StartTransform(), StopTransform()
  12650. */
  12651. public function MirrorL($angle=0, $x='',$y='') {
  12652. $this->Scale(-100, 100, $x, $y);
  12653. $this->Rotate(-2*($angle-90), $x, $y);
  12654. }
  12655. /**
  12656. * Translate graphic object horizontally.
  12657. * @param $t_x (int) movement to the right (or left for RTL)
  12658. * @public
  12659. * @since 2.1.000 (2008-01-07)
  12660. * @see StartTransform(), StopTransform()
  12661. */
  12662. public function TranslateX($t_x) {
  12663. $this->Translate($t_x, 0);
  12664. }
  12665. /**
  12666. * Translate graphic object vertically.
  12667. * @param $t_y (int) movement to the bottom
  12668. * @public
  12669. * @since 2.1.000 (2008-01-07)
  12670. * @see StartTransform(), StopTransform()
  12671. */
  12672. public function TranslateY($t_y) {
  12673. $this->Translate(0, $t_y);
  12674. }
  12675. /**
  12676. * Translate graphic object horizontally and vertically.
  12677. * @param $t_x (int) movement to the right
  12678. * @param $t_y (int) movement to the bottom
  12679. * @public
  12680. * @since 2.1.000 (2008-01-07)
  12681. * @see StartTransform(), StopTransform()
  12682. */
  12683. public function Translate($t_x, $t_y) {
  12684. //calculate elements of transformation matrix
  12685. $tm = array();
  12686. $tm[0] = 1;
  12687. $tm[1] = 0;
  12688. $tm[2] = 0;
  12689. $tm[3] = 1;
  12690. $tm[4] = $t_x * $this->k;
  12691. $tm[5] = -$t_y * $this->k;
  12692. //translate the coordinate system
  12693. $this->Transform($tm);
  12694. }
  12695. /**
  12696. * Rotate object.
  12697. * @param $angle (float) angle in degrees for counter-clockwise rotation
  12698. * @param $x (int) abscissa of the rotation center. Default is current x position
  12699. * @param $y (int) ordinate of the rotation center. Default is current y position
  12700. * @public
  12701. * @since 2.1.000 (2008-01-07)
  12702. * @see StartTransform(), StopTransform()
  12703. */
  12704. public function Rotate($angle, $x='', $y='') {
  12705. if ($x === '') {
  12706. $x = $this->x;
  12707. }
  12708. if ($y === '') {
  12709. $y = $this->y;
  12710. }
  12711. $y = ($this->h - $y) * $this->k;
  12712. $x *= $this->k;
  12713. //calculate elements of transformation matrix
  12714. $tm = array();
  12715. $tm[0] = cos(deg2rad($angle));
  12716. $tm[1] = sin(deg2rad($angle));
  12717. $tm[2] = -$tm[1];
  12718. $tm[3] = $tm[0];
  12719. $tm[4] = $x + ($tm[1] * $y) - ($tm[0] * $x);
  12720. $tm[5] = $y - ($tm[0] * $y) - ($tm[1] * $x);
  12721. //rotate the coordinate system around ($x,$y)
  12722. $this->Transform($tm);
  12723. }
  12724. /**
  12725. * Skew horizontally.
  12726. * @param $angle_x (float) angle in degrees between -90 (skew to the left) and 90 (skew to the right)
  12727. * @param $x (int) abscissa of the skewing center. default is current x position
  12728. * @param $y (int) ordinate of the skewing center. default is current y position
  12729. * @public
  12730. * @since 2.1.000 (2008-01-07)
  12731. * @see StartTransform(), StopTransform()
  12732. */
  12733. public function SkewX($angle_x, $x='', $y='') {
  12734. $this->Skew($angle_x, 0, $x, $y);
  12735. }
  12736. /**
  12737. * Skew vertically.
  12738. * @param $angle_y (float) angle in degrees between -90 (skew to the bottom) and 90 (skew to the top)
  12739. * @param $x (int) abscissa of the skewing center. default is current x position
  12740. * @param $y (int) ordinate of the skewing center. default is current y position
  12741. * @public
  12742. * @since 2.1.000 (2008-01-07)
  12743. * @see StartTransform(), StopTransform()
  12744. */
  12745. public function SkewY($angle_y, $x='', $y='') {
  12746. $this->Skew(0, $angle_y, $x, $y);
  12747. }
  12748. /**
  12749. * Skew.
  12750. * @param $angle_x (float) angle in degrees between -90 (skew to the left) and 90 (skew to the right)
  12751. * @param $angle_y (float) angle in degrees between -90 (skew to the bottom) and 90 (skew to the top)
  12752. * @param $x (int) abscissa of the skewing center. default is current x position
  12753. * @param $y (int) ordinate of the skewing center. default is current y position
  12754. * @public
  12755. * @since 2.1.000 (2008-01-07)
  12756. * @see StartTransform(), StopTransform()
  12757. */
  12758. public function Skew($angle_x, $angle_y, $x='', $y='') {
  12759. if ($x === '') {
  12760. $x = $this->x;
  12761. }
  12762. if ($y === '') {
  12763. $y = $this->y;
  12764. }
  12765. if (($angle_x <= -90) OR ($angle_x >= 90) OR ($angle_y <= -90) OR ($angle_y >= 90)) {
  12766. $this->Error('Please use values between -90 and +90 degrees for Skewing.');
  12767. }
  12768. $x *= $this->k;
  12769. $y = ($this->h - $y) * $this->k;
  12770. //calculate elements of transformation matrix
  12771. $tm = array();
  12772. $tm[0] = 1;
  12773. $tm[1] = tan(deg2rad($angle_y));
  12774. $tm[2] = tan(deg2rad($angle_x));
  12775. $tm[3] = 1;
  12776. $tm[4] = -$tm[2] * $y;
  12777. $tm[5] = -$tm[1] * $x;
  12778. //skew the coordinate system
  12779. $this->Transform($tm);
  12780. }
  12781. /**
  12782. * Apply graphic transformations.
  12783. * @param $tm (array) transformation matrix
  12784. * @protected
  12785. * @since 2.1.000 (2008-01-07)
  12786. * @see StartTransform(), StopTransform()
  12787. */
  12788. protected function Transform($tm) {
  12789. $this->_out(sprintf('%.3F %.3F %.3F %.3F %.3F %.3F cm', $tm[0], $tm[1], $tm[2], $tm[3], $tm[4], $tm[5]));
  12790. // add tranformation matrix
  12791. $this->transfmatrix[$this->transfmatrix_key][] = array('a' => $tm[0], 'b' => $tm[1], 'c' => $tm[2], 'd' => $tm[3], 'e' => $tm[4], 'f' => $tm[5]);
  12792. // update transformation mark
  12793. if ($this->inxobj) {
  12794. // we are inside an XObject template
  12795. if (end($this->xobjects[$this->xobjid]['transfmrk']) !== false) {
  12796. $key = key($this->xobjects[$this->xobjid]['transfmrk']);
  12797. $this->xobjects[$this->xobjid]['transfmrk'][$key] = strlen($this->xobjects[$this->xobjid]['outdata']);
  12798. }
  12799. } elseif (end($this->transfmrk[$this->page]) !== false) {
  12800. $key = key($this->transfmrk[$this->page]);
  12801. $this->transfmrk[$this->page][$key] = $this->pagelen[$this->page];
  12802. }
  12803. }
  12804. // END TRANSFORMATIONS SECTION -------------------------
  12805. // START GRAPHIC FUNCTIONS SECTION ---------------------
  12806. // The following section is based on the code provided by David Hernandez Sanz
  12807. /**
  12808. * Defines the line width. By default, the value equals 0.2 mm. The method can be called before the first page is created and the value is retained from page to page.
  12809. * @param $width (float) The width.
  12810. * @public
  12811. * @since 1.0
  12812. * @see Line(), Rect(), Cell(), MultiCell()
  12813. */
  12814. public function SetLineWidth($width) {
  12815. //Set line width
  12816. $this->LineWidth = $width;
  12817. $this->linestyleWidth = sprintf('%.2F w', ($width * $this->k));
  12818. if ($this->page > 0) {
  12819. $this->_out($this->linestyleWidth);
  12820. }
  12821. }
  12822. /**
  12823. * Returns the current the line width.
  12824. * @return int Line width
  12825. * @public
  12826. * @since 2.1.000 (2008-01-07)
  12827. * @see Line(), SetLineWidth()
  12828. */
  12829. public function GetLineWidth() {
  12830. return $this->LineWidth;
  12831. }
  12832. /**
  12833. * Set line style.
  12834. * @param $style (array) Line style. Array with keys among the following:
  12835. * <ul>
  12836. * <li>width (float): Width of the line in user units.</li>
  12837. * <li>cap (string): Type of cap to put on the line. Possible values are:
  12838. * butt, round, square. The difference between "square" and "butt" is that
  12839. * "square" projects a flat end past the end of the line.</li>
  12840. * <li>join (string): Type of join. Possible values are: miter, round,
  12841. * bevel.</li>
  12842. * <li>dash (mixed): Dash pattern. Is 0 (without dash) or string with
  12843. * series of length values, which are the lengths of the on and off dashes.
  12844. * For example: "2" represents 2 on, 2 off, 2 on, 2 off, ...; "2,1" is 2 on,
  12845. * 1 off, 2 on, 1 off, ...</li>
  12846. * <li>phase (integer): Modifier on the dash pattern which is used to shift
  12847. * the point at which the pattern starts.</li>
  12848. * <li>color (array): Draw color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K).</li>
  12849. * </ul>
  12850. * @param $ret (boolean) if true do not send the command.
  12851. * @return string the PDF command
  12852. * @public
  12853. * @since 2.1.000 (2008-01-08)
  12854. */
  12855. public function SetLineStyle($style, $ret=false) {
  12856. $s = ''; // string to be returned
  12857. if (!is_array($style)) {
  12858. return;
  12859. }
  12860. if (isset($style['width'])) {
  12861. $this->LineWidth = $style['width'];
  12862. $this->linestyleWidth = sprintf('%.2F w', ($style['width'] * $this->k));
  12863. $s .= $this->linestyleWidth.' ';
  12864. }
  12865. if (isset($style['cap'])) {
  12866. $ca = array('butt' => 0, 'round'=> 1, 'square' => 2);
  12867. if (isset($ca[$style['cap']])) {
  12868. $this->linestyleCap = $ca[$style['cap']].' J';
  12869. $s .= $this->linestyleCap.' ';
  12870. }
  12871. }
  12872. if (isset($style['join'])) {
  12873. $ja = array('miter' => 0, 'round' => 1, 'bevel' => 2);
  12874. if (isset($ja[$style['join']])) {
  12875. $this->linestyleJoin = $ja[$style['join']].' j';
  12876. $s .= $this->linestyleJoin.' ';
  12877. }
  12878. }
  12879. if (isset($style['dash'])) {
  12880. $dash_string = '';
  12881. if ($style['dash']) {
  12882. if (preg_match('/^.+,/', $style['dash']) > 0) {
  12883. $tab = explode(',', $style['dash']);
  12884. } else {
  12885. $tab = array($style['dash']);
  12886. }
  12887. $dash_string = '';
  12888. foreach ($tab as $i => $v) {
  12889. if ($i) {
  12890. $dash_string .= ' ';
  12891. }
  12892. $dash_string .= sprintf('%.2F', $v);
  12893. }
  12894. }
  12895. if (!isset($style['phase']) OR !$style['dash']) {
  12896. $style['phase'] = 0;
  12897. }
  12898. $this->linestyleDash = sprintf('[%s] %.2F d', $dash_string, $style['phase']);
  12899. $s .= $this->linestyleDash.' ';
  12900. }
  12901. if (isset($style['color'])) {
  12902. $s .= $this->SetDrawColorArray($style['color'], true).' ';
  12903. }
  12904. if (!$ret) {
  12905. $this->_out($s);
  12906. }
  12907. return $s;
  12908. }
  12909. /**
  12910. * Begin a new subpath by moving the current point to coordinates (x, y), omitting any connecting line segment.
  12911. * @param $x (float) Abscissa of point.
  12912. * @param $y (float) Ordinate of point.
  12913. * @protected
  12914. * @since 2.1.000 (2008-01-08)
  12915. */
  12916. protected function _outPoint($x, $y) {
  12917. $this->_out(sprintf('%.2F %.2F m', $x * $this->k, ($this->h - $y) * $this->k));
  12918. }
  12919. /**
  12920. * Append a straight line segment from the current point to the point (x, y).
  12921. * The new current point shall be (x, y).
  12922. * @param $x (float) Abscissa of end point.
  12923. * @param $y (float) Ordinate of end point.
  12924. * @protected
  12925. * @since 2.1.000 (2008-01-08)
  12926. */
  12927. protected function _outLine($x, $y) {
  12928. $this->_out(sprintf('%.2F %.2F l', $x * $this->k, ($this->h - $y) * $this->k));
  12929. }
  12930. /**
  12931. * Append a rectangle to the current path as a complete subpath, with lower-left corner (x, y) and dimensions widthand height in user space.
  12932. * @param $x (float) Abscissa of upper-left corner.
  12933. * @param $y (float) Ordinate of upper-left corner.
  12934. * @param $w (float) Width.
  12935. * @param $h (float) Height.
  12936. * @param $op (string) options
  12937. * @protected
  12938. * @since 2.1.000 (2008-01-08)
  12939. */
  12940. protected function _outRect($x, $y, $w, $h, $op) {
  12941. $this->_out(sprintf('%.2F %.2F %.2F %.2F re %s', $x * $this->k, ($this->h - $y) * $this->k, $w * $this->k, -$h * $this->k, $op));
  12942. }
  12943. /**
  12944. * Append a cubic Bézier curve to the current path. The curve shall extend from the current point to the point (x3, y3), using (x1, y1) and (x2, y2) as the Bézier control points.
  12945. * The new current point shall be (x3, y3).
  12946. * @param $x1 (float) Abscissa of control point 1.
  12947. * @param $y1 (float) Ordinate of control point 1.
  12948. * @param $x2 (float) Abscissa of control point 2.
  12949. * @param $y2 (float) Ordinate of control point 2.
  12950. * @param $x3 (float) Abscissa of end point.
  12951. * @param $y3 (float) Ordinate of end point.
  12952. * @protected
  12953. * @since 2.1.000 (2008-01-08)
  12954. */
  12955. protected function _outCurve($x1, $y1, $x2, $y2, $x3, $y3) {
  12956. $this->_out(sprintf('%.2F %.2F %.2F %.2F %.2F %.2F c', $x1 * $this->k, ($this->h - $y1) * $this->k, $x2 * $this->k, ($this->h - $y2) * $this->k, $x3 * $this->k, ($this->h - $y3) * $this->k));
  12957. }
  12958. /**
  12959. * Append a cubic Bézier curve to the current path. The curve shall extend from the current point to the point (x3, y3), using the current point and (x2, y2) as the Bézier control points.
  12960. * The new current point shall be (x3, y3).
  12961. * @param $x2 (float) Abscissa of control point 2.
  12962. * @param $y2 (float) Ordinate of control point 2.
  12963. * @param $x3 (float) Abscissa of end point.
  12964. * @param $y3 (float) Ordinate of end point.
  12965. * @protected
  12966. * @since 4.9.019 (2010-04-26)
  12967. */
  12968. protected function _outCurveV($x2, $y2, $x3, $y3) {
  12969. $this->_out(sprintf('%.2F %.2F %.2F %.2F v', $x2 * $this->k, ($this->h - $y2) * $this->k, $x3 * $this->k, ($this->h - $y3) * $this->k));
  12970. }
  12971. /**
  12972. * Append a cubic Bézier curve to the current path. The curve shall extend from the current point to the point (x3, y3), using (x1, y1) and (x3, y3) as the Bézier control points.
  12973. * The new current point shall be (x3, y3).
  12974. * @param $x1 (float) Abscissa of control point 1.
  12975. * @param $y1 (float) Ordinate of control point 1.
  12976. * @param $x3 (float) Abscissa of end point.
  12977. * @param $y3 (float) Ordinate of end point.
  12978. * @protected
  12979. * @since 2.1.000 (2008-01-08)
  12980. */
  12981. protected function _outCurveY($x1, $y1, $x3, $y3) {
  12982. $this->_out(sprintf('%.2F %.2F %.2F %.2F y', $x1 * $this->k, ($this->h - $y1) * $this->k, $x3 * $this->k, ($this->h - $y3) * $this->k));
  12983. }
  12984. /**
  12985. * Draws a line between two points.
  12986. * @param $x1 (float) Abscissa of first point.
  12987. * @param $y1 (float) Ordinate of first point.
  12988. * @param $x2 (float) Abscissa of second point.
  12989. * @param $y2 (float) Ordinate of second point.
  12990. * @param $style (array) Line style. Array like for SetLineStyle(). Default value: default line style (empty array).
  12991. * @public
  12992. * @since 1.0
  12993. * @see SetLineWidth(), SetDrawColor(), SetLineStyle()
  12994. */
  12995. public function Line($x1, $y1, $x2, $y2, $style=array()) {
  12996. if (is_array($style)) {
  12997. $this->SetLineStyle($style);
  12998. }
  12999. $this->_outPoint($x1, $y1);
  13000. $this->_outLine($x2, $y2);
  13001. $this->_out('S');
  13002. }
  13003. /**
  13004. * Draws a rectangle.
  13005. * @param $x (float) Abscissa of upper-left corner.
  13006. * @param $y (float) Ordinate of upper-left corner.
  13007. * @param $w (float) Width.
  13008. * @param $h (float) Height.
  13009. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  13010. * @param $border_style (array) Border style of rectangle. Array with keys among the following:
  13011. * <ul>
  13012. * <li>all: Line style of all borders. Array like for SetLineStyle().</li>
  13013. * <li>L, T, R, B or combinations: Line style of left, top, right or bottom border. Array like for SetLineStyle().</li>
  13014. * </ul>
  13015. * If a key is not present or is null, not draws the border. Default value: default line style (empty array).
  13016. * @param $border_style (array) Border style of rectangle. Array like for SetLineStyle(). Default value: default line style (empty array).
  13017. * @param $fill_color (array) Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K). Default value: default color (empty array).
  13018. * @public
  13019. * @since 1.0
  13020. * @see SetLineStyle()
  13021. */
  13022. public function Rect($x, $y, $w, $h, $style='', $border_style=array(), $fill_color=array()) {
  13023. if (!(false === strpos($style, 'F')) AND !empty($fill_color)) {
  13024. $this->SetFillColorArray($fill_color);
  13025. }
  13026. $op = $this->getPathPaintOperator($style);
  13027. if ((!$border_style) OR (isset($border_style['all']))) {
  13028. if (isset($border_style['all']) AND $border_style['all']) {
  13029. $this->SetLineStyle($border_style['all']);
  13030. $border_style = array();
  13031. }
  13032. }
  13033. $this->_outRect($x, $y, $w, $h, $op);
  13034. if ($border_style) {
  13035. $border_style2 = array();
  13036. foreach ($border_style as $line => $value) {
  13037. $length = strlen($line);
  13038. for ($i = 0; $i < $length; ++$i) {
  13039. $border_style2[$line[$i]] = $value;
  13040. }
  13041. }
  13042. $border_style = $border_style2;
  13043. if (isset($border_style['L']) AND $border_style['L']) {
  13044. $this->Line($x, $y, $x, $y + $h, $border_style['L']);
  13045. }
  13046. if (isset($border_style['T']) AND $border_style['T']) {
  13047. $this->Line($x, $y, $x + $w, $y, $border_style['T']);
  13048. }
  13049. if (isset($border_style['R']) AND $border_style['R']) {
  13050. $this->Line($x + $w, $y, $x + $w, $y + $h, $border_style['R']);
  13051. }
  13052. if (isset($border_style['B']) AND $border_style['B']) {
  13053. $this->Line($x, $y + $h, $x + $w, $y + $h, $border_style['B']);
  13054. }
  13055. }
  13056. }
  13057. /**
  13058. * Draws a Bezier curve.
  13059. * The Bezier curve is a tangent to the line between the control points at
  13060. * either end of the curve.
  13061. * @param $x0 (float) Abscissa of start point.
  13062. * @param $y0 (float) Ordinate of start point.
  13063. * @param $x1 (float) Abscissa of control point 1.
  13064. * @param $y1 (float) Ordinate of control point 1.
  13065. * @param $x2 (float) Abscissa of control point 2.
  13066. * @param $y2 (float) Ordinate of control point 2.
  13067. * @param $x3 (float) Abscissa of end point.
  13068. * @param $y3 (float) Ordinate of end point.
  13069. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  13070. * @param $line_style (array) Line style of curve. Array like for SetLineStyle(). Default value: default line style (empty array).
  13071. * @param $fill_color (array) Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K). Default value: default color (empty array).
  13072. * @public
  13073. * @see SetLineStyle()
  13074. * @since 2.1.000 (2008-01-08)
  13075. */
  13076. public function Curve($x0, $y0, $x1, $y1, $x2, $y2, $x3, $y3, $style='', $line_style=array(), $fill_color=array()) {
  13077. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  13078. $this->SetFillColorArray($fill_color);
  13079. }
  13080. $op = $this->getPathPaintOperator($style);
  13081. if ($line_style) {
  13082. $this->SetLineStyle($line_style);
  13083. }
  13084. $this->_outPoint($x0, $y0);
  13085. $this->_outCurve($x1, $y1, $x2, $y2, $x3, $y3);
  13086. $this->_out($op);
  13087. }
  13088. /**
  13089. * Draws a poly-Bezier curve.
  13090. * Each Bezier curve segment is a tangent to the line between the control points at
  13091. * either end of the curve.
  13092. * @param $x0 (float) Abscissa of start point.
  13093. * @param $y0 (float) Ordinate of start point.
  13094. * @param $segments (float) An array of bezier descriptions. Format: array(x1, y1, x2, y2, x3, y3).
  13095. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  13096. * @param $line_style (array) Line style of curve. Array like for SetLineStyle(). Default value: default line style (empty array).
  13097. * @param $fill_color (array) Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K). Default value: default color (empty array).
  13098. * @public
  13099. * @see SetLineStyle()
  13100. * @since 3.0008 (2008-05-12)
  13101. */
  13102. public function Polycurve($x0, $y0, $segments, $style='', $line_style=array(), $fill_color=array()) {
  13103. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  13104. $this->SetFillColorArray($fill_color);
  13105. }
  13106. $op = $this->getPathPaintOperator($style);
  13107. if ($op == 'f') {
  13108. $line_style = array();
  13109. }
  13110. if ($line_style) {
  13111. $this->SetLineStyle($line_style);
  13112. }
  13113. $this->_outPoint($x0, $y0);
  13114. foreach ($segments as $segment) {
  13115. list($x1, $y1, $x2, $y2, $x3, $y3) = $segment;
  13116. $this->_outCurve($x1, $y1, $x2, $y2, $x3, $y3);
  13117. }
  13118. $this->_out($op);
  13119. }
  13120. /**
  13121. * Draws an ellipse.
  13122. * An ellipse is formed from n Bezier curves.
  13123. * @param $x0 (float) Abscissa of center point.
  13124. * @param $y0 (float) Ordinate of center point.
  13125. * @param $rx (float) Horizontal radius.
  13126. * @param $ry (float) Vertical radius (if ry = 0 then is a circle, see Circle()). Default value: 0.
  13127. * @param $angle: (float) Angle oriented (anti-clockwise). Default value: 0.
  13128. * @param $astart: (float) Angle start of draw line. Default value: 0.
  13129. * @param $afinish: (float) Angle finish of draw line. Default value: 360.
  13130. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  13131. * @param $line_style (array) Line style of ellipse. Array like for SetLineStyle(). Default value: default line style (empty array).
  13132. * @param $fill_color (array) Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K). Default value: default color (empty array).
  13133. * @param $nc (integer) Number of curves used to draw a 90 degrees portion of ellipse.
  13134. * @author Nicola Asuni
  13135. * @public
  13136. * @since 2.1.000 (2008-01-08)
  13137. */
  13138. public function Ellipse($x0, $y0, $rx, $ry='', $angle=0, $astart=0, $afinish=360, $style='', $line_style=array(), $fill_color=array(), $nc=2) {
  13139. if ($this->empty_string($ry) OR ($ry == 0)) {
  13140. $ry = $rx;
  13141. }
  13142. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  13143. $this->SetFillColorArray($fill_color);
  13144. }
  13145. $op = $this->getPathPaintOperator($style);
  13146. if ($op == 'f') {
  13147. $line_style = array();
  13148. }
  13149. if ($line_style) {
  13150. $this->SetLineStyle($line_style);
  13151. }
  13152. $this->_outellipticalarc($x0, $y0, $rx, $ry, $angle, $astart, $afinish, false, $nc, true, true, false);
  13153. $this->_out($op);
  13154. }
  13155. /**
  13156. * Append an elliptical arc to the current path.
  13157. * An ellipse is formed from n Bezier curves.
  13158. * @param $xc (float) Abscissa of center point.
  13159. * @param $yc (float) Ordinate of center point.
  13160. * @param $rx (float) Horizontal radius.
  13161. * @param $ry (float) Vertical radius (if ry = 0 then is a circle, see Circle()). Default value: 0.
  13162. * @param $xang: (float) Angle between the X-axis and the major axis of the ellipse. Default value: 0.
  13163. * @param $angs: (float) Angle start of draw line. Default value: 0.
  13164. * @param $angf: (float) Angle finish of draw line. Default value: 360.
  13165. * @param $pie (boolean) if true do not mark the border point (used to draw pie sectors).
  13166. * @param $nc (integer) Number of curves used to draw a 90 degrees portion of ellipse.
  13167. * @param $startpoint (boolean) if true output a starting point.
  13168. * @param $ccw (boolean) if true draws in counter-clockwise.
  13169. * @param $svg (boolean) if true the angles are in svg mode (already calculated).
  13170. * @return array bounding box coordinates (x min, y min, x max, y max)
  13171. * @author Nicola Asuni
  13172. * @protected
  13173. * @since 4.9.019 (2010-04-26)
  13174. */
  13175. protected function _outellipticalarc($xc, $yc, $rx, $ry, $xang=0, $angs=0, $angf=360, $pie=false, $nc=2, $startpoint=true, $ccw=true, $svg=false) {
  13176. $k = $this->k;
  13177. if ($nc < 2) {
  13178. $nc = 2;
  13179. }
  13180. $xmin = 2147483647;
  13181. $ymin = 2147483647;
  13182. $xmax = 0;
  13183. $ymax = 0;
  13184. if ($pie) {
  13185. // center of the arc
  13186. $this->_outPoint($xc, $yc);
  13187. }
  13188. $xang = deg2rad((float) $xang);
  13189. $angs = deg2rad((float) $angs);
  13190. $angf = deg2rad((float) $angf);
  13191. if ($svg) {
  13192. $as = $angs;
  13193. $af = $angf;
  13194. } else {
  13195. $as = atan2((sin($angs) / $ry), (cos($angs) / $rx));
  13196. $af = atan2((sin($angf) / $ry), (cos($angf) / $rx));
  13197. }
  13198. if ($as < 0) {
  13199. $as += (2 * M_PI);
  13200. }
  13201. if ($af < 0) {
  13202. $af += (2 * M_PI);
  13203. }
  13204. if ($ccw AND ($as > $af)) {
  13205. // reverse rotation
  13206. $as -= (2 * M_PI);
  13207. } elseif (!$ccw AND ($as < $af)) {
  13208. // reverse rotation
  13209. $af -= (2 * M_PI);
  13210. }
  13211. $total_angle = ($af - $as);
  13212. if ($nc < 2) {
  13213. $nc = 2;
  13214. }
  13215. // total arcs to draw
  13216. $nc *= (2 * abs($total_angle) / M_PI);
  13217. $nc = round($nc) + 1;
  13218. // angle of each arc
  13219. $arcang = ($total_angle / $nc);
  13220. // center point in PDF coordinates
  13221. $x0 = $xc;
  13222. $y0 = ($this->h - $yc);
  13223. // starting angle
  13224. $ang = $as;
  13225. $alpha = sin($arcang) * ((sqrt(4 + (3 * pow(tan(($arcang) / 2), 2))) - 1) / 3);
  13226. $cos_xang = cos($xang);
  13227. $sin_xang = sin($xang);
  13228. $cos_ang = cos($ang);
  13229. $sin_ang = sin($ang);
  13230. // first arc point
  13231. $px1 = $x0 + ($rx * $cos_xang * $cos_ang) - ($ry * $sin_xang * $sin_ang);
  13232. $py1 = $y0 + ($rx * $sin_xang * $cos_ang) + ($ry * $cos_xang * $sin_ang);
  13233. // first Bezier control point
  13234. $qx1 = ($alpha * ((-$rx * $cos_xang * $sin_ang) - ($ry * $sin_xang * $cos_ang)));
  13235. $qy1 = ($alpha * ((-$rx * $sin_xang * $sin_ang) + ($ry * $cos_xang * $cos_ang)));
  13236. if ($pie) {
  13237. // line from center to arc starting point
  13238. $this->_outLine($px1, $this->h - $py1);
  13239. } elseif ($startpoint) {
  13240. // arc starting point
  13241. $this->_outPoint($px1, $this->h - $py1);
  13242. }
  13243. // draw arcs
  13244. for ($i = 1; $i <= $nc; ++$i) {
  13245. // starting angle
  13246. $ang = $as + ($i * $arcang);
  13247. if ($i == $nc) {
  13248. $ang = $af;
  13249. }
  13250. $cos_ang = cos($ang);
  13251. $sin_ang = sin($ang);
  13252. // second arc point
  13253. $px2 = $x0 + ($rx * $cos_xang * $cos_ang) - ($ry * $sin_xang * $sin_ang);
  13254. $py2 = $y0 + ($rx * $sin_xang * $cos_ang) + ($ry * $cos_xang * $sin_ang);
  13255. // second Bezier control point
  13256. $qx2 = ($alpha * ((-$rx * $cos_xang * $sin_ang) - ($ry * $sin_xang * $cos_ang)));
  13257. $qy2 = ($alpha * ((-$rx * $sin_xang * $sin_ang) + ($ry * $cos_xang * $cos_ang)));
  13258. // draw arc
  13259. $cx1 = ($px1 + $qx1);
  13260. $cy1 = ($this->h - ($py1 + $qy1));
  13261. $cx2 = ($px2 - $qx2);
  13262. $cy2 = ($this->h - ($py2 - $qy2));
  13263. $cx3 = $px2;
  13264. $cy3 = ($this->h - $py2);
  13265. $this->_outCurve($cx1, $cy1, $cx2, $cy2, $cx3, $cy3);
  13266. // get bounding box coordinates
  13267. $xmin = min($xmin, $cx1, $cx2, $cx3);
  13268. $ymin = min($ymin, $cy1, $cy2, $cy3);
  13269. $xmax = max($xmax, $cx1, $cx2, $cx3);
  13270. $ymax = max($ymax, $cy1, $cy2, $cy3);
  13271. // move to next point
  13272. $px1 = $px2;
  13273. $py1 = $py2;
  13274. $qx1 = $qx2;
  13275. $qy1 = $qy2;
  13276. }
  13277. if ($pie) {
  13278. $this->_outLine($xc, $yc);
  13279. // get bounding box coordinates
  13280. $xmin = min($xmin, $xc);
  13281. $ymin = min($ymin, $yc);
  13282. $xmax = max($xmax, $xc);
  13283. $ymax = max($ymax, $yc);
  13284. }
  13285. return array($xmin, $ymin, $xmax, $ymax);
  13286. }
  13287. /**
  13288. * Draws a circle.
  13289. * A circle is formed from n Bezier curves.
  13290. * @param $x0 (float) Abscissa of center point.
  13291. * @param $y0 (float) Ordinate of center point.
  13292. * @param $r (float) Radius.
  13293. * @param $angstr: (float) Angle start of draw line. Default value: 0.
  13294. * @param $angend: (float) Angle finish of draw line. Default value: 360.
  13295. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  13296. * @param $line_style (array) Line style of circle. Array like for SetLineStyle(). Default value: default line style (empty array).
  13297. * @param $fill_color (array) Fill color. Format: array(red, green, blue). Default value: default color (empty array).
  13298. * @param $nc (integer) Number of curves used to draw a 90 degrees portion of circle.
  13299. * @public
  13300. * @since 2.1.000 (2008-01-08)
  13301. */
  13302. public function Circle($x0, $y0, $r, $angstr=0, $angend=360, $style='', $line_style=array(), $fill_color=array(), $nc=2) {
  13303. $this->Ellipse($x0, $y0, $r, $r, 0, $angstr, $angend, $style, $line_style, $fill_color, $nc);
  13304. }
  13305. /**
  13306. * Draws a polygonal line
  13307. * @param $p (array) Points 0 to ($np - 1). Array with values (x0, y0, x1, y1,..., x(np-1), y(np - 1))
  13308. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  13309. * @param $line_style (array) Line style of polygon. Array with keys among the following:
  13310. * <ul>
  13311. * <li>all: Line style of all lines. Array like for SetLineStyle().</li>
  13312. * <li>0 to ($np - 1): Line style of each line. Array like for SetLineStyle().</li>
  13313. * </ul>
  13314. * If a key is not present or is null, not draws the line. Default value is default line style (empty array).
  13315. * @param $fill_color (array) Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K). Default value: default color (empty array).
  13316. * @since 4.8.003 (2009-09-15)
  13317. * @public
  13318. */
  13319. public function PolyLine($p, $style='', $line_style=array(), $fill_color=array()) {
  13320. $this->Polygon($p, $style, $line_style, $fill_color, false);
  13321. }
  13322. /**
  13323. * Draws a polygon.
  13324. * @param $p (array) Points 0 to ($np - 1). Array with values (x0, y0, x1, y1,..., x(np-1), y(np - 1))
  13325. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  13326. * @param $line_style (array) Line style of polygon. Array with keys among the following:
  13327. * <ul>
  13328. * <li>all: Line style of all lines. Array like for SetLineStyle().</li>
  13329. * <li>0 to ($np - 1): Line style of each line. Array like for SetLineStyle().</li>
  13330. * </ul>
  13331. * If a key is not present or is null, not draws the line. Default value is default line style (empty array).
  13332. * @param $fill_color (array) Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K). Default value: default color (empty array).
  13333. * @param $closed (boolean) if true the polygon is closes, otherwise will remain open
  13334. * @public
  13335. * @since 2.1.000 (2008-01-08)
  13336. */
  13337. public function Polygon($p, $style='', $line_style=array(), $fill_color=array(), $closed=true) {
  13338. $nc = count($p); // number of coordinates
  13339. $np = $nc / 2; // number of points
  13340. if ($closed) {
  13341. // close polygon by adding the first 2 points at the end (one line)
  13342. for ($i = 0; $i < 4; ++$i) {
  13343. $p[$nc + $i] = $p[$i];
  13344. }
  13345. // copy style for the last added line
  13346. if (isset($line_style[0])) {
  13347. $line_style[$np] = $line_style[0];
  13348. }
  13349. $nc += 4;
  13350. }
  13351. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  13352. $this->SetFillColorArray($fill_color);
  13353. }
  13354. $op = $this->getPathPaintOperator($style);
  13355. if ($op == 'f') {
  13356. $line_style = array();
  13357. }
  13358. $draw = true;
  13359. if ($line_style) {
  13360. if (isset($line_style['all'])) {
  13361. $this->SetLineStyle($line_style['all']);
  13362. } else {
  13363. $draw = false;
  13364. if ($op == 'B') {
  13365. // draw fill
  13366. $op = 'f';
  13367. $this->_outPoint($p[0], $p[1]);
  13368. for ($i = 2; $i < $nc; $i = $i + 2) {
  13369. $this->_outLine($p[$i], $p[$i + 1]);
  13370. }
  13371. $this->_out($op);
  13372. }
  13373. // draw outline
  13374. $this->_outPoint($p[0], $p[1]);
  13375. for ($i = 2; $i < $nc; $i = $i + 2) {
  13376. $line_num = ($i / 2) - 1;
  13377. if (isset($line_style[$line_num])) {
  13378. if ($line_style[$line_num] != 0) {
  13379. if (is_array($line_style[$line_num])) {
  13380. $this->_out('S');
  13381. $this->SetLineStyle($line_style[$line_num]);
  13382. $this->_outPoint($p[$i - 2], $p[$i - 1]);
  13383. $this->_outLine($p[$i], $p[$i + 1]);
  13384. $this->_out('S');
  13385. $this->_outPoint($p[$i], $p[$i + 1]);
  13386. } else {
  13387. $this->_outLine($p[$i], $p[$i + 1]);
  13388. }
  13389. }
  13390. } else {
  13391. $this->_outLine($p[$i], $p[$i + 1]);
  13392. }
  13393. }
  13394. $this->_out($op);
  13395. }
  13396. }
  13397. if ($draw) {
  13398. $this->_outPoint($p[0], $p[1]);
  13399. for ($i = 2; $i < $nc; $i = $i + 2) {
  13400. $this->_outLine($p[$i], $p[$i + 1]);
  13401. }
  13402. $this->_out($op);
  13403. }
  13404. }
  13405. /**
  13406. * Draws a regular polygon.
  13407. * @param $x0 (float) Abscissa of center point.
  13408. * @param $y0 (float) Ordinate of center point.
  13409. * @param $r: (float) Radius of inscribed circle.
  13410. * @param $ns (integer) Number of sides.
  13411. * @param $angle (float) Angle oriented (anti-clockwise). Default value: 0.
  13412. * @param $draw_circle (boolean) Draw inscribed circle or not. Default value: false.
  13413. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  13414. * @param $line_style (array) Line style of polygon sides. Array with keys among the following:
  13415. * <ul>
  13416. * <li>all: Line style of all sides. Array like for SetLineStyle().</li>
  13417. * <li>0 to ($ns - 1): Line style of each side. Array like for SetLineStyle().</li>
  13418. * </ul>
  13419. * If a key is not present or is null, not draws the side. Default valu