fpdf.php 47 KB

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