fpdf.php 49 KB

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