Fpdf.php 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795
  1. <?php
  2. namespace Fpdi;
  3. /*******************************************************************************
  4. * FPDF *
  5. * *
  6. * Version: 1.84 *
  7. * Date: 2021-08-28 *
  8. * Author: Olivier PLATHEY *
  9. *******************************************************************************/
  10. define('FPDF_VERSION', '1.84');
  11. class Fpdf
  12. {
  13. protected $page; // current page number
  14. protected $n; // current object number
  15. protected $offsets; // array of object offsets
  16. protected $buffer; // buffer holding in-memory PDF
  17. protected $pages; // array containing pages
  18. protected $state; // current document state
  19. protected $compress; // compression flag
  20. protected $k; // scale factor (number of points in user unit)
  21. protected $DefOrientation; // default orientation
  22. protected $CurOrientation; // current orientation
  23. protected $StdPageSizes; // standard page sizes
  24. protected $DefPageSize; // default page size
  25. protected $CurPageSize; // current page size
  26. protected $CurRotation; // current page rotation
  27. protected $PageInfo; // page-related data
  28. protected $wPt, $hPt; // dimensions of current page in points
  29. protected $w, $h; // dimensions of current page in user unit
  30. protected $lMargin; // left margin
  31. protected $tMargin; // top margin
  32. protected $rMargin; // right margin
  33. protected $bMargin; // page break margin
  34. protected $cMargin; // cell margin
  35. protected $x, $y; // current position in user unit
  36. protected $lasth; // height of last printed cell
  37. protected $LineWidth; // line width in user unit
  38. protected $fontpath; // path containing fonts
  39. protected $CoreFonts; // array of core font names
  40. protected $fonts; // array of used fonts
  41. protected $FontFiles; // array of font files
  42. protected $encodings; // array of encodings
  43. protected $cmaps; // array of ToUnicode CMaps
  44. protected $FontFamily; // current font family
  45. protected $FontStyle; // current font style
  46. protected $underline; // underlining flag
  47. protected $CurrentFont; // current font info
  48. protected $FontSizePt; // current font size in points
  49. protected $FontSize; // current font size in user unit
  50. protected $DrawColor; // commands for drawing color
  51. protected $FillColor; // commands for filling color
  52. protected $TextColor; // commands for text color
  53. protected $ColorFlag; // indicates whether fill and text colors are different
  54. protected $WithAlpha; // indicates whether alpha channel is used
  55. protected $ws; // word spacing
  56. protected $images; // array of used images
  57. protected $PageLinks; // array of links in pages
  58. protected $links; // array of internal links
  59. protected $AutoPageBreak; // automatic page breaking
  60. protected $PageBreakTrigger; // threshold used to trigger page breaks
  61. protected $InHeader; // flag set when processing header
  62. protected $InFooter; // flag set when processing footer
  63. protected $AliasNbPages; // alias for total number of pages
  64. protected $ZoomMode; // zoom display mode
  65. protected $LayoutMode; // layout display mode
  66. protected $metadata; // document properties
  67. protected $PDFVersion; // PDF version number
  68. /*******************************************************************************
  69. * Public methods *
  70. *******************************************************************************/
  71. function __construct($orientation = 'P', $unit = 'mm', $size = 'A4')
  72. {
  73. // Some checks
  74. $this->_dochecks();
  75. // Initialization of properties
  76. $this->state = 0;
  77. $this->page = 0;
  78. $this->n = 2;
  79. $this->buffer = '';
  80. $this->pages = array();
  81. $this->PageInfo = array();
  82. $this->fonts = array();
  83. $this->FontFiles = array();
  84. $this->encodings = array();
  85. $this->cmaps = array();
  86. $this->images = array();
  87. $this->links = array();
  88. $this->InHeader = false;
  89. $this->InFooter = false;
  90. $this->lasth = 0;
  91. $this->FontFamily = '';
  92. $this->FontStyle = '';
  93. $this->FontSizePt = 12;
  94. $this->underline = false;
  95. $this->DrawColor = '0 G';
  96. $this->FillColor = '0 g';
  97. $this->TextColor = '0 g';
  98. $this->ColorFlag = false;
  99. $this->WithAlpha = false;
  100. $this->ws = 0;
  101. /*
  102. // Font path
  103. if (defined('FPDF_FONTPATH')) {
  104. $this->fontpath = FPDF_FONTPATH;
  105. if (substr($this->fontpath, -1) != '/' && substr($this->fontpath, -1) != '\\')
  106. $this->fontpath .= '/';
  107. } elseif (is_dir(dirname(__FILE__) . '/font'))
  108. $this->fontpath = dirname(__FILE__) . '/font/';
  109. else
  110. $this->fontpath = '';
  111. */
  112. if (is_dir(dirname(__FILE__) . '/font'))
  113. $this->fontpath = dirname(__FILE__) . '/font/';
  114. else
  115. $this->fontpath = '';
  116. // Core fonts
  117. $this->CoreFonts = array('courier', 'helvetica', 'times', 'symbol', 'zapfdingbats');
  118. // Scale factor
  119. if ($unit == 'pt')
  120. $this->k = 1;
  121. elseif ($unit == 'mm')
  122. $this->k = 72 / 25.4;
  123. elseif ($unit == 'cm')
  124. $this->k = 72 / 2.54;
  125. elseif ($unit == 'in')
  126. $this->k = 72;
  127. else
  128. $this->Error('Incorrect unit: ' . $unit);
  129. // Page sizes
  130. $this->StdPageSizes = array(
  131. 'a3' => array(841.89, 1190.55), 'a4' => array(595.28, 841.89), 'a5' => array(420.94, 595.28),
  132. 'letter' => array(612, 792), 'legal' => array(612, 1008)
  133. );
  134. $size = $this->_getpagesize($size);
  135. $this->DefPageSize = $size;
  136. $this->CurPageSize = $size;
  137. // Page orientation
  138. $orientation = strtolower($orientation);
  139. if ($orientation == 'p' || $orientation == 'portrait') {
  140. $this->DefOrientation = 'P';
  141. $this->w = $size[0];
  142. $this->h = $size[1];
  143. } elseif ($orientation == 'l' || $orientation == 'landscape') {
  144. $this->DefOrientation = 'L';
  145. $this->w = $size[1];
  146. $this->h = $size[0];
  147. } else
  148. $this->Error('Incorrect orientation: ' . $orientation);
  149. $this->CurOrientation = $this->DefOrientation;
  150. $this->wPt = $this->w * $this->k;
  151. $this->hPt = $this->h * $this->k;
  152. // Page rotation
  153. $this->CurRotation = 0;
  154. // Page margins (1 cm)
  155. $margin = 28.35 / $this->k;
  156. $this->SetMargins($margin, $margin);
  157. // Interior cell margin (1 mm)
  158. $this->cMargin = $margin / 10;
  159. // Line width (0.2 mm)
  160. $this->LineWidth = .567 / $this->k;
  161. // Automatic page break
  162. $this->SetAutoPageBreak(true, 2 * $margin);
  163. // Default display mode
  164. $this->SetDisplayMode('default');
  165. // Enable compression
  166. $this->SetCompression(true);
  167. // Set default PDF version number
  168. $this->PDFVersion = '1.3';
  169. }
  170. function SetMargins($left, $top, $right = null)
  171. {
  172. // Set left, top and right margins
  173. $this->lMargin = $left;
  174. $this->tMargin = $top;
  175. if ($right === null)
  176. $right = $left;
  177. $this->rMargin = $right;
  178. }
  179. function SetLeftMargin($margin)
  180. {
  181. // Set left margin
  182. $this->lMargin = $margin;
  183. if ($this->page > 0 && $this->x < $margin)
  184. $this->x = $margin;
  185. }
  186. function SetTopMargin($margin)
  187. {
  188. // Set top margin
  189. $this->tMargin = $margin;
  190. }
  191. function SetRightMargin($margin)
  192. {
  193. // Set right margin
  194. $this->rMargin = $margin;
  195. }
  196. function SetAutoPageBreak($auto, $margin = 0)
  197. {
  198. // Set auto page break mode and triggering margin
  199. $this->AutoPageBreak = $auto;
  200. $this->bMargin = $margin;
  201. $this->PageBreakTrigger = $this->h - $margin;
  202. }
  203. function SetDisplayMode($zoom, $layout = 'default')
  204. {
  205. // Set display mode in viewer
  206. if ($zoom == 'fullpage' || $zoom == 'fullwidth' || $zoom == 'real' || $zoom == 'default' || !is_string($zoom))
  207. $this->ZoomMode = $zoom;
  208. else
  209. $this->Error('Incorrect zoom display mode: ' . $zoom);
  210. if ($layout == 'single' || $layout == 'continuous' || $layout == 'two' || $layout == 'default')
  211. $this->LayoutMode = $layout;
  212. else
  213. $this->Error('Incorrect layout display mode: ' . $layout);
  214. }
  215. function SetCompression($compress)
  216. {
  217. // Set page compression
  218. if (function_exists('gzcompress'))
  219. $this->compress = $compress;
  220. else
  221. $this->compress = false;
  222. }
  223. function SetTitle($title, $isUTF8 = false)
  224. {
  225. // Title of document
  226. $this->metadata['Title'] = $isUTF8 ? $title : utf8_encode($title);
  227. }
  228. function SetAuthor($author, $isUTF8 = false)
  229. {
  230. // Author of document
  231. $this->metadata['Author'] = $isUTF8 ? $author : utf8_encode($author);
  232. }
  233. function SetSubject($subject, $isUTF8 = false)
  234. {
  235. // Subject of document
  236. $this->metadata['Subject'] = $isUTF8 ? $subject : utf8_encode($subject);
  237. }
  238. function SetKeywords($keywords, $isUTF8 = false)
  239. {
  240. // Keywords of document
  241. $this->metadata['Keywords'] = $isUTF8 ? $keywords : utf8_encode($keywords);
  242. }
  243. function SetCreator($creator, $isUTF8 = false)
  244. {
  245. // Creator of document
  246. $this->metadata['Creator'] = $isUTF8 ? $creator : utf8_encode($creator);
  247. }
  248. function AliasNbPages($alias = '{nb}')
  249. {
  250. // Define an alias for total number of pages
  251. $this->AliasNbPages = $alias;
  252. }
  253. function Error($msg)
  254. {
  255. // Fatal error
  256. throw new \Exception('FPDF error: ' . $msg);
  257. }
  258. function Close()
  259. {
  260. // Terminate document
  261. if ($this->state == 3)
  262. return;
  263. if ($this->page == 0)
  264. $this->AddPage();
  265. // Page footer
  266. $this->InFooter = true;
  267. $this->Footer();
  268. $this->InFooter = false;
  269. // Close page
  270. $this->_endpage();
  271. // Close document
  272. $this->_enddoc();
  273. }
  274. function AddPage($orientation = '', $size = '', $rotation = 0)
  275. {
  276. // Start a new page
  277. if ($this->state == 3)
  278. $this->Error('The document is closed');
  279. $family = $this->FontFamily;
  280. $style = $this->FontStyle . ($this->underline ? 'U' : '');
  281. $fontsize = $this->FontSizePt;
  282. $lw = $this->LineWidth;
  283. $dc = $this->DrawColor;
  284. $fc = $this->FillColor;
  285. $tc = $this->TextColor;
  286. $cf = $this->ColorFlag;
  287. if ($this->page > 0) {
  288. // Page footer
  289. $this->InFooter = true;
  290. $this->Footer();
  291. $this->InFooter = false;
  292. // Close page
  293. $this->_endpage();
  294. }
  295. // Start new page
  296. $this->_beginpage($orientation, $size, $rotation);
  297. // Set line cap style to square
  298. $this->_out('2 J');
  299. // Set line width
  300. $this->LineWidth = $lw;
  301. $this->_out(sprintf('%.2F w', $lw * $this->k));
  302. // Set font
  303. if ($family)
  304. $this->SetFont($family, $style, $fontsize);
  305. // Set colors
  306. $this->DrawColor = $dc;
  307. if ($dc != '0 G')
  308. $this->_out($dc);
  309. $this->FillColor = $fc;
  310. if ($fc != '0 g')
  311. $this->_out($fc);
  312. $this->TextColor = $tc;
  313. $this->ColorFlag = $cf;
  314. // Page header
  315. $this->InHeader = true;
  316. $this->Header();
  317. $this->InHeader = false;
  318. // Restore line width
  319. if ($this->LineWidth != $lw) {
  320. $this->LineWidth = $lw;
  321. $this->_out(sprintf('%.2F w', $lw * $this->k));
  322. }
  323. // Restore font
  324. if ($family)
  325. $this->SetFont($family, $style, $fontsize);
  326. // Restore colors
  327. if ($this->DrawColor != $dc) {
  328. $this->DrawColor = $dc;
  329. $this->_out($dc);
  330. }
  331. if ($this->FillColor != $fc) {
  332. $this->FillColor = $fc;
  333. $this->_out($fc);
  334. }
  335. $this->TextColor = $tc;
  336. $this->ColorFlag = $cf;
  337. }
  338. function Header()
  339. {
  340. // To be implemented in your own inherited class
  341. }
  342. function Footer()
  343. {
  344. // To be implemented in your own inherited class
  345. }
  346. function PageNo()
  347. {
  348. // Get current page number
  349. return $this->page;
  350. }
  351. function SetDrawColor($r, $g = null, $b = null)
  352. {
  353. // Set color for all stroking operations
  354. if (($r == 0 && $g == 0 && $b == 0) || $g === null)
  355. $this->DrawColor = sprintf('%.3F G', $r / 255);
  356. else
  357. $this->DrawColor = sprintf('%.3F %.3F %.3F RG', $r / 255, $g / 255, $b / 255);
  358. if ($this->page > 0)
  359. $this->_out($this->DrawColor);
  360. }
  361. function SetFillColor($r, $g = null, $b = null)
  362. {
  363. // Set color for all filling operations
  364. if (($r == 0 && $g == 0 && $b == 0) || $g === null)
  365. $this->FillColor = sprintf('%.3F g', $r / 255);
  366. else
  367. $this->FillColor = sprintf('%.3F %.3F %.3F rg', $r / 255, $g / 255, $b / 255);
  368. $this->ColorFlag = ($this->FillColor != $this->TextColor);
  369. if ($this->page > 0)
  370. $this->_out($this->FillColor);
  371. }
  372. function SetTextColor($r, $g = null, $b = null)
  373. {
  374. // Set color for text
  375. if (($r == 0 && $g == 0 && $b == 0) || $g === null)
  376. $this->TextColor = sprintf('%.3F g', $r / 255);
  377. else
  378. $this->TextColor = sprintf('%.3F %.3F %.3F rg', $r / 255, $g / 255, $b / 255);
  379. $this->ColorFlag = ($this->FillColor != $this->TextColor);
  380. }
  381. function GetStringWidth($s)
  382. {
  383. // Get width of a string in the current font
  384. $s = (string)$s;
  385. $cw = &$this->CurrentFont['cw'];
  386. $w = 0;
  387. $l = strlen($s);
  388. for ($i = 0; $i < $l; $i++)
  389. $w += $cw[$s[$i]];
  390. return $w * $this->FontSize / 1000;
  391. }
  392. function SetLineWidth($width)
  393. {
  394. // Set line width
  395. $this->LineWidth = $width;
  396. if ($this->page > 0)
  397. $this->_out(sprintf('%.2F w', $width * $this->k));
  398. }
  399. function Line($x1, $y1, $x2, $y2)
  400. {
  401. // Draw a line
  402. $this->_out(sprintf('%.2F %.2F m %.2F %.2F l S', $x1 * $this->k, ($this->h - $y1) * $this->k, $x2 * $this->k, ($this->h - $y2) * $this->k));
  403. }
  404. function Rect($x, $y, $w, $h, $style = '')
  405. {
  406. // Draw a rectangle
  407. if ($style == 'F')
  408. $op = 'f';
  409. elseif ($style == 'FD' || $style == 'DF')
  410. $op = 'B';
  411. else
  412. $op = 'S';
  413. $this->_out(sprintf('%.2F %.2F %.2F %.2F re %s', $x * $this->k, ($this->h - $y) * $this->k, $w * $this->k, -$h * $this->k, $op));
  414. }
  415. function AddFont($family, $style = '', $file = '')
  416. {
  417. // Add a TrueType, OpenType or Type1 font
  418. $family = strtolower($family);
  419. if ($file == '')
  420. $file = str_replace(' ', '', $family) . strtolower($style) . '.php';
  421. $style = strtoupper($style);
  422. if ($style == 'IB')
  423. $style = 'BI';
  424. $fontkey = $family . $style;
  425. if (isset($this->fonts[$fontkey]))
  426. return;
  427. $info = $this->_loadfont($file);
  428. $info['i'] = count($this->fonts) + 1;
  429. if (!empty($info['file'])) {
  430. // Embedded font
  431. if ($info['type'] == 'TrueType')
  432. $this->FontFiles[$info['file']] = array('length1' => $info['originalsize']);
  433. else
  434. $this->FontFiles[$info['file']] = array('length1' => $info['size1'], 'length2' => $info['size2']);
  435. }
  436. $this->fonts[$fontkey] = $info;
  437. }
  438. function SetFont($family, $style = '', $size = 0)
  439. {
  440. // Select a font; size given in points
  441. if ($family == '')
  442. $family = $this->FontFamily;
  443. else
  444. $family = strtolower($family);
  445. $style = strtoupper($style);
  446. if (strpos($style, 'U') !== false) {
  447. $this->underline = true;
  448. $style = str_replace('U', '', $style);
  449. } else
  450. $this->underline = false;
  451. if ($style == 'IB')
  452. $style = 'BI';
  453. if ($size == 0)
  454. $size = $this->FontSizePt;
  455. // Test if font is already selected
  456. if ($this->FontFamily == $family && $this->FontStyle == $style && $this->FontSizePt == $size)
  457. return;
  458. // Test if font is already loaded
  459. $fontkey = $family . $style;
  460. if (!isset($this->fonts[$fontkey])) {
  461. // Test if one of the core fonts
  462. if ($family == 'arial')
  463. $family = 'helvetica';
  464. if (in_array($family, $this->CoreFonts)) {
  465. if ($family == 'symbol' || $family == 'zapfdingbats')
  466. $style = '';
  467. $fontkey = $family . $style;
  468. if (!isset($this->fonts[$fontkey]))
  469. $this->AddFont($family, $style);
  470. } else
  471. $this->Error('Undefined font: ' . $family . ' ' . $style);
  472. }
  473. // Select it
  474. $this->FontFamily = $family;
  475. $this->FontStyle = $style;
  476. $this->FontSizePt = $size;
  477. $this->FontSize = $size / $this->k;
  478. $this->CurrentFont = &$this->fonts[$fontkey];
  479. if ($this->page > 0)
  480. $this->_out(sprintf('BT /F%d %.2F Tf ET', $this->CurrentFont['i'], $this->FontSizePt));
  481. }
  482. function SetFontSize($size)
  483. {
  484. // Set font size in points
  485. if ($this->FontSizePt == $size)
  486. return;
  487. $this->FontSizePt = $size;
  488. $this->FontSize = $size / $this->k;
  489. if ($this->page > 0)
  490. $this->_out(sprintf('BT /F%d %.2F Tf ET', $this->CurrentFont['i'], $this->FontSizePt));
  491. }
  492. function AddLink()
  493. {
  494. // Create a new internal link
  495. $n = count($this->links) + 1;
  496. $this->links[$n] = array(0, 0);
  497. return $n;
  498. }
  499. function SetLink($link, $y = 0, $page = -1)
  500. {
  501. // Set destination of internal link
  502. if ($y == -1)
  503. $y = $this->y;
  504. if ($page == -1)
  505. $page = $this->page;
  506. $this->links[$link] = array($page, $y);
  507. }
  508. function Link($x, $y, $w, $h, $link)
  509. {
  510. // Put a link on the page
  511. $this->PageLinks[$this->page][] = array($x * $this->k, $this->hPt - $y * $this->k, $w * $this->k, $h * $this->k, $link);
  512. }
  513. function Text($x, $y, $txt)
  514. {
  515. // Output a string
  516. if (!isset($this->CurrentFont))
  517. $this->Error('No font has been set');
  518. $s = sprintf('BT %.2F %.2F Td (%s) Tj ET', $x * $this->k, ($this->h - $y) * $this->k, $this->_escape($txt));
  519. if ($this->underline && $txt != '')
  520. $s .= ' ' . $this->_dounderline($x, $y, $txt);
  521. if ($this->ColorFlag)
  522. $s = 'q ' . $this->TextColor . ' ' . $s . ' Q';
  523. $this->_out($s);
  524. }
  525. function AcceptPageBreak()
  526. {
  527. // Accept automatic page break or not
  528. return $this->AutoPageBreak;
  529. }
  530. function Cell($w, $h = 0, $txt = '', $border = 0, $ln = 0, $align = '', $fill = false, $link = '')
  531. {
  532. // Output a cell
  533. $k = $this->k;
  534. if ($this->y + $h > $this->PageBreakTrigger && !$this->InHeader && !$this->InFooter && $this->AcceptPageBreak()) {
  535. // Automatic page break
  536. $x = $this->x;
  537. $ws = $this->ws;
  538. if ($ws > 0) {
  539. $this->ws = 0;
  540. $this->_out('0 Tw');
  541. }
  542. $this->AddPage($this->CurOrientation, $this->CurPageSize, $this->CurRotation);
  543. $this->x = $x;
  544. if ($ws > 0) {
  545. $this->ws = $ws;
  546. $this->_out(sprintf('%.3F Tw', $ws * $k));
  547. }
  548. }
  549. if ($w == 0)
  550. $w = $this->w - $this->rMargin - $this->x;
  551. $s = '';
  552. if ($fill || $border == 1) {
  553. if ($fill)
  554. $op = ($border == 1) ? 'B' : 'f';
  555. else
  556. $op = 'S';
  557. $s = sprintf('%.2F %.2F %.2F %.2F re %s ', $this->x * $k, ($this->h - $this->y) * $k, $w * $k, -$h * $k, $op);
  558. }
  559. if (is_string($border)) {
  560. $x = $this->x;
  561. $y = $this->y;
  562. if (strpos($border, 'L') !== false)
  563. $s .= sprintf('%.2F %.2F m %.2F %.2F l S ', $x * $k, ($this->h - $y) * $k, $x * $k, ($this->h - ($y + $h)) * $k);
  564. if (strpos($border, 'T') !== false)
  565. $s .= sprintf('%.2F %.2F m %.2F %.2F l S ', $x * $k, ($this->h - $y) * $k, ($x + $w) * $k, ($this->h - $y) * $k);
  566. if (strpos($border, 'R') !== false)
  567. $s .= sprintf('%.2F %.2F m %.2F %.2F l S ', ($x + $w) * $k, ($this->h - $y) * $k, ($x + $w) * $k, ($this->h - ($y + $h)) * $k);
  568. if (strpos($border, 'B') !== false)
  569. $s .= sprintf('%.2F %.2F m %.2F %.2F l S ', $x * $k, ($this->h - ($y + $h)) * $k, ($x + $w) * $k, ($this->h - ($y + $h)) * $k);
  570. }
  571. if ($txt !== '') {
  572. if (!isset($this->CurrentFont))
  573. $this->Error('No font has been set');
  574. if ($align == 'R')
  575. $dx = $w - $this->cMargin - $this->GetStringWidth($txt);
  576. elseif ($align == 'C')
  577. $dx = ($w - $this->GetStringWidth($txt)) / 2;
  578. else
  579. $dx = $this->cMargin;
  580. if ($this->ColorFlag)
  581. $s .= 'q ' . $this->TextColor . ' ';
  582. $s .= sprintf('BT %.2F %.2F Td (%s) Tj ET', ($this->x + $dx) * $k, ($this->h - ($this->y + .5 * $h + .3 * $this->FontSize)) * $k, $this->_escape($txt));
  583. if ($this->underline)
  584. $s .= ' ' . $this->_dounderline($this->x + $dx, $this->y + .5 * $h + .3 * $this->FontSize, $txt);
  585. if ($this->ColorFlag)
  586. $s .= ' Q';
  587. if ($link)
  588. $this->Link($this->x + $dx, $this->y + .5 * $h - .5 * $this->FontSize, $this->GetStringWidth($txt), $this->FontSize, $link);
  589. }
  590. if ($s)
  591. $this->_out($s);
  592. $this->lasth = $h;
  593. if ($ln > 0) {
  594. // Go to next line
  595. $this->y += $h;
  596. if ($ln == 1)
  597. $this->x = $this->lMargin;
  598. } else
  599. $this->x += $w;
  600. }
  601. function MultiCell($w, $h, $txt, $border = 0, $align = 'J', $fill = false)
  602. {
  603. // Output text with automatic or explicit line breaks
  604. if (!isset($this->CurrentFont))
  605. $this->Error('No font has been set');
  606. $cw = &$this->CurrentFont['cw'];
  607. if ($w == 0)
  608. $w = $this->w - $this->rMargin - $this->x;
  609. $wmax = ($w - 2 * $this->cMargin) * 1000 / $this->FontSize;
  610. $s = str_replace("\r", '', $txt);
  611. $nb = strlen($s);
  612. if ($nb > 0 && $s[$nb - 1] == "\n")
  613. $nb--;
  614. $b = 0;
  615. if ($border) {
  616. if ($border == 1) {
  617. $border = 'LTRB';
  618. $b = 'LRT';
  619. $b2 = 'LR';
  620. } else {
  621. $b2 = '';
  622. if (strpos($border, 'L') !== false)
  623. $b2 .= 'L';
  624. if (strpos($border, 'R') !== false)
  625. $b2 .= 'R';
  626. $b = (strpos($border, 'T') !== false) ? $b2 . 'T' : $b2;
  627. }
  628. }
  629. $sep = -1;
  630. $i = 0;
  631. $j = 0;
  632. $l = 0;
  633. $ns = 0;
  634. $nl = 1;
  635. while ($i < $nb) {
  636. // Get next character
  637. $c = $s[$i];
  638. if ($c == "\n") {
  639. // Explicit line break
  640. if ($this->ws > 0) {
  641. $this->ws = 0;
  642. $this->_out('0 Tw');
  643. }
  644. $this->Cell($w, $h, substr($s, $j, $i - $j), $b, 2, $align, $fill);
  645. $i++;
  646. $sep = -1;
  647. $j = $i;
  648. $l = 0;
  649. $ns = 0;
  650. $nl++;
  651. if ($border && $nl == 2)
  652. $b = $b2;
  653. continue;
  654. }
  655. if ($c == ' ') {
  656. $sep = $i;
  657. $ls = $l;
  658. $ns++;
  659. }
  660. $l += $cw[$c];
  661. if ($l > $wmax) {
  662. // Automatic line break
  663. if ($sep == -1) {
  664. if ($i == $j)
  665. $i++;
  666. if ($this->ws > 0) {
  667. $this->ws = 0;
  668. $this->_out('0 Tw');
  669. }
  670. $this->Cell($w, $h, substr($s, $j, $i - $j), $b, 2, $align, $fill);
  671. } else {
  672. if ($align == 'J') {
  673. $this->ws = ($ns > 1) ? ($wmax - $ls) / 1000 * $this->FontSize / ($ns - 1) : 0;
  674. $this->_out(sprintf('%.3F Tw', $this->ws * $this->k));
  675. }
  676. $this->Cell($w, $h, substr($s, $j, $sep - $j), $b, 2, $align, $fill);
  677. $i = $sep + 1;
  678. }
  679. $sep = -1;
  680. $j = $i;
  681. $l = 0;
  682. $ns = 0;
  683. $nl++;
  684. if ($border && $nl == 2)
  685. $b = $b2;
  686. } else
  687. $i++;
  688. }
  689. // Last chunk
  690. if ($this->ws > 0) {
  691. $this->ws = 0;
  692. $this->_out('0 Tw');
  693. }
  694. if ($border && strpos($border, 'B') !== false)
  695. $b .= 'B';
  696. $this->Cell($w, $h, substr($s, $j, $i - $j), $b, 2, $align, $fill);
  697. $this->x = $this->lMargin;
  698. }
  699. function Write($h, $txt, $link = '')
  700. {
  701. // Output text in flowing mode
  702. if (!isset($this->CurrentFont))
  703. $this->Error('No font has been set');
  704. $cw = &$this->CurrentFont['cw'];
  705. $w = $this->w - $this->rMargin - $this->x;
  706. $wmax = ($w - 2 * $this->cMargin) * 1000 / $this->FontSize;
  707. $s = str_replace("\r", '', $txt);
  708. $nb = strlen($s);
  709. $sep = -1;
  710. $i = 0;
  711. $j = 0;
  712. $l = 0;
  713. $nl = 1;
  714. while ($i < $nb) {
  715. // Get next character
  716. $c = $s[$i];
  717. if ($c == "\n") {
  718. // Explicit line break
  719. $this->Cell($w, $h, substr($s, $j, $i - $j), 0, 2, '', false, $link);
  720. $i++;
  721. $sep = -1;
  722. $j = $i;
  723. $l = 0;
  724. if ($nl == 1) {
  725. $this->x = $this->lMargin;
  726. $w = $this->w - $this->rMargin - $this->x;
  727. $wmax = ($w - 2 * $this->cMargin) * 1000 / $this->FontSize;
  728. }
  729. $nl++;
  730. continue;
  731. }
  732. if ($c == ' ')
  733. $sep = $i;
  734. $l += $cw[$c];
  735. if ($l > $wmax) {
  736. // Automatic line break
  737. if ($sep == -1) {
  738. if ($this->x > $this->lMargin) {
  739. // Move to next line
  740. $this->x = $this->lMargin;
  741. $this->y += $h;
  742. $w = $this->w - $this->rMargin - $this->x;
  743. $wmax = ($w - 2 * $this->cMargin) * 1000 / $this->FontSize;
  744. $i++;
  745. $nl++;
  746. continue;
  747. }
  748. if ($i == $j)
  749. $i++;
  750. $this->Cell($w, $h, substr($s, $j, $i - $j), 0, 2, '', false, $link);
  751. } else {
  752. $this->Cell($w, $h, substr($s, $j, $sep - $j), 0, 2, '', false, $link);
  753. $i = $sep + 1;
  754. }
  755. $sep = -1;
  756. $j = $i;
  757. $l = 0;
  758. if ($nl == 1) {
  759. $this->x = $this->lMargin;
  760. $w = $this->w - $this->rMargin - $this->x;
  761. $wmax = ($w - 2 * $this->cMargin) * 1000 / $this->FontSize;
  762. }
  763. $nl++;
  764. } else
  765. $i++;
  766. }
  767. // Last chunk
  768. if ($i != $j)
  769. $this->Cell($l / 1000 * $this->FontSize, $h, substr($s, $j), 0, 0, '', false, $link);
  770. }
  771. function Ln($h = null)
  772. {
  773. // Line feed; default value is the last cell height
  774. $this->x = $this->lMargin;
  775. if ($h === null)
  776. $this->y += $this->lasth;
  777. else
  778. $this->y += $h;
  779. }
  780. function Image($file, $x = null, $y = null, $w = 0, $h = 0, $type = '', $link = '')
  781. {
  782. // Put an image on the page
  783. if ($file == '')
  784. $this->Error('Image file name is empty');
  785. if (!isset($this->images[$file])) {
  786. // First use of this image, get info
  787. if ($type == '') {
  788. $pos = strrpos($file, '.');
  789. if (!$pos)
  790. $this->Error('Image file has no extension and no type was specified: ' . $file);
  791. $type = substr($file, $pos + 1);
  792. }
  793. $type = strtolower($type);
  794. if ($type == 'jpeg')
  795. $type = 'jpg';
  796. $mtd = '_parse' . $type;
  797. if (!method_exists($this, $mtd))
  798. $this->Error('Unsupported image type: ' . $type);
  799. $info = $this->$mtd($file);
  800. $info['i'] = count($this->images) + 1;
  801. $this->images[$file] = $info;
  802. } else
  803. $info = $this->images[$file];
  804. // Automatic width and height calculation if needed
  805. if ($w == 0 && $h == 0) {
  806. // Put image at 96 dpi
  807. $w = -96;
  808. $h = -96;
  809. }
  810. if ($w < 0)
  811. $w = -$info['w'] * 72 / $w / $this->k;
  812. if ($h < 0)
  813. $h = -$info['h'] * 72 / $h / $this->k;
  814. if ($w == 0)
  815. $w = $h * $info['w'] / $info['h'];
  816. if ($h == 0)
  817. $h = $w * $info['h'] / $info['w'];
  818. // Flowing mode
  819. if ($y === null) {
  820. if ($this->y + $h > $this->PageBreakTrigger && !$this->InHeader && !$this->InFooter && $this->AcceptPageBreak()) {
  821. // Automatic page break
  822. $x2 = $this->x;
  823. $this->AddPage($this->CurOrientation, $this->CurPageSize, $this->CurRotation);
  824. $this->x = $x2;
  825. }
  826. $y = $this->y;
  827. $this->y += $h;
  828. }
  829. if ($x === null)
  830. $x = $this->x;
  831. $this->_out(sprintf('q %.2F 0 0 %.2F %.2F %.2F cm /I%d Do Q', $w * $this->k, $h * $this->k, $x * $this->k, ($this->h - ($y + $h)) * $this->k, $info['i']));
  832. if ($link)
  833. $this->Link($x, $y, $w, $h, $link);
  834. }
  835. function GetPageWidth()
  836. {
  837. // Get current page width
  838. return $this->w;
  839. }
  840. function GetPageHeight()
  841. {
  842. // Get current page height
  843. return $this->h;
  844. }
  845. function GetX()
  846. {
  847. // Get x position
  848. return $this->x;
  849. }
  850. function SetX($x)
  851. {
  852. // Set x position
  853. if ($x >= 0)
  854. $this->x = $x;
  855. else
  856. $this->x = $this->w + $x;
  857. }
  858. function GetY()
  859. {
  860. // Get y position
  861. return $this->y;
  862. }
  863. function SetY($y, $resetX = true)
  864. {
  865. // Set y position and optionally reset x
  866. if ($y >= 0)
  867. $this->y = $y;
  868. else
  869. $this->y = $this->h + $y;
  870. if ($resetX)
  871. $this->x = $this->lMargin;
  872. }
  873. function SetXY($x, $y)
  874. {
  875. // Set x and y positions
  876. $this->SetX($x);
  877. $this->SetY($y, false);
  878. }
  879. function Output($dest = '', $name = '', $isUTF8 = false)
  880. {
  881. // Output PDF to some destination
  882. $this->Close();
  883. if (strlen($name) == 1 && strlen($dest) != 1) {
  884. // Fix parameter order
  885. $tmp = $dest;
  886. $dest = $name;
  887. $name = $tmp;
  888. }
  889. if ($dest == '')
  890. $dest = 'I';
  891. if ($name == '')
  892. $name = 'doc.pdf';
  893. switch (strtoupper($dest)) {
  894. case 'I':
  895. // Send to standard output
  896. $this->_checkoutput();
  897. if (PHP_SAPI != 'cli') {
  898. // We send to a browser
  899. header('Content-Type: application/pdf');
  900. header('Content-Disposition: inline; ' . $this->_httpencode('filename', $name, $isUTF8));
  901. header('Cache-Control: private, max-age=0, must-revalidate');
  902. header('Pragma: public');
  903. }
  904. echo $this->buffer;
  905. break;
  906. case 'D':
  907. // Download file
  908. $this->_checkoutput();
  909. header('Content-Type: application/x-download');
  910. header('Content-Disposition: attachment; ' . $this->_httpencode('filename', $name, $isUTF8));
  911. header('Cache-Control: private, max-age=0, must-revalidate');
  912. header('Pragma: public');
  913. echo $this->buffer;
  914. break;
  915. case 'F':
  916. // Save to local file
  917. if (!file_put_contents($name, $this->buffer))
  918. $this->Error('Unable to create output file: ' . $name);
  919. break;
  920. case 'S':
  921. // Return as a string
  922. return $this->buffer;
  923. default:
  924. $this->Error('Incorrect output destination: ' . $dest);
  925. }
  926. return '';
  927. }
  928. /*******************************************************************************
  929. * Protected methods *
  930. *******************************************************************************/
  931. protected function _dochecks()
  932. {
  933. // Check mbstring overloading
  934. if (ini_get('mbstring.func_overload') & 2)
  935. $this->Error('mbstring overloading must be disabled');
  936. }
  937. protected function _checkoutput()
  938. {
  939. if (PHP_SAPI != 'cli') {
  940. if (headers_sent($file, $line))
  941. $this->Error("Some data has already been output, can't send PDF file (output started at $file:$line)");
  942. }
  943. if (ob_get_length()) {
  944. // The output buffer is not empty
  945. if (preg_match('/^(\xEF\xBB\xBF)?\s*$/', ob_get_contents())) {
  946. // It contains only a UTF-8 BOM and/or whitespace, let's clean it
  947. ob_clean();
  948. } else
  949. $this->Error("Some data has already been output, can't send PDF file");
  950. }
  951. }
  952. protected function _getpagesize($size)
  953. {
  954. if (is_string($size)) {
  955. $size = strtolower($size);
  956. if (!isset($this->StdPageSizes[$size]))
  957. $this->Error('Unknown page size: ' . $size);
  958. $a = $this->StdPageSizes[$size];
  959. return array($a[0] / $this->k, $a[1] / $this->k);
  960. } else {
  961. if ($size[0] > $size[1])
  962. return array($size[1], $size[0]);
  963. else
  964. return $size;
  965. }
  966. }
  967. protected function _beginpage($orientation, $size, $rotation)
  968. {
  969. $this->page++;
  970. $this->pages[$this->page] = '';
  971. $this->PageLinks[$this->page] = array();
  972. $this->state = 2;
  973. $this->x = $this->lMargin;
  974. $this->y = $this->tMargin;
  975. $this->FontFamily = '';
  976. // Check page size and orientation
  977. if ($orientation == '')
  978. $orientation = $this->DefOrientation;
  979. else
  980. $orientation = strtoupper($orientation[0]);
  981. if ($size == '')
  982. $size = $this->DefPageSize;
  983. else
  984. $size = $this->_getpagesize($size);
  985. if ($orientation != $this->CurOrientation || $size[0] != $this->CurPageSize[0] || $size[1] != $this->CurPageSize[1]) {
  986. // New size or orientation
  987. if ($orientation == 'P') {
  988. $this->w = $size[0];
  989. $this->h = $size[1];
  990. } else {
  991. $this->w = $size[1];
  992. $this->h = $size[0];
  993. }
  994. $this->wPt = $this->w * $this->k;
  995. $this->hPt = $this->h * $this->k;
  996. $this->PageBreakTrigger = $this->h - $this->bMargin;
  997. $this->CurOrientation = $orientation;
  998. $this->CurPageSize = $size;
  999. }
  1000. if ($orientation != $this->DefOrientation || $size[0] != $this->DefPageSize[0] || $size[1] != $this->DefPageSize[1])
  1001. $this->PageInfo[$this->page]['size'] = array($this->wPt, $this->hPt);
  1002. if ($rotation != 0) {
  1003. if ($rotation % 90 != 0)
  1004. $this->Error('Incorrect rotation value: ' . $rotation);
  1005. $this->CurRotation = $rotation;
  1006. $this->PageInfo[$this->page]['rotation'] = $rotation;
  1007. }
  1008. }
  1009. protected function _endpage()
  1010. {
  1011. $this->state = 1;
  1012. }
  1013. protected function _loadfont($font)
  1014. {
  1015. // Load a font definition file from the font directory
  1016. if (strpos($font, '/') !== false || strpos($font, "\\") !== false)
  1017. $this->Error('Incorrect font definition file name: ' . $font);
  1018. include($this->fontpath . $font);
  1019. if (!isset($name))
  1020. $this->Error('Could not include font definition file');
  1021. if (isset($enc))
  1022. $enc = strtolower($enc);
  1023. if (!isset($subsetted))
  1024. $subsetted = false;
  1025. return get_defined_vars();
  1026. }
  1027. protected function _isascii($s)
  1028. {
  1029. // Test if string is ASCII
  1030. $nb = strlen($s);
  1031. for ($i = 0; $i < $nb; $i++) {
  1032. if (ord($s[$i]) > 127)
  1033. return false;
  1034. }
  1035. return true;
  1036. }
  1037. protected function _httpencode($param, $value, $isUTF8)
  1038. {
  1039. // Encode HTTP header field parameter
  1040. if ($this->_isascii($value))
  1041. return $param . '="' . $value . '"';
  1042. if (!$isUTF8)
  1043. $value = utf8_encode($value);
  1044. if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false)
  1045. return $param . '="' . rawurlencode($value) . '"';
  1046. else
  1047. return $param . "*=UTF-8''" . rawurlencode($value);
  1048. }
  1049. protected function _UTF8toUTF16($s)
  1050. {
  1051. // Convert UTF-8 to UTF-16BE with BOM
  1052. $res = "\xFE\xFF";
  1053. $nb = strlen($s);
  1054. $i = 0;
  1055. while ($i < $nb) {
  1056. $c1 = ord($s[$i++]);
  1057. if ($c1 >= 224) {
  1058. // 3-byte character
  1059. $c2 = ord($s[$i++]);
  1060. $c3 = ord($s[$i++]);
  1061. $res .= chr((($c1 & 0x0F) << 4) + (($c2 & 0x3C) >> 2));
  1062. $res .= chr((($c2 & 0x03) << 6) + ($c3 & 0x3F));
  1063. } elseif ($c1 >= 192) {
  1064. // 2-byte character
  1065. $c2 = ord($s[$i++]);
  1066. $res .= chr(($c1 & 0x1C) >> 2);
  1067. $res .= chr((($c1 & 0x03) << 6) + ($c2 & 0x3F));
  1068. } else {
  1069. // Single-byte character
  1070. $res .= "\0" . chr($c1);
  1071. }
  1072. }
  1073. return $res;
  1074. }
  1075. protected function _escape($s)
  1076. {
  1077. // Escape special characters
  1078. if (strpos($s, '(') !== false || strpos($s, ')') !== false || strpos($s, '\\') !== false || strpos($s, "\r") !== false)
  1079. return str_replace(array('\\', '(', ')', "\r"), array('\\\\', '\\(', '\\)', '\\r'), $s);
  1080. else
  1081. return $s;
  1082. }
  1083. protected function _textstring($s)
  1084. {
  1085. // Format a text string
  1086. if (!$this->_isascii($s))
  1087. $s = $this->_UTF8toUTF16($s);
  1088. return '(' . $this->_escape($s) . ')';
  1089. }
  1090. protected function _dounderline($x, $y, $txt)
  1091. {
  1092. // Underline text
  1093. $up = $this->CurrentFont['up'];
  1094. $ut = $this->CurrentFont['ut'];
  1095. $w = $this->GetStringWidth($txt) + $this->ws * substr_count($txt, ' ');
  1096. return sprintf('%.2F %.2F %.2F %.2F re f', $x * $this->k, ($this->h - ($y - $up / 1000 * $this->FontSize)) * $this->k, $w * $this->k, -$ut / 1000 * $this->FontSizePt);
  1097. }
  1098. protected function _parsejpg($file)
  1099. {
  1100. // Extract info from a JPEG file
  1101. $a = getimagesize($file);
  1102. if (!$a)
  1103. $this->Error('Missing or incorrect image file: ' . $file);
  1104. if ($a[2] != 2)
  1105. $this->Error('Not a JPEG file: ' . $file);
  1106. if (!isset($a['channels']) || $a['channels'] == 3)
  1107. $colspace = 'DeviceRGB';
  1108. elseif ($a['channels'] == 4)
  1109. $colspace = 'DeviceCMYK';
  1110. else
  1111. $colspace = 'DeviceGray';
  1112. $bpc = isset($a['bits']) ? $a['bits'] : 8;
  1113. $data = file_get_contents($file);
  1114. return array('w' => $a[0], 'h' => $a[1], 'cs' => $colspace, 'bpc' => $bpc, 'f' => 'DCTDecode', 'data' => $data);
  1115. }
  1116. protected function _parsepng($file)
  1117. {
  1118. // Extract info from a PNG file
  1119. $f = fopen($file, 'rb');
  1120. if (!$f)
  1121. $this->Error('Can\'t open image file: ' . $file);
  1122. $info = $this->_parsepngstream($f, $file);
  1123. fclose($f);
  1124. return $info;
  1125. }
  1126. protected function _parsepngstream($f, $file)
  1127. {
  1128. // Check signature
  1129. if ($this->_readstream($f, 8) != chr(137) . 'PNG' . chr(13) . chr(10) . chr(26) . chr(10))
  1130. $this->Error('Not a PNG file: ' . $file);
  1131. // Read header chunk
  1132. $this->_readstream($f, 4);
  1133. if ($this->_readstream($f, 4) != 'IHDR')
  1134. $this->Error('Incorrect PNG file: ' . $file);
  1135. $w = $this->_readint($f);
  1136. $h = $this->_readint($f);
  1137. $bpc = ord($this->_readstream($f, 1));
  1138. if ($bpc > 8)
  1139. $this->Error('16-bit depth not supported: ' . $file);
  1140. $ct = ord($this->_readstream($f, 1));
  1141. if ($ct == 0 || $ct == 4)
  1142. $colspace = 'DeviceGray';
  1143. elseif ($ct == 2 || $ct == 6)
  1144. $colspace = 'DeviceRGB';
  1145. elseif ($ct == 3)
  1146. $colspace = 'Indexed';
  1147. else
  1148. $this->Error('Unknown color type: ' . $file);
  1149. if (ord($this->_readstream($f, 1)) != 0)
  1150. $this->Error('Unknown compression method: ' . $file);
  1151. if (ord($this->_readstream($f, 1)) != 0)
  1152. $this->Error('Unknown filter method: ' . $file);
  1153. if (ord($this->_readstream($f, 1)) != 0)
  1154. $this->Error('Interlacing not supported: ' . $file);
  1155. $this->_readstream($f, 4);
  1156. $dp = '/Predictor 15 /Colors ' . ($colspace == 'DeviceRGB' ? 3 : 1) . ' /BitsPerComponent ' . $bpc . ' /Columns ' . $w;
  1157. // Scan chunks looking for palette, transparency and image data
  1158. $pal = '';
  1159. $trns = '';
  1160. $data = '';
  1161. do {
  1162. $n = $this->_readint($f);
  1163. $type = $this->_readstream($f, 4);
  1164. if ($type == 'PLTE') {
  1165. // Read palette
  1166. $pal = $this->_readstream($f, $n);
  1167. $this->_readstream($f, 4);
  1168. } elseif ($type == 'tRNS') {
  1169. // Read transparency info
  1170. $t = $this->_readstream($f, $n);
  1171. if ($ct == 0)
  1172. $trns = array(ord(substr($t, 1, 1)));
  1173. elseif ($ct == 2)
  1174. $trns = array(ord(substr($t, 1, 1)), ord(substr($t, 3, 1)), ord(substr($t, 5, 1)));
  1175. else {
  1176. $pos = strpos($t, chr(0));
  1177. if ($pos !== false)
  1178. $trns = array($pos);
  1179. }
  1180. $this->_readstream($f, 4);
  1181. } elseif ($type == 'IDAT') {
  1182. // Read image data block
  1183. $data .= $this->_readstream($f, $n);
  1184. $this->_readstream($f, 4);
  1185. } elseif ($type == 'IEND')
  1186. break;
  1187. else
  1188. $this->_readstream($f, $n + 4);
  1189. } while ($n);
  1190. if ($colspace == 'Indexed' && empty($pal))
  1191. $this->Error('Missing palette in ' . $file);
  1192. $info = array('w' => $w, 'h' => $h, 'cs' => $colspace, 'bpc' => $bpc, 'f' => 'FlateDecode', 'dp' => $dp, 'pal' => $pal, 'trns' => $trns);
  1193. if ($ct >= 4) {
  1194. // Extract alpha channel
  1195. if (!function_exists('gzuncompress'))
  1196. $this->Error('Zlib not available, can\'t handle alpha channel: ' . $file);
  1197. $data = gzuncompress($data);
  1198. $color = '';
  1199. $alpha = '';
  1200. if ($ct == 4) {
  1201. // Gray image
  1202. $len = 2 * $w;
  1203. for ($i = 0; $i < $h; $i++) {
  1204. $pos = (1 + $len) * $i;
  1205. $color .= $data[$pos];
  1206. $alpha .= $data[$pos];
  1207. $line = substr($data, $pos + 1, $len);
  1208. $color .= preg_replace('/(.)./s', '$1', $line);
  1209. $alpha .= preg_replace('/.(.)/s', '$1', $line);
  1210. }
  1211. } else {
  1212. // RGB image
  1213. $len = 4 * $w;
  1214. for ($i = 0; $i < $h; $i++) {
  1215. $pos = (1 + $len) * $i;
  1216. $color .= $data[$pos];
  1217. $alpha .= $data[$pos];
  1218. $line = substr($data, $pos + 1, $len);
  1219. $color .= preg_replace('/(.{3})./s', '$1', $line);
  1220. $alpha .= preg_replace('/.{3}(.)/s', '$1', $line);
  1221. }
  1222. }
  1223. unset($data);
  1224. $data = gzcompress($color);
  1225. $info['smask'] = gzcompress($alpha);
  1226. $this->WithAlpha = true;
  1227. if ($this->PDFVersion < '1.4')
  1228. $this->PDFVersion = '1.4';
  1229. }
  1230. $info['data'] = $data;
  1231. return $info;
  1232. }
  1233. protected function _readstream($f, $n)
  1234. {
  1235. // Read n bytes from stream
  1236. $res = '';
  1237. while ($n > 0 && !feof($f)) {
  1238. $s = fread($f, $n);
  1239. if ($s === false)
  1240. $this->Error('Error while reading stream');
  1241. $n -= strlen($s);
  1242. $res .= $s;
  1243. }
  1244. if ($n > 0)
  1245. $this->Error('Unexpected end of stream');
  1246. return $res;
  1247. }
  1248. protected function _readint($f)
  1249. {
  1250. // Read a 4-byte integer from stream
  1251. $a = unpack('Ni', $this->_readstream($f, 4));
  1252. return $a['i'];
  1253. }
  1254. protected function _parsegif($file)
  1255. {
  1256. // Extract info from a GIF file (via PNG conversion)
  1257. if (!function_exists('imagepng'))
  1258. $this->Error('GD extension is required for GIF support');
  1259. if (!function_exists('imagecreatefromgif'))
  1260. $this->Error('GD has no GIF read support');
  1261. $im = imagecreatefromgif($file);
  1262. if (!$im)
  1263. $this->Error('Missing or incorrect image file: ' . $file);
  1264. imageinterlace($im, 0);
  1265. ob_start();
  1266. imagepng($im);
  1267. $data = ob_get_clean();
  1268. imagedestroy($im);
  1269. $f = fopen('php://temp', 'rb+');
  1270. if (!$f)
  1271. $this->Error('Unable to create memory stream');
  1272. fwrite($f, $data);
  1273. rewind($f);
  1274. $info = $this->_parsepngstream($f, $file);
  1275. fclose($f);
  1276. return $info;
  1277. }
  1278. protected function _out($s)
  1279. {
  1280. // Add a line to the document
  1281. if ($this->state == 2)
  1282. $this->pages[$this->page] .= $s . "\n";
  1283. elseif ($this->state == 1)
  1284. $this->_put($s);
  1285. elseif ($this->state == 0)
  1286. $this->Error('No page has been added yet');
  1287. elseif ($this->state == 3)
  1288. $this->Error('The document is closed');
  1289. }
  1290. protected function _put($s)
  1291. {
  1292. $this->buffer .= $s . "\n";
  1293. }
  1294. protected function _getoffset()
  1295. {
  1296. return strlen($this->buffer);
  1297. }
  1298. protected function _newobj($n = null)
  1299. {
  1300. // Begin a new object
  1301. if ($n === null)
  1302. $n = ++$this->n;
  1303. $this->offsets[$n] = $this->_getoffset();
  1304. $this->_put($n . ' 0 obj');
  1305. }
  1306. protected function _putstream($data)
  1307. {
  1308. $this->_put('stream');
  1309. $this->_put($data);
  1310. $this->_put('endstream');
  1311. }
  1312. protected function _putstreamobject($data)
  1313. {
  1314. if ($this->compress) {
  1315. $entries = '/Filter /FlateDecode ';
  1316. $data = gzcompress($data);
  1317. } else
  1318. $entries = '';
  1319. $entries .= '/Length ' . strlen($data);
  1320. $this->_newobj();
  1321. $this->_put('<<' . $entries . '>>');
  1322. $this->_putstream($data);
  1323. $this->_put('endobj');
  1324. }
  1325. protected function _putpage($n)
  1326. {
  1327. $this->_newobj();
  1328. $this->_put('<</Type /Page');
  1329. $this->_put('/Parent 1 0 R');
  1330. if (isset($this->PageInfo[$n]['size']))
  1331. $this->_put(sprintf('/MediaBox [0 0 %.2F %.2F]', $this->PageInfo[$n]['size'][0], $this->PageInfo[$n]['size'][1]));
  1332. if (isset($this->PageInfo[$n]['rotation']))
  1333. $this->_put('/Rotate ' . $this->PageInfo[$n]['rotation']);
  1334. $this->_put('/Resources 2 0 R');
  1335. if (!empty($this->PageLinks[$n])) {
  1336. $s = '/Annots [';
  1337. foreach ($this->PageLinks[$n] as $pl)
  1338. $s .= $pl[5] . ' 0 R ';
  1339. $s .= ']';
  1340. $this->_put($s);
  1341. }
  1342. if ($this->WithAlpha)
  1343. $this->_put('/Group <</Type /Group /S /Transparency /CS /DeviceRGB>>');
  1344. $this->_put('/Contents ' . ($this->n + 1) . ' 0 R>>');
  1345. $this->_put('endobj');
  1346. // Page content
  1347. if (!empty($this->AliasNbPages))
  1348. $this->pages[$n] = str_replace($this->AliasNbPages, $this->page, $this->pages[$n]);
  1349. $this->_putstreamobject($this->pages[$n]);
  1350. // Annotations
  1351. foreach ($this->PageLinks[$n] as $pl) {
  1352. $this->_newobj();
  1353. $rect = sprintf('%.2F %.2F %.2F %.2F', $pl[0], $pl[1], $pl[0] + $pl[2], $pl[1] - $pl[3]);
  1354. $s = '<</Type /Annot /Subtype /Link /Rect [' . $rect . '] /Border [0 0 0] ';
  1355. if (is_string($pl[4]))
  1356. $s .= '/A <</S /URI /URI ' . $this->_textstring($pl[4]) . '>>>>';
  1357. else {
  1358. $l = $this->links[$pl[4]];
  1359. if (isset($this->PageInfo[$l[0]]['size']))
  1360. $h = $this->PageInfo[$l[0]]['size'][1];
  1361. else
  1362. $h = ($this->DefOrientation == 'P') ? $this->DefPageSize[1] * $this->k : $this->DefPageSize[0] * $this->k;
  1363. $s .= sprintf('/Dest [%d 0 R /XYZ 0 %.2F null]>>', $this->PageInfo[$l[0]]['n'], $h - $l[1] * $this->k);
  1364. }
  1365. $this->_put($s);
  1366. $this->_put('endobj');
  1367. }
  1368. }
  1369. protected function _putpages()
  1370. {
  1371. $nb = $this->page;
  1372. $n = $this->n;
  1373. for ($i = 1; $i <= $nb; $i++) {
  1374. $this->PageInfo[$i]['n'] = ++$n;
  1375. $n++;
  1376. foreach ($this->PageLinks[$i] as &$pl)
  1377. $pl[5] = ++$n;
  1378. unset($pl);
  1379. }
  1380. for ($i = 1; $i <= $nb; $i++)
  1381. $this->_putpage($i);
  1382. // Pages root
  1383. $this->_newobj(1);
  1384. $this->_put('<</Type /Pages');
  1385. $kids = '/Kids [';
  1386. for ($i = 1; $i <= $nb; $i++)
  1387. $kids .= $this->PageInfo[$i]['n'] . ' 0 R ';
  1388. $kids .= ']';
  1389. $this->_put($kids);
  1390. $this->_put('/Count ' . $nb);
  1391. if ($this->DefOrientation == 'P') {
  1392. $w = $this->DefPageSize[0];
  1393. $h = $this->DefPageSize[1];
  1394. } else {
  1395. $w = $this->DefPageSize[1];
  1396. $h = $this->DefPageSize[0];
  1397. }
  1398. $this->_put(sprintf('/MediaBox [0 0 %.2F %.2F]', $w * $this->k, $h * $this->k));
  1399. $this->_put('>>');
  1400. $this->_put('endobj');
  1401. }
  1402. protected function _putfonts()
  1403. {
  1404. foreach ($this->FontFiles as $file => $info) {
  1405. // Font file embedding
  1406. $this->_newobj();
  1407. $this->FontFiles[$file]['n'] = $this->n;
  1408. $font = file_get_contents($this->fontpath . $file, true);
  1409. if (!$font)
  1410. $this->Error('Font file not found: ' . $file);
  1411. $compressed = (substr($file, -2) == '.z');
  1412. if (!$compressed && isset($info['length2']))
  1413. $font = substr($font, 6, $info['length1']) . substr($font, 6 + $info['length1'] + 6, $info['length2']);
  1414. $this->_put('<</Length ' . strlen($font));
  1415. if ($compressed)
  1416. $this->_put('/Filter /FlateDecode');
  1417. $this->_put('/Length1 ' . $info['length1']);
  1418. if (isset($info['length2']))
  1419. $this->_put('/Length2 ' . $info['length2'] . ' /Length3 0');
  1420. $this->_put('>>');
  1421. $this->_putstream($font);
  1422. $this->_put('endobj');
  1423. }
  1424. foreach ($this->fonts as $k => $font) {
  1425. // Encoding
  1426. if (isset($font['diff'])) {
  1427. if (!isset($this->encodings[$font['enc']])) {
  1428. $this->_newobj();
  1429. $this->_put('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences [' . $font['diff'] . ']>>');
  1430. $this->_put('endobj');
  1431. $this->encodings[$font['enc']] = $this->n;
  1432. }
  1433. }
  1434. // ToUnicode CMap
  1435. if (isset($font['uv'])) {
  1436. if (isset($font['enc']))
  1437. $cmapkey = $font['enc'];
  1438. else
  1439. $cmapkey = $font['name'];
  1440. if (!isset($this->cmaps[$cmapkey])) {
  1441. $cmap = $this->_tounicodecmap($font['uv']);
  1442. $this->_putstreamobject($cmap);
  1443. $this->cmaps[$cmapkey] = $this->n;
  1444. }
  1445. }
  1446. // Font object
  1447. $this->fonts[$k]['n'] = $this->n + 1;
  1448. $type = $font['type'];
  1449. $name = $font['name'];
  1450. if (isset($font['subsetted']))
  1451. $name = 'AAAAAA+' . $name;
  1452. if ($type == 'Core') {
  1453. // Core font
  1454. $this->_newobj();
  1455. $this->_put('<</Type /Font');
  1456. $this->_put('/BaseFont /' . $name);
  1457. $this->_put('/Subtype /Type1');
  1458. if ($name != 'Symbol' && $name != 'ZapfDingbats')
  1459. $this->_put('/Encoding /WinAnsiEncoding');
  1460. if (isset($font['uv']))
  1461. $this->_put('/ToUnicode ' . $this->cmaps[$cmapkey] . ' 0 R');
  1462. $this->_put('>>');
  1463. $this->_put('endobj');
  1464. } elseif ($type == 'Type1' || $type == 'TrueType') {
  1465. // Additional Type1 or TrueType/OpenType font
  1466. $this->_newobj();
  1467. $this->_put('<</Type /Font');
  1468. $this->_put('/BaseFont /' . $name);
  1469. $this->_put('/Subtype /' . $type);
  1470. $this->_put('/FirstChar 32 /LastChar 255');
  1471. $this->_put('/Widths ' . ($this->n + 1) . ' 0 R');
  1472. $this->_put('/FontDescriptor ' . ($this->n + 2) . ' 0 R');
  1473. if (isset($font['diff']))
  1474. $this->_put('/Encoding ' . $this->encodings[$font['enc']] . ' 0 R');
  1475. else
  1476. $this->_put('/Encoding /WinAnsiEncoding');
  1477. if (isset($font['uv']))
  1478. $this->_put('/ToUnicode ' . $this->cmaps[$cmapkey] . ' 0 R');
  1479. $this->_put('>>');
  1480. $this->_put('endobj');
  1481. // Widths
  1482. $this->_newobj();
  1483. $cw = &$font['cw'];
  1484. $s = '[';
  1485. for ($i = 32; $i <= 255; $i++)
  1486. $s .= $cw[chr($i)] . ' ';
  1487. $this->_put($s . ']');
  1488. $this->_put('endobj');
  1489. // Descriptor
  1490. $this->_newobj();
  1491. $s = '<</Type /FontDescriptor /FontName /' . $name;
  1492. foreach ($font['desc'] as $k => $v)
  1493. $s .= ' /' . $k . ' ' . $v;
  1494. if (!empty($font['file']))
  1495. $s .= ' /FontFile' . ($type == 'Type1' ? '' : '2') . ' ' . $this->FontFiles[$font['file']]['n'] . ' 0 R';
  1496. $this->_put($s . '>>');
  1497. $this->_put('endobj');
  1498. } else {
  1499. // Allow for additional types
  1500. $mtd = '_put' . strtolower($type);
  1501. if (!method_exists($this, $mtd))
  1502. $this->Error('Unsupported font type: ' . $type);
  1503. $this->$mtd($font);
  1504. }
  1505. }
  1506. }
  1507. protected function _tounicodecmap($uv)
  1508. {
  1509. $ranges = '';
  1510. $nbr = 0;
  1511. $chars = '';
  1512. $nbc = 0;
  1513. foreach ($uv as $c => $v) {
  1514. if (is_array($v)) {
  1515. $ranges .= sprintf("<%02X> <%02X> <%04X>\n", $c, $c + $v[1] - 1, $v[0]);
  1516. $nbr++;
  1517. } else {
  1518. $chars .= sprintf("<%02X> <%04X>\n", $c, $v);
  1519. $nbc++;
  1520. }
  1521. }
  1522. $s = "/CIDInit /ProcSet findresource begin\n";
  1523. $s .= "12 dict begin\n";
  1524. $s .= "begincmap\n";
  1525. $s .= "/CIDSystemInfo\n";
  1526. $s .= "<</Registry (Adobe)\n";
  1527. $s .= "/Ordering (UCS)\n";
  1528. $s .= "/Supplement 0\n";
  1529. $s .= ">> def\n";
  1530. $s .= "/CMapName /Adobe-Identity-UCS def\n";
  1531. $s .= "/CMapType 2 def\n";
  1532. $s .= "1 begincodespacerange\n";
  1533. $s .= "<00> <FF>\n";
  1534. $s .= "endcodespacerange\n";
  1535. if ($nbr > 0) {
  1536. $s .= "$nbr beginbfrange\n";
  1537. $s .= $ranges;
  1538. $s .= "endbfrange\n";
  1539. }
  1540. if ($nbc > 0) {
  1541. $s .= "$nbc beginbfchar\n";
  1542. $s .= $chars;
  1543. $s .= "endbfchar\n";
  1544. }
  1545. $s .= "endcmap\n";
  1546. $s .= "CMapName currentdict /CMap defineresource pop\n";
  1547. $s .= "end\n";
  1548. $s .= "end";
  1549. return $s;
  1550. }
  1551. protected function _putimages()
  1552. {
  1553. foreach (array_keys($this->images) as $file) {
  1554. $this->_putimage($this->images[$file]);
  1555. unset($this->images[$file]['data']);
  1556. unset($this->images[$file]['smask']);
  1557. }
  1558. }
  1559. protected function _putimage(&$info)
  1560. {
  1561. $this->_newobj();
  1562. $info['n'] = $this->n;
  1563. $this->_put('<</Type /XObject');
  1564. $this->_put('/Subtype /Image');
  1565. $this->_put('/Width ' . $info['w']);
  1566. $this->_put('/Height ' . $info['h']);
  1567. if ($info['cs'] == 'Indexed')
  1568. $this->_put('/ColorSpace [/Indexed /DeviceRGB ' . (strlen($info['pal']) / 3 - 1) . ' ' . ($this->n + 1) . ' 0 R]');
  1569. else {
  1570. $this->_put('/ColorSpace /' . $info['cs']);
  1571. if ($info['cs'] == 'DeviceCMYK')
  1572. $this->_put('/Decode [1 0 1 0 1 0 1 0]');
  1573. }
  1574. $this->_put('/BitsPerComponent ' . $info['bpc']);
  1575. if (isset($info['f']))
  1576. $this->_put('/Filter /' . $info['f']);
  1577. if (isset($info['dp']))
  1578. $this->_put('/DecodeParms <<' . $info['dp'] . '>>');
  1579. if (isset($info['trns']) && is_array($info['trns'])) {
  1580. $trns = '';
  1581. for ($i = 0; $i < count($info['trns']); $i++)
  1582. $trns .= $info['trns'][$i] . ' ' . $info['trns'][$i] . ' ';
  1583. $this->_put('/Mask [' . $trns . ']');
  1584. }
  1585. if (isset($info['smask']))
  1586. $this->_put('/SMask ' . ($this->n + 1) . ' 0 R');
  1587. $this->_put('/Length ' . strlen($info['data']) . '>>');
  1588. $this->_putstream($info['data']);
  1589. $this->_put('endobj');
  1590. // Soft mask
  1591. if (isset($info['smask'])) {
  1592. $dp = '/Predictor 15 /Colors 1 /BitsPerComponent 8 /Columns ' . $info['w'];
  1593. $smask = array('w' => $info['w'], 'h' => $info['h'], 'cs' => 'DeviceGray', 'bpc' => 8, 'f' => $info['f'], 'dp' => $dp, 'data' => $info['smask']);
  1594. $this->_putimage($smask);
  1595. }
  1596. // Palette
  1597. if ($info['cs'] == 'Indexed')
  1598. $this->_putstreamobject($info['pal']);
  1599. }
  1600. protected function _putxobjectdict()
  1601. {
  1602. foreach ($this->images as $image)
  1603. $this->_put('/I' . $image['i'] . ' ' . $image['n'] . ' 0 R');
  1604. }
  1605. protected function _putresourcedict()
  1606. {
  1607. $this->_put('/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]');
  1608. $this->_put('/Font <<');
  1609. foreach ($this->fonts as $font)
  1610. $this->_put('/F' . $font['i'] . ' ' . $font['n'] . ' 0 R');
  1611. $this->_put('>>');
  1612. $this->_put('/XObject <<');
  1613. $this->_putxobjectdict();
  1614. $this->_put('>>');
  1615. }
  1616. protected function _putresources()
  1617. {
  1618. $this->_putfonts();
  1619. $this->_putimages();
  1620. // Resource dictionary
  1621. $this->_newobj(2);
  1622. $this->_put('<<');
  1623. $this->_putresourcedict();
  1624. $this->_put('>>');
  1625. $this->_put('endobj');
  1626. }
  1627. protected function _putinfo()
  1628. {
  1629. $this->metadata['Producer'] = 'FPDF ' . FPDF_VERSION;
  1630. $this->metadata['CreationDate'] = 'D:' . @date('YmdHis');
  1631. foreach ($this->metadata as $key => $value)
  1632. $this->_put('/' . $key . ' ' . $this->_textstring($value));
  1633. }
  1634. protected function _putcatalog()
  1635. {
  1636. $n = $this->PageInfo[1]['n'];
  1637. $this->_put('/Type /Catalog');
  1638. $this->_put('/Pages 1 0 R');
  1639. if ($this->ZoomMode == 'fullpage')
  1640. $this->_put('/OpenAction [' . $n . ' 0 R /Fit]');
  1641. elseif ($this->ZoomMode == 'fullwidth')
  1642. $this->_put('/OpenAction [' . $n . ' 0 R /FitH null]');
  1643. elseif ($this->ZoomMode == 'real')
  1644. $this->_put('/OpenAction [' . $n . ' 0 R /XYZ null null 1]');
  1645. elseif (!is_string($this->ZoomMode))
  1646. $this->_put('/OpenAction [' . $n . ' 0 R /XYZ null null ' . sprintf('%.2F', $this->ZoomMode / 100) . ']');
  1647. if ($this->LayoutMode == 'single')
  1648. $this->_put('/PageLayout /SinglePage');
  1649. elseif ($this->LayoutMode == 'continuous')
  1650. $this->_put('/PageLayout /OneColumn');
  1651. elseif ($this->LayoutMode == 'two')
  1652. $this->_put('/PageLayout /TwoColumnLeft');
  1653. }
  1654. protected function _putheader()
  1655. {
  1656. $this->_put('%PDF-' . $this->PDFVersion);
  1657. }
  1658. protected function _puttrailer()
  1659. {
  1660. $this->_put('/Size ' . ($this->n + 1));
  1661. $this->_put('/Root ' . $this->n . ' 0 R');
  1662. $this->_put('/Info ' . ($this->n - 1) . ' 0 R');
  1663. }
  1664. protected function _enddoc()
  1665. {
  1666. $this->_putheader();
  1667. $this->_putpages();
  1668. $this->_putresources();
  1669. // Info
  1670. $this->_newobj();
  1671. $this->_put('<<');
  1672. $this->_putinfo();
  1673. $this->_put('>>');
  1674. $this->_put('endobj');
  1675. // Catalog
  1676. $this->_newobj();
  1677. $this->_put('<<');
  1678. $this->_putcatalog();
  1679. $this->_put('>>');
  1680. $this->_put('endobj');
  1681. // Cross-ref
  1682. $offset = $this->_getoffset();
  1683. $this->_put('xref');
  1684. $this->_put('0 ' . ($this->n + 1));
  1685. $this->_put('0000000000 65535 f ');
  1686. for ($i = 1; $i <= $this->n; $i++)
  1687. $this->_put(sprintf('%010d 00000 n ', $this->offsets[$i]));
  1688. // Trailer
  1689. $this->_put('trailer');
  1690. $this->_put('<<');
  1691. $this->_puttrailer();
  1692. $this->_put('>>');
  1693. $this->_put('startxref');
  1694. $this->_put($offset);
  1695. $this->_put('%%EOF');
  1696. $this->state = 3;
  1697. }
  1698. }