fpdf.php 50 KB

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