PageRenderTime 156ms CodeModel.GetById 15ms RepoModel.GetById 3ms app.codeStats 1ms

/application/libraries/pdfest.php

https://github.com/paanblogger/sacl
PHP | 4114 lines | 3009 code | 458 blank | 647 comment | 421 complexity | e0b0f4bd5943e0c8cad6d13fd107faf1 MD5 | raw file
  1. <?php
  2. //require_once('/fpdi/fpdi_protection.php');
  3. //require_once("fpdf.php");
  4. //*require_once("fpdf_tpl.php");
  5. //*require_once("pdf_context.php");
  6. //*require_once("wrapper_functions.php");
  7. //*require_once("pdf_parser.php");
  8. //*require_once("fpdi_pdf_parser.php");
  9. ///////////////////////////////////////////////////////
  10. //// FPDF START ////
  11. ///////////////////////////////////////////////////////
  12. /*******************************************************************************
  13. * Software: FPDF *
  14. * Version: 1.53 *
  15. * Date: 2004-12-31 *
  16. * Author: Olivier PLATHEY *
  17. * License: Freeware *
  18. * *
  19. * You may use, modify and redistribute this software as you wish. *
  20. *******************************************************************************/
  21. if(!class_exists('FPDF'))
  22. {
  23. define('FPDF_VERSION','1.53');
  24. class FPDF
  25. {
  26. //Private properties
  27. var $page; //current page number
  28. var $n; //current object number
  29. var $offsets; //array of object offsets
  30. var $buffer; //buffer holding in-memory PDF
  31. var $pages; //array containing pages
  32. var $state; //current document state
  33. var $compress; //compression flag
  34. var $DefOrientation; //default orientation
  35. var $CurOrientation; //current orientation
  36. var $OrientationChanges; //array indicating orientation changes
  37. var $k; //scale factor (number of points in user unit)
  38. var $fwPt,$fhPt; //dimensions of page format in points
  39. var $fw,$fh; //dimensions of page format in user unit
  40. var $wPt,$hPt; //current dimensions of page in points
  41. var $w,$h; //current dimensions of page in user unit
  42. var $lMargin; //left margin
  43. var $tMargin; //top margin
  44. var $rMargin; //right margin
  45. var $bMargin; //page break margin
  46. var $cMargin; //cell margin
  47. var $x,$y; //current position in user unit for cell positioning
  48. var $lasth; //height of last cell printed
  49. var $LineWidth; //line width in user unit
  50. var $CoreFonts; //array of standard font names
  51. var $fonts; //array of used fonts
  52. var $FontFiles; //array of font files
  53. var $diffs; //array of encoding differences
  54. var $images; //array of used images
  55. var $PageLinks; //array of links in pages
  56. var $links; //array of internal links
  57. var $FontFamily; //current font family
  58. var $FontStyle; //current font style
  59. var $underline; //underlining flag
  60. var $CurrentFont; //current font info
  61. var $FontSizePt; //current font size in points
  62. var $FontSize; //current font size in user unit
  63. var $DrawColor; //commands for drawing color
  64. var $FillColor; //commands for filling color
  65. var $TextColor; //commands for text color
  66. var $ColorFlag; //indicates whether fill and text colors are different
  67. var $ws; //word spacing
  68. var $AutoPageBreak; //automatic page breaking
  69. var $PageBreakTrigger; //threshold used to trigger page breaks
  70. var $InFooter; //flag set when processing footer
  71. var $ZoomMode; //zoom display mode
  72. var $LayoutMode; //layout display mode
  73. var $title; //title
  74. var $subject; //subject
  75. var $author; //author
  76. var $keywords; //keywords
  77. var $creator; //creator
  78. var $AliasNbPages; //alias for total number of pages
  79. var $PDFVersion; //PDF version number
  80. /*******************************************************************************
  81. * *
  82. * Public methods *
  83. * *
  84. *******************************************************************************/
  85. function FPDF($orientation='P',$unit='mm',$format='A4')
  86. {
  87. //Some checks
  88. $this->_dochecks();
  89. //Initialization of properties
  90. $this->page=0;
  91. $this->n=2;
  92. $this->buffer='';
  93. $this->pages=array();
  94. $this->OrientationChanges=array();
  95. $this->state=0;
  96. $this->fonts=array();
  97. $this->FontFiles=array();
  98. $this->diffs=array();
  99. $this->images=array();
  100. $this->links=array();
  101. $this->InFooter=false;
  102. $this->lasth=0;
  103. $this->FontFamily='';
  104. $this->FontStyle='';
  105. $this->FontSizePt=12;
  106. $this->underline=false;
  107. $this->DrawColor='0 G';
  108. $this->FillColor='0 g';
  109. $this->TextColor='0 g';
  110. $this->ColorFlag=false;
  111. $this->ws=0;
  112. //Standard fonts
  113. $this->CoreFonts=array('courier'=>'Courier','courierB'=>'Courier-Bold','courierI'=>'Courier-Oblique','courierBI'=>'Courier-BoldOblique',
  114. 'helvetica'=>'Helvetica','helveticaB'=>'Helvetica-Bold','helveticaI'=>'Helvetica-Oblique','helveticaBI'=>'Helvetica-BoldOblique',
  115. 'times'=>'Times-Roman','timesB'=>'Times-Bold','timesI'=>'Times-Italic','timesBI'=>'Times-BoldItalic',
  116. 'symbol'=>'Symbol','zapfdingbats'=>'ZapfDingbats');
  117. //Scale factor
  118. if($unit=='pt')
  119. $this->k=1;
  120. elseif($unit=='mm')
  121. $this->k=72/25.4;
  122. elseif($unit=='cm')
  123. $this->k=72/2.54;
  124. elseif($unit=='in')
  125. $this->k=72;
  126. else
  127. $this->Error('Incorrect unit: '.$unit);
  128. //Page format
  129. if(is_string($format))
  130. {
  131. $format=strtolower($format);
  132. if($format=='a3')
  133. $format=array(841.89,1190.55);
  134. elseif($format=='a4')
  135. $format=array(595.28,841.89);
  136. elseif($format=='a5')
  137. $format=array(420.94,595.28);
  138. elseif($format=='letter')
  139. $format=array(612,792);
  140. elseif($format=='legal')
  141. $format=array(612,1008);
  142. else
  143. $this->Error('Unknown page format: '.$format);
  144. $this->fwPt=$format[0];
  145. $this->fhPt=$format[1];
  146. }
  147. else
  148. {
  149. $this->fwPt=$format[0]*$this->k;
  150. $this->fhPt=$format[1]*$this->k;
  151. }
  152. $this->fw=$this->fwPt/$this->k;
  153. $this->fh=$this->fhPt/$this->k;
  154. //Page orientation
  155. $orientation=strtolower($orientation);
  156. if($orientation=='p' || $orientation=='portrait')
  157. {
  158. $this->DefOrientation='P';
  159. $this->wPt=$this->fwPt;
  160. $this->hPt=$this->fhPt;
  161. }
  162. elseif($orientation=='l' || $orientation=='landscape')
  163. {
  164. $this->DefOrientation='L';
  165. $this->wPt=$this->fhPt;
  166. $this->hPt=$this->fwPt;
  167. }
  168. else
  169. $this->Error('Incorrect orientation: '.$orientation);
  170. $this->CurOrientation=$this->DefOrientation;
  171. $this->w=$this->wPt/$this->k;
  172. $this->h=$this->hPt/$this->k;
  173. //Page margins (1 cm)
  174. $margin=28.35/$this->k;
  175. $this->SetMargins($margin,$margin);
  176. //Interior cell margin (1 mm)
  177. $this->cMargin=$margin/10;
  178. //Line width (0.2 mm)
  179. $this->LineWidth=.567/$this->k;
  180. //Automatic page break
  181. $this->SetAutoPageBreak(true,2*$margin);
  182. //Full width display mode
  183. $this->SetDisplayMode('fullwidth');
  184. //Enable compression
  185. $this->SetCompression(true);
  186. //Set default PDF version number
  187. $this->PDFVersion='1.3';
  188. }
  189. function SetMargins($left,$top,$right=-1)
  190. {
  191. //Set left, top and right margins
  192. $this->lMargin=$left;
  193. $this->tMargin=$top;
  194. if($right==-1)
  195. $right=$left;
  196. $this->rMargin=$right;
  197. }
  198. function SetLeftMargin($margin)
  199. {
  200. //Set left margin
  201. $this->lMargin=$margin;
  202. if($this->page>0 && $this->x<$margin)
  203. $this->x=$margin;
  204. }
  205. function SetTopMargin($margin)
  206. {
  207. //Set top margin
  208. $this->tMargin=$margin;
  209. }
  210. function SetRightMargin($margin)
  211. {
  212. //Set right margin
  213. $this->rMargin=$margin;
  214. }
  215. function SetAutoPageBreak($auto,$margin=0)
  216. {
  217. //Set auto page break mode and triggering margin
  218. $this->AutoPageBreak=$auto;
  219. $this->bMargin=$margin;
  220. $this->PageBreakTrigger=$this->h-$margin;
  221. }
  222. function SetDisplayMode($zoom,$layout='continuous')
  223. {
  224. //Set display mode in viewer
  225. if($zoom=='fullpage' || $zoom=='fullwidth' || $zoom=='real' || $zoom=='default' || !is_string($zoom))
  226. $this->ZoomMode=$zoom;
  227. else
  228. $this->Error('Incorrect zoom display mode: '.$zoom);
  229. if($layout=='single' || $layout=='continuous' || $layout=='two' || $layout=='default')
  230. $this->LayoutMode=$layout;
  231. else
  232. $this->Error('Incorrect layout display mode: '.$layout);
  233. }
  234. function SetCompression($compress)
  235. {
  236. //Set page compression
  237. if(function_exists('gzcompress'))
  238. $this->compress=$compress;
  239. else
  240. $this->compress=false;
  241. }
  242. function SetTitle($title)
  243. {
  244. //Title of document
  245. $this->title=$title;
  246. }
  247. function SetSubject($subject)
  248. {
  249. //Subject of document
  250. $this->subject=$subject;
  251. }
  252. function SetAuthor($author)
  253. {
  254. //Author of document
  255. $this->author=$author;
  256. }
  257. function SetKeywords($keywords)
  258. {
  259. //Keywords of document
  260. $this->keywords=$keywords;
  261. }
  262. function SetCreator($creator)
  263. {
  264. //Creator of document
  265. $this->creator=$creator;
  266. }
  267. function AliasNbPages($alias='{nb}')
  268. {
  269. //Define an alias for total number of pages
  270. $this->AliasNbPages=$alias;
  271. }
  272. function Error($msg)
  273. {
  274. //Fatal error
  275. die('<B>FPDF error: </B>'.$msg);
  276. }
  277. function Open()
  278. {
  279. //Begin document
  280. $this->state=1;
  281. }
  282. function Close()
  283. {
  284. //Terminate document
  285. if($this->state==3)
  286. return;
  287. if($this->page==0)
  288. $this->AddPage();
  289. //Page footer
  290. $this->InFooter=true;
  291. $this->Footer();
  292. $this->InFooter=false;
  293. //Close page
  294. $this->_endpage();
  295. //Close document
  296. $this->_enddoc();
  297. }
  298. function AddPage($orientation='')
  299. {
  300. //Start a new page
  301. if($this->state==0)
  302. $this->Open();
  303. $family=$this->FontFamily;
  304. $style=$this->FontStyle.($this->underline ? 'U' : '');
  305. $size=$this->FontSizePt;
  306. $lw=$this->LineWidth;
  307. $dc=$this->DrawColor;
  308. $fc=$this->FillColor;
  309. $tc=$this->TextColor;
  310. $cf=$this->ColorFlag;
  311. if($this->page>0)
  312. {
  313. //Page footer
  314. $this->InFooter=true;
  315. $this->Footer();
  316. $this->InFooter=false;
  317. //Close page
  318. $this->_endpage();
  319. }
  320. //Start new page
  321. $this->_beginpage($orientation);
  322. //Set line cap style to square
  323. $this->_out('2 J');
  324. //Set line width
  325. $this->LineWidth=$lw;
  326. $this->_out(sprintf('%.2f w',$lw*$this->k));
  327. //Set font
  328. if($family)
  329. $this->SetFont($family,$style,$size);
  330. //Set colors
  331. $this->DrawColor=$dc;
  332. if($dc!='0 G')
  333. $this->_out($dc);
  334. $this->FillColor=$fc;
  335. if($fc!='0 g')
  336. $this->_out($fc);
  337. $this->TextColor=$tc;
  338. $this->ColorFlag=$cf;
  339. //Page header
  340. $this->Header();
  341. //Restore line width
  342. if($this->LineWidth!=$lw)
  343. {
  344. $this->LineWidth=$lw;
  345. $this->_out(sprintf('%.2f w',$lw*$this->k));
  346. }
  347. //Restore font
  348. if($family)
  349. $this->SetFont($family,$style,$size);
  350. //Restore colors
  351. if($this->DrawColor!=$dc)
  352. {
  353. $this->DrawColor=$dc;
  354. $this->_out($dc);
  355. }
  356. if($this->FillColor!=$fc)
  357. {
  358. $this->FillColor=$fc;
  359. $this->_out($fc);
  360. }
  361. $this->TextColor=$tc;
  362. $this->ColorFlag=$cf;
  363. }
  364. function Header()
  365. {
  366. //To be implemented in your own inherited class
  367. }
  368. function Footer()
  369. {
  370. //To be implemented in your own inherited class
  371. }
  372. function PageNo()
  373. {
  374. //Get current page number
  375. return $this->page;
  376. }
  377. function SetDrawColor($r,$g=-1,$b=-1)
  378. {
  379. //Set color for all stroking operations
  380. if(($r==0 && $g==0 && $b==0) || $g==-1)
  381. $this->DrawColor=sprintf('%.3f G',$r/255);
  382. else
  383. $this->DrawColor=sprintf('%.3f %.3f %.3f RG',$r/255,$g/255,$b/255);
  384. if($this->page>0)
  385. $this->_out($this->DrawColor);
  386. }
  387. function SetFillColor($r,$g=-1,$b=-1)
  388. {
  389. //Set color for all filling operations
  390. if(($r==0 && $g==0 && $b==0) || $g==-1)
  391. $this->FillColor=sprintf('%.3f g',$r/255);
  392. else
  393. $this->FillColor=sprintf('%.3f %.3f %.3f rg',$r/255,$g/255,$b/255);
  394. $this->ColorFlag=($this->FillColor!=$this->TextColor);
  395. if($this->page>0)
  396. $this->_out($this->FillColor);
  397. }
  398. function SetTextColor($r,$g=-1,$b=-1)
  399. {
  400. //Set color for text
  401. if(($r==0 && $g==0 && $b==0) || $g==-1)
  402. $this->TextColor=sprintf('%.3f g',$r/255);
  403. else
  404. $this->TextColor=sprintf('%.3f %.3f %.3f rg',$r/255,$g/255,$b/255);
  405. $this->ColorFlag=($this->FillColor!=$this->TextColor);
  406. }
  407. function GetStringWidth($s)
  408. {
  409. //Get width of a string in the current font
  410. $s=(string)$s;
  411. $cw=&$this->CurrentFont['cw'];
  412. $w=0;
  413. $l=strlen($s);
  414. for($i=0;$i<$l;$i++)
  415. $w+=$cw[$s{$i}];
  416. return $w*$this->FontSize/1000;
  417. }
  418. function SetLineWidth($width)
  419. {
  420. //Set line width
  421. $this->LineWidth=$width;
  422. if($this->page>0)
  423. $this->_out(sprintf('%.2f w',$width*$this->k));
  424. }
  425. function Line($x1,$y1,$x2,$y2)
  426. {
  427. //Draw a line
  428. $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));
  429. }
  430. function Rect($x,$y,$w,$h,$style='')
  431. {
  432. //Draw a rectangle
  433. if($style=='F')
  434. $op='f';
  435. elseif($style=='FD' || $style=='DF')
  436. $op='B';
  437. else
  438. $op='S';
  439. $this->_out(sprintf('%.2f %.2f %.2f %.2f re %s',$x*$this->k,($this->h-$y)*$this->k,$w*$this->k,-$h*$this->k,$op));
  440. }
  441. function AddFont($family,$style='',$file='')
  442. {
  443. //Add a TrueType or Type1 font
  444. $family=strtolower($family);
  445. if($file=='')
  446. $file=str_replace(' ','',$family).strtolower($style).'.php';
  447. if($family=='arial')
  448. $family='helvetica';
  449. $style=strtoupper($style);
  450. if($style=='IB')
  451. $style='BI';
  452. $fontkey=$family.$style;
  453. if(isset($this->fonts[$fontkey]))
  454. $this->Error('Font already added: '.$family.' '.$style);
  455. include($this->_getfontpath().$file);
  456. if(!isset($name))
  457. $this->Error('Could not include font definition file');
  458. $i=count($this->fonts)+1;
  459. $this->fonts[$fontkey]=array('i'=>$i,'type'=>$type,'name'=>$name,'desc'=>$desc,'up'=>$up,'ut'=>$ut,'cw'=>$cw,'enc'=>$enc,'file'=>$file);
  460. if($diff)
  461. {
  462. //Search existing encodings
  463. $d=0;
  464. $nb=count($this->diffs);
  465. for($i=1;$i<=$nb;$i++)
  466. {
  467. if($this->diffs[$i]==$diff)
  468. {
  469. $d=$i;
  470. break;
  471. }
  472. }
  473. if($d==0)
  474. {
  475. $d=$nb+1;
  476. $this->diffs[$d]=$diff;
  477. }
  478. $this->fonts[$fontkey]['diff']=$d;
  479. }
  480. if($file)
  481. {
  482. if($type=='TrueType')
  483. $this->FontFiles[$file]=array('length1'=>$originalsize);
  484. else
  485. $this->FontFiles[$file]=array('length1'=>$size1,'length2'=>$size2);
  486. }
  487. }
  488. function SetFont($family,$style='',$size=0)
  489. {
  490. //Select a font; size given in points
  491. global $fpdf_charwidths;
  492. $family=strtolower($family);
  493. if($family=='')
  494. $family=$this->FontFamily;
  495. if($family=='arial')
  496. $family='helvetica';
  497. elseif($family=='symbol' || $family=='zapfdingbats')
  498. $style='';
  499. $style=strtoupper($style);
  500. if(strpos($style,'U')!==false)
  501. {
  502. $this->underline=true;
  503. $style=str_replace('U','',$style);
  504. }
  505. else
  506. $this->underline=false;
  507. if($style=='IB')
  508. $style='BI';
  509. if($size==0)
  510. $size=$this->FontSizePt;
  511. //Test if font is already selected
  512. if($this->FontFamily==$family && $this->FontStyle==$style && $this->FontSizePt==$size)
  513. return;
  514. //Test if used for the first time
  515. $fontkey=$family.$style;
  516. if(!isset($this->fonts[$fontkey]))
  517. {
  518. //Check if one of the standard fonts
  519. if(isset($this->CoreFonts[$fontkey]))
  520. {
  521. if(!isset($fpdf_charwidths[$fontkey]))
  522. {
  523. //Load metric file
  524. $file=$family;
  525. if($family=='times' || $family=='helvetica')
  526. $file.=strtolower($style);
  527. //include($this->_getfontpath().$file.'.php');
  528. $type = 'Core';
  529. $name = 'Helvetica-Oblique';
  530. $up = -100;
  531. $ut = 50;
  532. $cw = array(
  533. chr(0)=>278,chr(1)=>278,chr(2)=>278,chr(3)=>278,chr(4)=>278,chr(5)=>278,chr(6)=>278,chr(7)=>278,chr(8)=>278,chr(9)=>278,chr(10)=>278,chr(11)=>278,chr(12)=>278,chr(13)=>278,chr(14)=>278,chr(15)=>278,chr(16)=>278,chr(17)=>278,chr(18)=>278,chr(19)=>278,chr(20)=>278,chr(21)=>278,
  534. chr(22)=>278,chr(23)=>278,chr(24)=>278,chr(25)=>278,chr(26)=>278,chr(27)=>278,chr(28)=>278,chr(29)=>278,chr(30)=>278,chr(31)=>278,' '=>278,'!'=>278,'"'=>355,'#'=>556,'$'=>556,'%'=>889,'&'=>667,'\''=>191,'('=>333,')'=>333,'*'=>389,'+'=>584,
  535. ','=>278,'-'=>333,'.'=>278,'/'=>278,'0'=>556,'1'=>556,'2'=>556,'3'=>556,'4'=>556,'5'=>556,'6'=>556,'7'=>556,'8'=>556,'9'=>556,':'=>278,';'=>278,'<'=>584,'='=>584,'>'=>584,'?'=>556,'@'=>1015,'A'=>667,
  536. 'B'=>667,'C'=>722,'D'=>722,'E'=>667,'F'=>611,'G'=>778,'H'=>722,'I'=>278,'J'=>500,'K'=>667,'L'=>556,'M'=>833,'N'=>722,'O'=>778,'P'=>667,'Q'=>778,'R'=>722,'S'=>667,'T'=>611,'U'=>722,'V'=>667,'W'=>944,
  537. 'X'=>667,'Y'=>667,'Z'=>611,'['=>278,'\\'=>278,']'=>278,'^'=>469,'_'=>556,'`'=>333,'a'=>556,'b'=>556,'c'=>500,'d'=>556,'e'=>556,'f'=>278,'g'=>556,'h'=>556,'i'=>222,'j'=>222,'k'=>500,'l'=>222,'m'=>833,
  538. 'n'=>556,'o'=>556,'p'=>556,'q'=>556,'r'=>333,'s'=>500,'t'=>278,'u'=>556,'v'=>500,'w'=>722,'x'=>500,'y'=>500,'z'=>500,'{'=>334,'|'=>260,'}'=>334,'~'=>584,chr(127)=>350,chr(128)=>556,chr(129)=>350,chr(130)=>222,chr(131)=>556,
  539. chr(132)=>333,chr(133)=>1000,chr(134)=>556,chr(135)=>556,chr(136)=>333,chr(137)=>1000,chr(138)=>667,chr(139)=>333,chr(140)=>1000,chr(141)=>350,chr(142)=>611,chr(143)=>350,chr(144)=>350,chr(145)=>222,chr(146)=>222,chr(147)=>333,chr(148)=>333,chr(149)=>350,chr(150)=>556,chr(151)=>1000,chr(152)=>333,chr(153)=>1000,
  540. chr(154)=>500,chr(155)=>333,chr(156)=>944,chr(157)=>350,chr(158)=>500,chr(159)=>667,chr(160)=>278,chr(161)=>333,chr(162)=>556,chr(163)=>556,chr(164)=>556,chr(165)=>556,chr(166)=>260,chr(167)=>556,chr(168)=>333,chr(169)=>737,chr(170)=>370,chr(171)=>556,chr(172)=>584,chr(173)=>333,chr(174)=>737,chr(175)=>333,
  541. chr(176)=>400,chr(177)=>584,chr(178)=>333,chr(179)=>333,chr(180)=>333,chr(181)=>556,chr(182)=>537,chr(183)=>278,chr(184)=>333,chr(185)=>333,chr(186)=>365,chr(187)=>556,chr(188)=>834,chr(189)=>834,chr(190)=>834,chr(191)=>611,chr(192)=>667,chr(193)=>667,chr(194)=>667,chr(195)=>667,chr(196)=>667,chr(197)=>667,
  542. chr(198)=>1000,chr(199)=>722,chr(200)=>667,chr(201)=>667,chr(202)=>667,chr(203)=>667,chr(204)=>278,chr(205)=>278,chr(206)=>278,chr(207)=>278,chr(208)=>722,chr(209)=>722,chr(210)=>778,chr(211)=>778,chr(212)=>778,chr(213)=>778,chr(214)=>778,chr(215)=>584,chr(216)=>778,chr(217)=>722,chr(218)=>722,chr(219)=>722,
  543. chr(220)=>722,chr(221)=>667,chr(222)=>667,chr(223)=>611,chr(224)=>556,chr(225)=>556,chr(226)=>556,chr(227)=>556,chr(228)=>556,chr(229)=>556,chr(230)=>889,chr(231)=>500,chr(232)=>556,chr(233)=>556,chr(234)=>556,chr(235)=>556,chr(236)=>278,chr(237)=>278,chr(238)=>278,chr(239)=>278,chr(240)=>556,chr(241)=>556,
  544. chr(242)=>556,chr(243)=>556,chr(244)=>556,chr(245)=>556,chr(246)=>556,chr(247)=>584,chr(248)=>611,chr(249)=>556,chr(250)=>556,chr(251)=>556,chr(252)=>556,chr(253)=>500,chr(254)=>556,chr(255)=>500);
  545. $fpdf_charwidths[$fontkey] = $cw;
  546. if(!isset($fpdf_charwidths[$fontkey]))
  547. $this->Error('Could not include font metric file');
  548. }
  549. $i=count($this->fonts)+1;
  550. $this->fonts[$fontkey]=array('i'=>$i,'type'=>'core','name'=>$this->CoreFonts[$fontkey],'up'=>-100,'ut'=>50,'cw'=>$fpdf_charwidths[$fontkey]);
  551. }
  552. else
  553. $this->Error('Undefined font: '.$family.' '.$style);
  554. }
  555. //Select it
  556. $this->FontFamily=$family;
  557. $this->FontStyle=$style;
  558. $this->FontSizePt=$size;
  559. $this->FontSize=$size/$this->k;
  560. $this->CurrentFont=&$this->fonts[$fontkey];
  561. if($this->page>0)
  562. $this->_out(sprintf('BT /F%d %.2f Tf ET',$this->CurrentFont['i'],$this->FontSizePt));
  563. }
  564. function SetFontSize($size)
  565. {
  566. //Set font size in points
  567. if($this->FontSizePt==$size)
  568. return;
  569. $this->FontSizePt=$size;
  570. $this->FontSize=$size/$this->k;
  571. if($this->page>0)
  572. $this->_out(sprintf('BT /F%d %.2f Tf ET',$this->CurrentFont['i'],$this->FontSizePt));
  573. }
  574. function AddLink()
  575. {
  576. //Create a new internal link
  577. $n=count($this->links)+1;
  578. $this->links[$n]=array(0,0);
  579. return $n;
  580. }
  581. function SetLink($link,$y=0,$page=-1)
  582. {
  583. //Set destination of internal link
  584. if($y==-1)
  585. $y=$this->y;
  586. if($page==-1)
  587. $page=$this->page;
  588. $this->links[$link]=array($page,$y);
  589. }
  590. function Link($x,$y,$w,$h,$link)
  591. {
  592. //Put a link on the page
  593. $this->PageLinks[$this->page][]=array($x*$this->k,$this->hPt-$y*$this->k,$w*$this->k,$h*$this->k,$link);
  594. }
  595. function Text($x,$y,$txt)
  596. {
  597. //Output a string
  598. $s=sprintf('BT %.2f %.2f Td (%s) Tj ET',$x*$this->k,($this->h-$y)*$this->k,$this->_escape($txt));
  599. if($this->underline && $txt!='')
  600. $s.=' '.$this->_dounderline($x,$y,$txt);
  601. if($this->ColorFlag)
  602. $s='q '.$this->TextColor.' '.$s.' Q';
  603. $this->_out($s);
  604. }
  605. function AcceptPageBreak()
  606. {
  607. //Accept automatic page break or not
  608. return $this->AutoPageBreak;
  609. }
  610. function Cell($w,$h=0,$txt='',$border=0,$ln=0,$align='',$fill=0,$link='')
  611. {
  612. //Output a cell
  613. $k=$this->k;
  614. if($this->y+$h>$this->PageBreakTrigger && !$this->InFooter && $this->AcceptPageBreak())
  615. {
  616. //Automatic page break
  617. $x=$this->x;
  618. $ws=$this->ws;
  619. if($ws>0)
  620. {
  621. $this->ws=0;
  622. $this->_out('0 Tw');
  623. }
  624. $this->AddPage($this->CurOrientation);
  625. $this->x=$x;
  626. if($ws>0)
  627. {
  628. $this->ws=$ws;
  629. $this->_out(sprintf('%.3f Tw',$ws*$k));
  630. }
  631. }
  632. if($w==0)
  633. $w=$this->w-$this->rMargin-$this->x;
  634. $s='';
  635. if($fill==1 || $border==1)
  636. {
  637. if($fill==1)
  638. $op=($border==1) ? 'B' : 'f';
  639. else
  640. $op='S';
  641. $s=sprintf('%.2f %.2f %.2f %.2f re %s ',$this->x*$k,($this->h-$this->y)*$k,$w*$k,-$h*$k,$op);
  642. }
  643. if(is_string($border))
  644. {
  645. $x=$this->x;
  646. $y=$this->y;
  647. if(strpos($border,'L')!==false)
  648. $s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-$y)*$k,$x*$k,($this->h-($y+$h))*$k);
  649. if(strpos($border,'T')!==false)
  650. $s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-$y)*$k);
  651. if(strpos($border,'R')!==false)
  652. $s.=sprintf('%.2f %.2f m %.2f %.2f l S ',($x+$w)*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
  653. if(strpos($border,'B')!==false)
  654. $s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-($y+$h))*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
  655. }
  656. if($txt!=='')
  657. {
  658. if($align=='R')
  659. $dx=$w-$this->cMargin-$this->GetStringWidth($txt);
  660. elseif($align=='C')
  661. $dx=($w-$this->GetStringWidth($txt))/2;
  662. else
  663. $dx=$this->cMargin;
  664. if($this->ColorFlag)
  665. $s.='q '.$this->TextColor.' ';
  666. $txt2=str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$txt)));
  667. $s.=sprintf('BT %.2f %.2f Td (%s) Tj ET',($this->x+$dx)*$k,($this->h-($this->y+.5*$h+.3*$this->FontSize))*$k,$txt2);
  668. if($this->underline)
  669. $s.=' '.$this->_dounderline($this->x+$dx,$this->y+.5*$h+.3*$this->FontSize,$txt);
  670. if($this->ColorFlag)
  671. $s.=' Q';
  672. if($link)
  673. $this->Link($this->x+$dx,$this->y+.5*$h-.5*$this->FontSize,$this->GetStringWidth($txt),$this->FontSize,$link);
  674. }
  675. if($s)
  676. $this->_out($s);
  677. $this->lasth=$h;
  678. if($ln>0)
  679. {
  680. //Go to next line
  681. $this->y+=$h;
  682. if($ln==1)
  683. $this->x=$this->lMargin;
  684. }
  685. else
  686. $this->x+=$w;
  687. }
  688. function MultiCell($w,$h,$txt,$border=0,$align='J',$fill=0)
  689. {
  690. //Output text with automatic or explicit line breaks
  691. $cw=&$this->CurrentFont['cw'];
  692. if($w==0)
  693. $w=$this->w-$this->rMargin-$this->x;
  694. $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
  695. $s=str_replace("\r",'',$txt);
  696. $nb=strlen($s);
  697. if($nb>0 && $s[$nb-1]=="\n")
  698. $nb--;
  699. $b=0;
  700. if($border)
  701. {
  702. if($border==1)
  703. {
  704. $border='LTRB';
  705. $b='LRT';
  706. $b2='LR';
  707. }
  708. else
  709. {
  710. $b2='';
  711. if(strpos($border,'L')!==false)
  712. $b2.='L';
  713. if(strpos($border,'R')!==false)
  714. $b2.='R';
  715. $b=(strpos($border,'T')!==false) ? $b2.'T' : $b2;
  716. }
  717. }
  718. $sep=-1;
  719. $i=0;
  720. $j=0;
  721. $l=0;
  722. $ns=0;
  723. $nl=1;
  724. while($i<$nb)
  725. {
  726. //Get next character
  727. $c=$s{$i};
  728. if($c=="\n")
  729. {
  730. //Explicit line break
  731. if($this->ws>0)
  732. {
  733. $this->ws=0;
  734. $this->_out('0 Tw');
  735. }
  736. $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
  737. $i++;
  738. $sep=-1;
  739. $j=$i;
  740. $l=0;
  741. $ns=0;
  742. $nl++;
  743. if($border && $nl==2)
  744. $b=$b2;
  745. continue;
  746. }
  747. if($c==' ')
  748. {
  749. $sep=$i;
  750. $ls=$l;
  751. $ns++;
  752. }
  753. $l+=$cw[$c];
  754. if($l>$wmax)
  755. {
  756. //Automatic line break
  757. if($sep==-1)
  758. {
  759. if($i==$j)
  760. $i++;
  761. if($this->ws>0)
  762. {
  763. $this->ws=0;
  764. $this->_out('0 Tw');
  765. }
  766. $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
  767. }
  768. else
  769. {
  770. if($align=='J')
  771. {
  772. $this->ws=($ns>1) ? ($wmax-$ls)/1000*$this->FontSize/($ns-1) : 0;
  773. $this->_out(sprintf('%.3f Tw',$this->ws*$this->k));
  774. }
  775. $this->Cell($w,$h,substr($s,$j,$sep-$j),$b,2,$align,$fill);
  776. $i=$sep+1;
  777. }
  778. $sep=-1;
  779. $j=$i;
  780. $l=0;
  781. $ns=0;
  782. $nl++;
  783. if($border && $nl==2)
  784. $b=$b2;
  785. }
  786. else
  787. $i++;
  788. }
  789. //Last chunk
  790. if($this->ws>0)
  791. {
  792. $this->ws=0;
  793. $this->_out('0 Tw');
  794. }
  795. if($border && strpos($border,'B')!==false)
  796. $b.='B';
  797. $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
  798. $this->x=$this->lMargin;
  799. }
  800. function Write($h,$txt,$link='')
  801. {
  802. //Output text in flowing mode
  803. $cw=&$this->CurrentFont['cw'];
  804. $w=$this->w-$this->rMargin-$this->x;
  805. $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
  806. $s=str_replace("\r",'',$txt);
  807. $nb=strlen($s);
  808. $sep=-1;
  809. $i=0;
  810. $j=0;
  811. $l=0;
  812. $nl=1;
  813. while($i<$nb)
  814. {
  815. //Get next character
  816. $c=$s{$i};
  817. if($c=="\n")
  818. {
  819. //Explicit line break
  820. $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
  821. $i++;
  822. $sep=-1;
  823. $j=$i;
  824. $l=0;
  825. if($nl==1)
  826. {
  827. $this->x=$this->lMargin;
  828. $w=$this->w-$this->rMargin-$this->x;
  829. $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
  830. }
  831. $nl++;
  832. continue;
  833. }
  834. if($c==' ')
  835. $sep=$i;
  836. $l+=$cw[$c];
  837. if($l>$wmax)
  838. {
  839. //Automatic line break
  840. if($sep==-1)
  841. {
  842. if($this->x>$this->lMargin)
  843. {
  844. //Move to next line
  845. $this->x=$this->lMargin;
  846. $this->y+=$h;
  847. $w=$this->w-$this->rMargin-$this->x;
  848. $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
  849. $i++;
  850. $nl++;
  851. continue;
  852. }
  853. if($i==$j)
  854. $i++;
  855. $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
  856. }
  857. else
  858. {
  859. $this->Cell($w,$h,substr($s,$j,$sep-$j),0,2,'',0,$link);
  860. $i=$sep+1;
  861. }
  862. $sep=-1;
  863. $j=$i;
  864. $l=0;
  865. if($nl==1)
  866. {
  867. $this->x=$this->lMargin;
  868. $w=$this->w-$this->rMargin-$this->x;
  869. $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
  870. }
  871. $nl++;
  872. }
  873. else
  874. $i++;
  875. }
  876. //Last chunk
  877. if($i!=$j)
  878. $this->Cell($l/1000*$this->FontSize,$h,substr($s,$j),0,0,'',0,$link);
  879. }
  880. function Image($file,$x,$y,$w=0,$h=0,$type='',$link='')
  881. {
  882. //Put an image on the page
  883. if(!isset($this->images[$file]))
  884. {
  885. //First use of image, get info
  886. if($type=='')
  887. {
  888. $pos=strrpos($file,'.');
  889. if(!$pos)
  890. $this->Error('Image file has no extension and no type was specified: '.$file);
  891. $type=substr($file,$pos+1);
  892. }
  893. $type=strtolower($type);
  894. $mqr=get_magic_quotes_runtime();
  895. //set_magic_quotes_runtime(0);
  896. if($type=='jpg' || $type=='jpeg')
  897. $info=$this->_parsejpg($file);
  898. elseif($type=='png')
  899. $info=$this->_parsepng($file);
  900. else
  901. {
  902. //Allow for additional formats
  903. $mtd='_parse'.$type;
  904. if(!method_exists($this,$mtd))
  905. $this->Error('Unsupported image type: '.$type);
  906. $info=$this->$mtd($file);
  907. }
  908. //set_magic_quotes_runtime($mqr);
  909. $info['i']=count($this->images)+1;
  910. $this->images[$file]=$info;
  911. }
  912. else
  913. $info=$this->images[$file];
  914. //Automatic width and height calculation if needed
  915. if($w==0 && $h==0)
  916. {
  917. //Put image at 72 dpi
  918. $w=$info['w']/$this->k;
  919. $h=$info['h']/$this->k;
  920. }
  921. if($w==0)
  922. $w=$h*$info['w']/$info['h'];
  923. if($h==0)
  924. $h=$w*$info['h']/$info['w'];
  925. $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']));
  926. if($link)
  927. $this->Link($x,$y,$w,$h,$link);
  928. }
  929. function Ln($h='')
  930. {
  931. //Line feed; default value is last cell height
  932. $this->x=$this->lMargin;
  933. if(is_string($h))
  934. $this->y+=$this->lasth;
  935. else
  936. $this->y+=$h;
  937. }
  938. function GetX()
  939. {
  940. //Get x position
  941. return $this->x;
  942. }
  943. function SetX($x)
  944. {
  945. //Set x position
  946. if($x>=0)
  947. $this->x=$x;
  948. else
  949. $this->x=$this->w+$x;
  950. }
  951. function GetY()
  952. {
  953. //Get y position
  954. return $this->y;
  955. }
  956. function SetY($y)
  957. {
  958. //Set y position and reset x
  959. $this->x=$this->lMargin;
  960. if($y>=0)
  961. $this->y=$y;
  962. else
  963. $this->y=$this->h+$y;
  964. }
  965. function SetXY($x,$y)
  966. {
  967. //Set x and y positions
  968. $this->SetY($y);
  969. $this->SetX($x);
  970. }
  971. function Output($name='',$dest='')
  972. {
  973. //Output PDF to some destination
  974. //Finish document if necessary
  975. if($this->state<3)
  976. $this->Close();
  977. //Normalize parameters
  978. if(is_bool($dest))
  979. $dest=$dest ? 'D' : 'F';
  980. $dest=strtoupper($dest);
  981. if($dest=='')
  982. {
  983. if($name=='')
  984. {
  985. $name='doc.pdf';
  986. $dest='I';
  987. }
  988. else
  989. $dest='F';
  990. }
  991. switch($dest)
  992. {
  993. case 'I':
  994. //Send to standard output
  995. if(ob_get_contents())
  996. $this->Error('Some data has already been output, can\'t send PDF file');
  997. if(php_sapi_name()!='cli')
  998. {
  999. //We send to a browser
  1000. header('Content-Type: application/pdf');
  1001. if(headers_sent())
  1002. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  1003. header('Content-Length: '.strlen($this->buffer));
  1004. header('Content-disposition: inline; filename="'.$name.'"');
  1005. }
  1006. echo $this->buffer;
  1007. break;
  1008. case 'D':
  1009. //Download file
  1010. if(ob_get_contents())
  1011. $this->Error('Some data has already been output, can\'t send PDF file');
  1012. if(isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'],'MSIE'))
  1013. header('Content-Type: application/force-download');
  1014. else
  1015. header('Content-Type: application/octet-stream');
  1016. if(headers_sent())
  1017. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  1018. header('Content-Length: '.strlen($this->buffer));
  1019. header('Content-disposition: attachment; filename="'.$name.'"');
  1020. echo $this->buffer;
  1021. break;
  1022. case 'F':
  1023. //Save to local file
  1024. $f=fopen($name,'wb');
  1025. if(!$f)
  1026. $this->Error('Unable to create output file: '.$name);
  1027. fwrite($f,$this->buffer,strlen($this->buffer));
  1028. fclose($f);
  1029. break;
  1030. case 'S':
  1031. //Return as a string
  1032. return $this->buffer;
  1033. default:
  1034. $this->Error('Incorrect output destination: '.$dest);
  1035. }
  1036. return '';
  1037. }
  1038. /*******************************************************************************
  1039. * *
  1040. * Protected methods *
  1041. * *
  1042. *******************************************************************************/
  1043. function _dochecks()
  1044. {
  1045. //Check for locale-related bug
  1046. if(1.1==1)
  1047. $this->Error('Don\'t alter the locale before including class file');
  1048. //Check for decimal separator
  1049. if(sprintf('%.1f',1.0)!='1.0')
  1050. setlocale(LC_NUMERIC,'C');
  1051. }
  1052. function _getfontpath()
  1053. {
  1054. if(!defined('FPDF_FONTPATH') && is_dir(dirname(__FILE__).'/font'))
  1055. define('FPDF_FONTPATH',dirname(__FILE__).'/font/');
  1056. return defined('FPDF_FONTPATH') ? FPDF_FONTPATH : '';
  1057. }
  1058. function _putpages()
  1059. {
  1060. $nb=$this->page;
  1061. if(!empty($this->AliasNbPages))
  1062. {
  1063. //Replace number of pages
  1064. for($n=1;$n<=$nb;$n++)
  1065. $this->pages[$n]=str_replace($this->AliasNbPages,$nb,$this->pages[$n]);
  1066. }
  1067. if($this->DefOrientation=='P')
  1068. {
  1069. $wPt=$this->fwPt;
  1070. $hPt=$this->fhPt;
  1071. }
  1072. else
  1073. {
  1074. $wPt=$this->fhPt;
  1075. $hPt=$this->fwPt;
  1076. }
  1077. $filter=($this->compress) ? '/Filter /FlateDecode ' : '';
  1078. for($n=1;$n<=$nb;$n++)
  1079. {
  1080. //Page
  1081. $this->_newobj();
  1082. $this->_out('<</Type /Page');
  1083. $this->_out('/Parent 1 0 R');
  1084. if(isset($this->OrientationChanges[$n]))
  1085. $this->_out(sprintf('/MediaBox [0 0 %.2f %.2f]',$hPt,$wPt));
  1086. $this->_out('/Resources 2 0 R');
  1087. if(isset($this->PageLinks[$n]))
  1088. {
  1089. //Links
  1090. $annots='/Annots [';
  1091. foreach($this->PageLinks[$n] as $pl)
  1092. {
  1093. $rect=sprintf('%.2f %.2f %.2f %.2f',$pl[0],$pl[1],$pl[0]+$pl[2],$pl[1]-$pl[3]);
  1094. $annots.='<</Type /Annot /Subtype /Link /Rect ['.$rect.'] /Border [0 0 0] ';
  1095. if(is_string($pl[4]))
  1096. $annots.='/A <</S /URI /URI '.$this->_textstring($pl[4]).'>>>>';
  1097. else
  1098. {
  1099. $l=$this->links[$pl[4]];
  1100. $h=isset($this->OrientationChanges[$l[0]]) ? $wPt : $hPt;
  1101. $annots.=sprintf('/Dest [%d 0 R /XYZ 0 %.2f null]>>',1+2*$l[0],$h-$l[1]*$this->k);
  1102. }
  1103. }
  1104. $this->_out($annots.']');
  1105. }
  1106. $this->_out('/Contents '.($this->n+1).' 0 R>>');
  1107. $this->_out('endobj');
  1108. //Page content
  1109. $p=($this->compress) ? gzcompress($this->pages[$n]) : $this->pages[$n];
  1110. $this->_newobj();
  1111. $this->_out('<<'.$filter.'/Length '.strlen($p).'>>');
  1112. $this->_putstream($p);
  1113. $this->_out('endobj');
  1114. }
  1115. //Pages root
  1116. $this->offsets[1]=strlen($this->buffer);
  1117. $this->_out('1 0 obj');
  1118. $this->_out('<</Type /Pages');
  1119. $kids='/Kids [';
  1120. for($i=0;$i<$nb;$i++)
  1121. $kids.=(3+2*$i).' 0 R ';
  1122. $this->_out($kids.']');
  1123. $this->_out('/Count '.$nb);
  1124. $this->_out(sprintf('/MediaBox [0 0 %.2f %.2f]',$wPt,$hPt));
  1125. $this->_out('>>');
  1126. $this->_out('endobj');
  1127. }
  1128. function _putfonts()
  1129. {
  1130. $nf=$this->n;
  1131. foreach($this->diffs as $diff)
  1132. {
  1133. //Encodings
  1134. $this->_newobj();
  1135. $this->_out('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.']>>');
  1136. $this->_out('endobj');
  1137. }
  1138. $mqr=get_magic_quotes_runtime();
  1139. //set_magic_quotes_runtime(0);
  1140. foreach($this->FontFiles as $file=>$info)
  1141. {
  1142. //Font file embedding
  1143. $this->_newobj();
  1144. $this->FontFiles[$file]['n']=$this->n;
  1145. $font='';
  1146. $f=fopen($this->_getfontpath().$file,'rb',1);
  1147. if(!$f)
  1148. $this->Error('Font file not found');
  1149. while(!feof($f))
  1150. $font.=fread($f,8192);
  1151. fclose($f);
  1152. $compressed=(substr($file,-2)=='.z');
  1153. if(!$compressed && isset($info['length2']))
  1154. {
  1155. $header=(ord($font{0})==128);
  1156. if($header)
  1157. {
  1158. //Strip first binary header
  1159. $font=substr($font,6);
  1160. }
  1161. if($header && ord($font{$info['length1']})==128)
  1162. {
  1163. //Strip second binary header
  1164. $font=substr($font,0,$info['length1']).substr($font,$info['length1']+6);
  1165. }
  1166. }
  1167. $this->_out('<</Length '.strlen($font));
  1168. if($compressed)
  1169. $this->_out('/Filter /FlateDecode');
  1170. $this->_out('/Length1 '.$info['length1']);
  1171. if(isset($info['length2']))
  1172. $this->_out('/Length2 '.$info['length2'].' /Length3 0');
  1173. $this->_out('>>');
  1174. $this->_putstream($font);
  1175. $this->_out('endobj');
  1176. }
  1177. //set_magic_quotes_runtime($mqr);
  1178. foreach($this->fonts as $k=>$font)
  1179. {
  1180. //Font objects
  1181. $this->fonts[$k]['n']=$this->n+1;
  1182. $type=$font['type'];
  1183. $name=$font['name'];
  1184. if($type=='core')
  1185. {
  1186. //Standard font
  1187. $this->_newobj();
  1188. $this->_out('<</Type /Font');
  1189. $this->_out('/BaseFont /'.$name);
  1190. $this->_out('/Subtype /Type1');
  1191. if($name!='Symbol' && $name!='ZapfDingbats')
  1192. $this->_out('/Encoding /WinAnsiEncoding');
  1193. $this->_out('>>');
  1194. $this->_out('endobj');
  1195. }
  1196. elseif($type=='Type1' || $type=='TrueType')
  1197. {
  1198. //Additional Type1 or TrueType font
  1199. $this->_newobj();
  1200. $this->_out('<</Type /Font');
  1201. $this->_out('/BaseFont /'.$name);
  1202. $this->_out('/Subtype /'.$type);
  1203. $this->_out('/FirstChar 32 /LastChar 255');
  1204. $this->_out('/Widths '.($this->n+1).' 0 R');
  1205. $this->_out('/FontDescriptor '.($this->n+2).' 0 R');
  1206. if($font['enc'])
  1207. {
  1208. if(isset($font['diff']))
  1209. $this->_out('/Encoding '.($nf+$font['diff']).' 0 R');
  1210. else
  1211. $this->_out('/Encoding /WinAnsiEncoding');
  1212. }
  1213. $this->_out('>>');
  1214. $this->_out('endobj');
  1215. //Widths
  1216. $this->_newobj();
  1217. $cw=&$font['cw'];
  1218. $s='[';
  1219. for($i=32;$i<=255;$i++)
  1220. $s.=$cw[chr($i)].' ';
  1221. $this->_out($s.']');
  1222. $this->_out('endobj');
  1223. //Descriptor
  1224. $this->_newobj();
  1225. $s='<</Type /FontDescriptor /FontName /'.$name;
  1226. foreach($font['desc'] as $k=>$v)
  1227. $s.=' /'.$k.' '.$v;
  1228. $file=$font['file'];
  1229. if($file)
  1230. $s.=' /FontFile'.($type=='Type1' ? '' : '2').' '.$this->FontFiles[$file]['n'].' 0 R';
  1231. $this->_out($s.'>>');
  1232. $this->_out('endobj');
  1233. }
  1234. else
  1235. {
  1236. //Allow for additional types
  1237. $mtd='_put'.strtolower($type);
  1238. if(!method_exists($this,$mtd))
  1239. $this->Error('Unsupported font type: '.$type);
  1240. $this->$mtd($font);
  1241. }
  1242. }
  1243. }
  1244. function _putimages()
  1245. {
  1246. $filter=($this->compress) ? '/Filter /FlateDecode ' : '';
  1247. reset($this->images);
  1248. while(list($file,$info)=each($this->images))
  1249. {
  1250. $this->_newobj();
  1251. $this->images[$file]['n']=$this->n;
  1252. $this->_out('<</Type /XObject');
  1253. $this->_out('/Subtype /Image');
  1254. $this->_out('/Width '.$info['w']);
  1255. $this->_out('/Height '.$info['h']);
  1256. if($info['cs']=='Indexed')
  1257. $this->_out('/ColorSpace [/Indexed /DeviceRGB '.(strlen($info['pal'])/3-1).' '.($this->n+1).' 0 R]');
  1258. else
  1259. {
  1260. $this->_out('/ColorSpace /'.$info['cs']);
  1261. if($info['cs']=='DeviceCMYK')
  1262. $this->_out('/Decode [1 0 1 0 1 0 1 0]');
  1263. }
  1264. $this->_out('/BitsPerComponent '.$info['bpc']);
  1265. if(isset($info['f']))
  1266. $this->_out('/Filter /'.$info['f']);
  1267. if(isset($info['parms']))
  1268. $this->_out($info['parms']);
  1269. if(isset($info['trns']) && is_array($info['trns']))
  1270. {
  1271. $trns='';
  1272. for($i=0;$i<count($info['trns']);$i++)
  1273. $trns.=$info['trns'][$i].' '.$info['trns'][$i].' ';
  1274. $this->_out('/Mask ['.$trns.']');
  1275. }
  1276. $this->_out('/Length '.strlen($info['data']).'>>');
  1277. $this->_putstream($info['data']);
  1278. unset($this->images[$file]['data']);
  1279. $this->_out('endobj');
  1280. //Palette
  1281. if($info['cs']=='Indexed')
  1282. {
  1283. $this->_newobj();
  1284. $pal=($this->compress) ? gzcompress($info['pal']) : $info['pal'];
  1285. $this->_out('<<'.$filter.'/Length '.strlen($pal).'>>');
  1286. $this->_putstream($pal);
  1287. $this->_out('endobj');
  1288. }
  1289. }
  1290. }
  1291. function _putxobjectdict()
  1292. {
  1293. foreach($this->images as $image)
  1294. $this->_out('/I'.$image['i'].' '.$image['n'].' 0 R');
  1295. }
  1296. function _putresourcedict()
  1297. {
  1298. $this->_out('/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]');
  1299. $this->_out('/Font <<');
  1300. foreach($this->fonts as $font)
  1301. $this->_out('/F'.$font['i'].' '.$font['n'].' 0 R');
  1302. $this->_out('>>');
  1303. $this->_out('/XObject <<');
  1304. $this->_putxobjectdict();
  1305. $this->_out('>>');
  1306. }
  1307. function _putresources()
  1308. {
  1309. $this->_putfonts();
  1310. $this->_putimages();
  1311. //Resource dictionary
  1312. $this->offsets[2]=strlen($this->buffer);
  1313. $this->_out('2 0 obj');
  1314. $this->_out('<<');
  1315. $this->_putresourcedict();
  1316. $this->_out('>>');
  1317. $this->_out('endobj');
  1318. }
  1319. function _putinfo()
  1320. {
  1321. $this->_out('/Producer '.$this->_textstring('FPDF '.FPDF_VERSION));
  1322. if(!empty($this->title))
  1323. $this->_out('/Title '.$this->_textstring($this->title));
  1324. if(!empty($this->subject))
  1325. $this->_out('/Subject '.$this->_textstring($this->subject));
  1326. if(!empty($this->author))
  1327. $this->_out('/Author '.$this->_textstring($this->author));
  1328. if(!empty($this->keywords))
  1329. $this->_out('/Keywords '.$this->_textstring($this->keywords));
  1330. if(!empty($this->creator))
  1331. $this->_out('/Creator '.$this->_textstring($this->creator));
  1332. $this->_out('/CreationDate '.$this->_textstring('D:'.date('YmdHis')));
  1333. }
  1334. function _putcatalog()
  1335. {
  1336. $this->_out('/Type /Catalog');
  1337. $this->_out('/Pages 1 0 R');
  1338. if($this->ZoomMode=='fullpage')
  1339. $this->_out('/OpenAction [3 0 R /Fit]');
  1340. elseif($this->ZoomMode=='fullwidth')
  1341. $this->_out('/OpenAction [3 0 R /FitH null]');
  1342. elseif($this->ZoomMode=='real')
  1343. $this->_out('/OpenAction [3 0 R /XYZ null null 1]');
  1344. elseif(!is_string($this->ZoomMode))
  1345. $this->_out('/OpenAction [3 0 R /XYZ null null '.($this->ZoomMode/100).']');
  1346. if($this->LayoutMode=='single')
  1347. $this->_out('/PageLayout /SinglePage');
  1348. elseif($this->LayoutMode=='continuous')
  1349. $this->_out('/PageLayout /OneColumn');
  1350. elseif($this->LayoutMode=='two')
  1351. $this->_out('/PageLayout /TwoColumnLeft');
  1352. }
  1353. function _putheader()
  1354. {
  1355. $this->_out('%PDF-'.$this->PDFVersion);
  1356. }
  1357. function _puttrailer()
  1358. {
  1359. $this->_out('/Size '.($this->n+1));
  1360. $this->_out('/Root '.$this->n.' 0 R');
  1361. $this->_out('/Info '.($this->n-1).' 0 R');
  1362. }
  1363. function _enddoc()
  1364. {
  1365. $this->_putheader();
  1366. $this->_putpages();
  1367. $this->_putresources();
  1368. //Info
  1369. $this->_newobj();
  1370. $this->_out('<<');
  1371. $this->_putinfo();
  1372. $this->_out('>>');
  1373. $this->_out('endobj');
  1374. //Catalog
  1375. $this->_newobj();
  1376. $this->_out('<<');
  1377. $this->_putcatalog();
  1378. $this->_out('>>');
  1379. $this->_out('endobj');
  1380. //Cross-ref
  1381. $o=strlen($this->buffer);
  1382. $this->_out('xref');
  1383. $this->_out('0 '.($this->n+1));
  1384. $this->_out('0000000000 65535 f ');
  1385. for($i=1;$i<=$this->n;$i++)
  1386. $this->_out(sprintf('%010d 00000 n ',$this->offsets[$i]));
  1387. //Trailer
  1388. $this->_out('trailer');
  1389. $this->_out('<<');
  1390. $this->_puttrailer();
  1391. $this->_out('>>');
  1392. $this->_out('startxref');
  1393. $this->_out($o);
  1394. $this->_out('%%EOF');
  1395. $this->state=3;
  1396. }
  1397. function _beginpage($orientation)
  1398. {
  1399. $this->page++;
  1400. $this->pages[$this->page]='';
  1401. $this->state=2;
  1402. $this->x=$this->lMargin;
  1403. $this->y=$this->tMargin;
  1404. $this->FontFamily='';
  1405. //Page orientation
  1406. if(!$orientation)
  1407. $orientation=$this->DefOrientation;
  1408. else
  1409. {
  1410. $orientation=strtoupper($orientation{0});
  1411. if($orientation!=$this->DefOrientation)
  1412. $this->OrientationChanges[$this->page]=true;
  1413. }
  1414. if($orientation!=$this->CurOrientation)
  1415. {
  1416. //Change orientation
  1417. if($orientation=='P')
  1418. {
  1419. $this->wPt=$this->fwPt;
  1420. $this->hPt=$this->fhPt;
  1421. $this->w=$this->fw;
  1422. $this->h=$this->fh;
  1423. }
  1424. else
  1425. {
  1426. $this->wPt=$this->fhPt;
  1427. $this->hPt=$this->fwPt;
  1428. $this->w=$this->fh;
  1429. $this->h=$this->fw;
  1430. }
  1431. $this->PageBreakTrigger=$this->h-$this->bMargin;
  1432. $this->CurOrientation=$orientation;
  1433. }
  1434. }
  1435. function _endpage()
  1436. {
  1437. //End of page contents
  1438. $this->state=1;
  1439. }
  1440. function _newobj()
  1441. {
  1442. //Begin a new object
  1443. $this->n++;
  1444. $this->offsets[$this->n]=strlen($this->buffer);
  1445. $this->_out($this->n.' 0 obj');
  1446. }
  1447. function _dounderline($x,$y,$txt)
  1448. {
  1449. //Underline text
  1450. $up=$this->CurrentFont['up'];
  1451. $ut=$this->CurrentFont['ut'];
  1452. $w=$this->GetStringWidth($txt)+$this->ws*substr_count($txt,' ');
  1453. 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);
  1454. }
  1455. function _parsejpg($file)
  1456. {
  1457. //Extract info from a JPEG file
  1458. $a=GetImageSize($file);
  1459. if(!$a)
  1460. $this->Error('Missing or incorrect image file: '.$file);
  1461. if($a[2]!=2)
  1462. $this->Error('Not a JPEG file: '.$file);
  1463. if(!isset($a['channels']) || $a['channels']==3)
  1464. $colspace='DeviceRGB';
  1465. elseif($a['channels']==4)
  1466. $colspace='DeviceCMYK';
  1467. else
  1468. $colspace='DeviceGray';
  1469. $bpc=isset($a['bits']) ? $a['bits'] : 8;
  1470. //Read whole file
  1471. $f=fopen($file,'rb');
  1472. $data='';
  1473. while(!feof($f))
  1474. $data.=fread($f,4096);
  1475. fclose($f);
  1476. return array('w'=>$a[0],'h'=>$a[1],'cs'=>$colspace,'bpc'=>$bpc,'f'=>'DCTDecode','data'=>$data);
  1477. }
  1478. function _parsepng($file)
  1479. {
  1480. //Extract info from a PNG file
  1481. $f=fopen($file,'rb');
  1482. if(!$f)
  1483. $this->Error('Can\'t open image file: '.$file);
  1484. //Check signature
  1485. if(fread($f,8)!=chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10))
  1486. $this->Error('Not a PNG file: '.$file);
  1487. //Read header chunk
  1488. fread($f,4);
  1489. if(fread($f,4)!='IHDR')
  1490. $this->Error('Incorrect PNG file: '.$file);
  1491. $w=$this->_freadint($f);
  1492. $h=$this->_freadint($f);
  1493. $bpc=ord(fread($f,1));
  1494. if($bpc>8)
  1495. $this->Error('16-bit depth not supported: '.$file);
  1496. $ct=ord(fread($f,1));
  1497. if($ct==0)
  1498. $colspace='DeviceGray';
  1499. elseif($ct==2)
  1500. $colspace='DeviceRGB';
  1501. elseif($ct==3)
  1502. $colspace='Indexed';
  1503. else
  1504. $this->Error('Alpha channel not supported: '.$file);
  1505. if(ord(fread($f,1))!=0)
  1506. $this->Error('Unknown compression method: '.$file);
  1507. if(ord(fread($f,1))!=0)
  1508. $this->Error('Unknown filter method: '.$file);
  1509. if(ord(fread($f,1))!=0)
  1510. $this->Error('Interlacing not supported: '.$file);
  1511. fread($f,4);
  1512. $parms='/DecodeParms <</Predictor 15 /Colors '.($ct==2 ? 3 : 1).' /BitsPerComponent '.$bpc.' /Columns '.$w.'>>';
  1513. //Scan chunks looking for palette, transparency and image data
  1514. $pal='';
  1515. $trns='';
  1516. $data='';
  1517. do
  1518. {
  1519. $n=$this->_freadint($f);
  1520. $type=fread($f,4);
  1521. if($type=='PLTE')
  1522. {
  1523. //Read palette
  1524. $pal=fread($f,$n);
  1525. fread($f,4);
  1526. }
  1527. elseif($type=='tRNS')
  1528. {
  1529. //Read transparency info
  1530. $t=fread($f,$n);
  1531. if($ct==0)
  1532. $trns=array(ord(substr($t,1,1)));
  1533. elseif($ct==2)
  1534. $trns=array(ord(substr($t,1,1)),ord(substr($t,3,1)),ord(substr($t,5,1)));
  1535. else
  1536. {
  1537. $pos=strpos($t,chr(0));
  1538. if($pos!==false)
  1539. $trns=array($pos);
  1540. }
  1541. fread($f,4);
  1542. }
  1543. elseif($type=='IDAT')
  1544. {
  1545. //Read image data block
  1546. $data.=fread($f,$n);
  1547. fread($f,4);
  1548. }
  1549. elseif($type=='IEND')
  1550. break;
  1551. else
  1552. fread($f,$n+4);
  1553. }
  1554. while($n);
  1555. if($colspace=='Indexed' && empty($pal))
  1556. $this->Error('Missing palette in '.$file);
  1557. fclose($f);
  1558. return array('w'=>$w,'h'=>$h,'cs'=>$colspace,'bpc'=>$bpc,'f'=>'FlateDecode','parms'=>$parms,'pal'=>$pal,'trns'=>$trns,'data'=>$data);
  1559. }
  1560. function _freadint($f)
  1561. {
  1562. //Read a 4-byte integer from file
  1563. $a=unpack('Ni',fread($f,4));
  1564. return $a['i'];
  1565. }
  1566. function _textstring($s)
  1567. {
  1568. //Format a text string
  1569. return '('.$this->_escape($s).')';
  1570. }
  1571. function _escape($s)
  1572. {
  1573. //Add \ before \, ( and )
  1574. return str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$s)));
  1575. }
  1576. function _putstream($s)
  1577. {
  1578. $this->_out('stream');
  1579. $this->_out($s);
  1580. $this->_out('endstream');
  1581. }
  1582. function _out($s)
  1583. {
  1584. //Add a line to the document
  1585. if($this->state==2)
  1586. $this->pages[$this->page].=$s."\n";
  1587. else
  1588. $this->buffer.=$s."\n";
  1589. }
  1590. //End of class
  1591. }
  1592. //Handle special IE contype request
  1593. if(isset($_SERVER['HTTP_USER_AGENT']) && $_SERVER['HTTP_USER_AGENT']=='contype')
  1594. {
  1595. header('Content-Type: application/pdf');
  1596. exit;
  1597. }
  1598. }
  1599. ///////////////////////////////////////////////////////
  1600. //// FPDF_TPL START ////
  1601. ///////////////////////////////////////////////////////
  1602. class FPDF_TPL extends FPDF {
  1603. /**
  1604. * Array of Tpl-Data
  1605. * @var array
  1606. */
  1607. var $tpls = array();
  1608. /**
  1609. * Current Template-ID
  1610. * @var int
  1611. */
  1612. var $tpl = 0;
  1613. /**
  1614. * "In Template"-Flag
  1615. * @var boolean
  1616. */
  1617. var $_intpl = false;
  1618. /**
  1619. * Nameprefix of Templates used in Resources-Dictonary
  1620. * @var string A String defining the Prefix used as Template-Object-Names. Have to beginn with an /
  1621. */
  1622. var $tplprefix = "/TPL";
  1623. /**
  1624. * Resources used By Templates and Pages
  1625. * @var array
  1626. */
  1627. var $_res = array();
  1628. /**
  1629. * Constructor
  1630. * See FPDF-Documentation
  1631. * @param string $orientation
  1632. * @param string $unit
  1633. * @param mixed $format
  1634. */
  1635. function fpdf_tpl($orientation='P',$unit='mm',$format='A4') {
  1636. parent::fpdf($orientation,$unit,$format);
  1637. }
  1638. /**
  1639. * Start a Template
  1640. *
  1641. * This method starts a template. You can give own coordinates to build an own sized
  1642. * Template. Pay attention, that the margins are adapted to the new templatesize.
  1643. * If you want to write outside the template, for example to build a clipped Template,
  1644. * you have to set the Margins and "Cursor"-Position manual after beginTemplate-Call.
  1645. *
  1646. * If no parameter is given, the template uses the current page-size.
  1647. * The Method returns an ID of the current Template. This ID is used later for using this template.
  1648. * Warning: A created Template is used in PDF at all events. Still if you don't use it after creation!
  1649. *
  1650. * @param int $x The x-coordinate given in user-unit
  1651. * @param int $y The y-coordinate given in user-unit
  1652. * @param int $w The width given in user-unit
  1653. * @param int $h The height given in user-unit
  1654. * @return int The ID of new created Template
  1655. */
  1656. function beginTemplate($x=null,$y=null,$w=null,$h=null) {
  1657. if ($this->page <= 0)
  1658. $this->error("You have to add a page to fpdf first!");
  1659. if ($x == null)
  1660. $x = 0;
  1661. if ($y == null)
  1662. $y = 0;
  1663. if ($w == null)
  1664. $w = $this->w;
  1665. if ($h == null)
  1666. $h = $this->h;
  1667. // Save settings
  1668. $this->tpl++;
  1669. $tpl =& $this->tpls[$this->tpl];
  1670. $tpl = array(
  1671. 'o_x' => $this->x,
  1672. 'o_y' => $this->y,
  1673. 'o_AutoPageBreak' => $this->AutoPageBreak,
  1674. 'o_bMargin' => $this->bMargin,
  1675. 'o_tMargin' => $this->tMargin,
  1676. 'o_lMargin' => $this->lMargin,
  1677. 'o_rMargin' => $this->rMargin,
  1678. 'o_h' => $this->h,
  1679. 'o_w' => $this->w,
  1680. 'buffer' => '',
  1681. 'x' => $x,
  1682. 'y' => $y,
  1683. 'w' => $w,
  1684. 'h' => $h
  1685. );
  1686. $this->SetAutoPageBreak(false);
  1687. // Define own high and width to calculate possitions correct
  1688. $this->h = $h;
  1689. $this->w = $w;
  1690. $this->_intpl = true;
  1691. $this->SetXY($x+$this->lMargin,$y+$this->tMargin);
  1692. $this->SetRightMargin($this->w-$w+$this->rMargin);
  1693. return $this->tpl;
  1694. }
  1695. /**
  1696. * End Template
  1697. *
  1698. * This method ends a template and reset initiated variables on beginTemplate.
  1699. *
  1700. * @return mixed If a template is opened, the ID is returned. If not a false is returned.
  1701. */
  1702. function endTemplate() {
  1703. if ($this->_intpl) {
  1704. $this->_intpl = false;
  1705. $tpl =& $this->tpls[$this->tpl];
  1706. $this->SetXY($tpl['o_x'], $tpl['o_y']);
  1707. $this->tMargin = $tpl['o_tMargin'];
  1708. $this->lMargin = $tpl['o_lMargin'];
  1709. $this->rMargin = $tpl['o_rMargin'];
  1710. $this->h = $tpl['o_h'];
  1711. $this->w = $tpl['o_w'];
  1712. $this->SetAutoPageBreak($tpl['o_AutoPageBreak'], $tpl['o_bMargin']);
  1713. return $this->tpl;
  1714. } else {
  1715. return false;
  1716. }
  1717. }
  1718. /**
  1719. * Use a Template in current Page or other Template
  1720. *
  1721. * You can use a template in a page or in another template.
  1722. * You can give the used template a new size like you use the Image()-method.
  1723. * All parameters are optional. The width or height is calculated automaticaly
  1724. * if one is given. If no parameter is given the origin size as defined in
  1725. * beginTemplate() is used.
  1726. * The calculated or used width and height are returned as an array.
  1727. *
  1728. * @param int $tplidx A valid template-Id
  1729. * @param int $_x The x-position
  1730. * @param int $_y The y-position
  1731. * @param int $_w The new width of the template
  1732. * @param int $_h The new height of the template
  1733. * @retrun array The height and width of the template
  1734. */
  1735. function useTemplate($tplidx, $_x=null, $_y=null, $_w=0, $_h=0) {
  1736. if ($this->page <= 0)
  1737. $this->error("You have to add a page to fpdf first!");
  1738. if (!isset($this->tpls[$tplidx]))
  1739. $this->error("Template does not exist!");
  1740. if ($this->_intpl) {
  1741. $this->_res['tpl'][$this->tpl]['tpls'][$tplidx] =& $this->tpls[$tplidx];
  1742. }
  1743. $tpl =& $this->tpls[$tplidx];
  1744. $x = $tpl['x'];
  1745. $y = $tpl['y'];
  1746. $w = $tpl['w'];
  1747. $h = $tpl['h'];
  1748. if ($_x == null)
  1749. $_x = $x;
  1750. if ($_y == null)
  1751. $_y = $y;
  1752. $wh = $this->getTemplateSize($tplidx,$_w,$_h);
  1753. $_w = $wh['w'];
  1754. $_h = $wh['h'];
  1755. $this->_out(sprintf("q %.4f 0 0 %.4f %.2f %.2f cm", ($_w/$w), ($_h/$h), $_x*$this->k, ($this->h-($_y+$_h))*$this->k)); // Translate
  1756. $this->_out($this->tplprefix.$tplidx." Do Q");
  1757. return array("w" => $_w, "h" => $_h);
  1758. }
  1759. /**
  1760. * Get The calculated Size of a Template
  1761. *
  1762. * If one size is given, this method calculates the other one.
  1763. *
  1764. * @param int $tplidx A valid template-Id
  1765. * @param int $_w The width of the template
  1766. * @param int $_h The height of the template
  1767. * @return array The height and width of the template
  1768. */
  1769. function getTemplateSize($tplidx, $_w=0, $_h=0) {
  1770. if (!$this->tpls[$tplidx])
  1771. return false;
  1772. $tpl =& $this->tpls[$tplidx];
  1773. $w = $tpl['w'];
  1774. $h = $tpl['h'];
  1775. if ($_w == 0 and $_h == 0) {
  1776. $_w = $w;
  1777. $_h = $h;
  1778. }
  1779. if($_w==0)
  1780. $_w=$_h*$w/$h;
  1781. if($_h==0)
  1782. $_h=$_w*$h/$w;
  1783. return array("w" => $_w, "h" => $_h);
  1784. }
  1785. /**
  1786. * See FPDF-Documentation ;-)
  1787. */
  1788. function SetFont($family,$style='',$size=0) {
  1789. /**
  1790. * force the resetting of font changes in a template
  1791. */
  1792. if ($this->_intpl)
  1793. $this->FontFamily = '';
  1794. parent::SetFont($family, $style, $size);
  1795. $fontkey = $this->FontFamily.$this->FontStyle;
  1796. if ($this->_intpl) {
  1797. $this->_res['tpl'][$this->tpl]['fonts'][$fontkey] =& $this->fonts[$fontkey];
  1798. } else {
  1799. $this->_res['page'][$this->page]['fonts'][$fontkey] =& $this->fonts[$fontkey];
  1800. }
  1801. }
  1802. /**
  1803. * See FPDF-Documentation ;-)
  1804. */
  1805. function Image($file,$x,$y,$w=0,$h=0,$type='',$link='') {
  1806. parent::Image($file,$x,$y,$w,$h,$type,$link);
  1807. if ($this->_intpl) {
  1808. $this->_res['tpl'][$this->tpl]['images'][$file] =& $this->images[$file];
  1809. } else {
  1810. $this->_res['page'][$this->page]['images'][$file] =& $this->images[$file];
  1811. }
  1812. }
  1813. /**
  1814. * See FPDF-Documentation ;-)
  1815. *
  1816. * AddPage is not available when you're "in" a template.
  1817. */
  1818. function AddPage($orientation='') {
  1819. if ($this->_intpl)
  1820. $this->Error('Adding pages in templates isn\'t possible!');
  1821. parent::AddPage($orientation);
  1822. }
  1823. /**
  1824. * Preserve adding Links in Templates ...won't work
  1825. */
  1826. function Link($x,$y,$w,$h,$link) {
  1827. if ($this->_intpl)
  1828. $this->Error('Using links in templates aren\'t possible!');
  1829. parent::Link($x,$y,$w,$h,$link);
  1830. }
  1831. function AddLink() {
  1832. if ($this->_intpl)
  1833. $this->Error('Adding links in templates aren\'t possible!');
  1834. return parent::AddLink();
  1835. }
  1836. function SetLink($link,$y=0,$page=-1) {
  1837. if ($this->_intpl)
  1838. $this->Error('Setting links in templates aren\'t possible!');
  1839. parent::SetLink($link,$y,$page);
  1840. }
  1841. /**
  1842. * Private Method that writes the form xobjects
  1843. */
  1844. function _putformxobjects() {
  1845. $filter=($this->compress) ? '/Filter /FlateDecode ' : '';
  1846. reset($this->tpls);
  1847. foreach($this->tpls AS $tplidx => $tpl) {
  1848. $p=($this->compress) ? gzcompress($tpl['buffer']) : $tpl['buffer'];
  1849. $this->_newobj();
  1850. $this->tpls[$tplidx]['n'] = $this->n;
  1851. $this->_out('<<'.$filter.'/Type /XObject');
  1852. $this->_out('/Subtype /Form');
  1853. $this->_out('/FormType 1');
  1854. $this->_out(sprintf('/BBox [%.2f %.2f %.2f %.2f]',$tpl['x']*$this->k, ($tpl['h']-$tpl['y'])*$this->k, $tpl['w']*$this->k, ($tpl['h']-$tpl['y']-$tpl['h'])*$this->k));
  1855. $this->_out('/Resources ');
  1856. $this->_out('<</ProcSet [/PDF /Text /ImageB /ImageC /ImageI]');
  1857. if (isset($this->_res['tpl'][$tplidx]['fonts']) && count($this->_res['tpl'][$tplidx]['fonts'])) {
  1858. $this->_out('/Font <<');
  1859. foreach($this->_res['tpl'][$tplidx]['fonts'] as $font)
  1860. $this->_out('/F'.$font['i'].' '.$font['n'].' 0 R');
  1861. $this->_out('>>');
  1862. }
  1863. if(isset($this->_res['tpl'][$tplidx]['images']) && count($this->_res['tpl'][$tplidx]['images']) ||
  1864. isset($this->_res['tpl'][$tplidx]['tpls']) && count($this->_res['tpl'][$tplidx]['tpls']))
  1865. {
  1866. $this->_out('/XObject <<');
  1867. if (isset($this->_res['tpl'][$tplidx]['images']) && count($this->_res['tpl'][$tplidx]['images'])) {
  1868. foreach($this->_res['tpl'][$tplidx]['images'] as $image)
  1869. $this->_out('/I'.$image['i'].' '.$image['n'].' 0 R');
  1870. }
  1871. if (isset($this->_res['tpl'][$tplidx]['tpls']) && count($this->_res['tpl'][$tplidx]['tpls'])) {
  1872. foreach($this->_res['tpl'][$tplidx]['tpls'] as $i => $tpl)
  1873. $this->_out($this->tplprefix.$i.' '.$tpl['n'].' 0 R');
  1874. }
  1875. $this->_out('>>');
  1876. }
  1877. $this->_out('>>');
  1878. $this->_out('/Length '.strlen($p).' >>');
  1879. $this->_putstream($p);
  1880. $this->_out('endobj');
  1881. }
  1882. }
  1883. /**
  1884. * Private Method
  1885. */
  1886. function _putresources() {
  1887. $this->_putfonts();
  1888. $this->_putimages();
  1889. $this->_putformxobjects();
  1890. //Resource dictionary
  1891. $this->offsets[2]=strlen($this->buffer);
  1892. $this->_out('2 0 obj');
  1893. $this->_out('<<');
  1894. $this->_putresourcedict();
  1895. $this->_out('>>');
  1896. $this->_out('endobj');
  1897. }
  1898. function _putxobjectdict() {
  1899. parent::_putxobjectdict();
  1900. if (count($this->tpls)) {
  1901. foreach($this->tpls as $tplidx => $tpl) {
  1902. $this->_out($this->tplprefix.$tplidx.' '.$tpl['n'].' 0 R');
  1903. }
  1904. }
  1905. }
  1906. /**
  1907. * Private Method
  1908. */
  1909. function _out($s) {
  1910. //Add a line to the document
  1911. if ($this->state==2) {
  1912. if (!$this->_intpl)
  1913. $this->pages[$this->page].=$s."\n";
  1914. else
  1915. $this->tpls[$this->tpl]['buffer'] .= $s."\n";
  1916. } else {
  1917. $this->buffer.=$s."\n";
  1918. }
  1919. }
  1920. }
  1921. ///////////////////////////////////////////////////////
  1922. //// PDF_CONTEXT START ////
  1923. ///////////////////////////////////////////////////////
  1924. class pdf_context {
  1925. var $file;
  1926. var $buffer;
  1927. var $offset;
  1928. var $length;
  1929. var $stack;
  1930. // Constructor
  1931. function pdf_context($f) {
  1932. $this->file = $f;
  1933. $this->reset();
  1934. }
  1935. // Optionally move the file
  1936. // pointer to a new location
  1937. // and reset the buffered data
  1938. function reset($pos = null, $l = 100) {
  1939. if (!is_null ($pos)) {
  1940. fseek ($this->file, $pos);
  1941. }
  1942. $this->buffer = $l > 0 ? fread($this->file, $l) : '';
  1943. $this->offset = 0;
  1944. $this->length = strlen($this->buffer);
  1945. $this->stack = array();
  1946. }
  1947. // Make sure that there is at least one
  1948. // character beyond the current offset in
  1949. // the buffer to prevent the tokenizer
  1950. // from attempting to access data that does
  1951. // not exist
  1952. function ensure_content() {
  1953. if ($this->offset >= $this->length - 1) {
  1954. return $this->increase_length();
  1955. } else {
  1956. return true;
  1957. }
  1958. }
  1959. // Forcefully read more data into the buffer
  1960. function increase_length($l=100) {
  1961. if (feof($this->file)) {
  1962. return false;
  1963. } else {
  1964. $this->buffer .= fread($this->file, $l);
  1965. $this->length = strlen($this->buffer);
  1966. return true;
  1967. }
  1968. }
  1969. }
  1970. ///////////////////////////////////////////////////////
  1971. //// WRAPPER_FUNCTION START ////
  1972. ///////////////////////////////////////////////////////
  1973. if (!defined("PHP_VER_LOWER43"))
  1974. define("PHP_VER_LOWER43", version_compare(PHP_VERSION, "4.3", "<"));
  1975. /**
  1976. * ensure that strspn works correct if php-version < 4.3
  1977. */
  1978. function _strspn($str1, $str2, $start=null, $length=null) {
  1979. $numargs = func_num_args();
  1980. if (PHP_VER_LOWER43 == 1) {
  1981. if (isset($length)) {
  1982. $str1 = substr($str1, $start, $length);
  1983. } else {
  1984. $str1 = substr($str1, $start);
  1985. }
  1986. }
  1987. if ($numargs == 2 || PHP_VER_LOWER43 == 1) {
  1988. return strspn($str1, $str2);
  1989. } else if ($numargs == 3) {
  1990. return strspn($str1, $str2, $start);
  1991. } else {
  1992. return strspn($str1, $str2, $start, $length);
  1993. }
  1994. }
  1995. /**
  1996. * ensure that strcspn works correct if php-version < 4.3
  1997. */
  1998. function _strcspn($str1, $str2, $start=null, $length=null) {
  1999. $numargs = func_num_args();
  2000. if (PHP_VER_LOWER43 == 1) {
  2001. if (isset($length)) {
  2002. $str1 = substr($str1, $start, $length);
  2003. } else {
  2004. $str1 = substr($str1, $start);
  2005. }
  2006. }
  2007. if ($numargs == 2 || PHP_VER_LOWER43 == 1) {
  2008. return strcspn($str1, $str2);
  2009. } else if ($numargs == 3) {
  2010. return strcspn($str1, $str2, $start);
  2011. } else {
  2012. return strcspn($str1, $str2, $start, $length);
  2013. }
  2014. }
  2015. /**
  2016. * ensure that fgets works correct if php-version < 4.3
  2017. */
  2018. function _fgets (&$h, $force=false) {
  2019. $startpos = ftell($h);
  2020. $s = fgets($h, 1024);
  2021. if ((PHP_VER_LOWER43 == 1 || $force) && preg_match("/^([^\r\n]*[\r\n]{1,2})(.)/",trim($s), $ns)) {
  2022. $s = $ns[1];
  2023. fseek($h,$startpos+strlen($s));
  2024. }
  2025. return $s;
  2026. }
  2027. ///////////////////////////////////////////////////////
  2028. //// PDF_PARSER START ////
  2029. ///////////////////////////////////////////////////////
  2030. if (!defined ('PDF_TYPE_NULL'))
  2031. define ('PDF_TYPE_NULL', 0);
  2032. if (!defined ('PDF_TYPE_NUMERIC'))
  2033. define ('PDF_TYPE_NUMERIC', 1);
  2034. if (!defined ('PDF_TYPE_TOKEN'))
  2035. define ('PDF_TYPE_TOKEN', 2);
  2036. if (!defined ('PDF_TYPE_HEX'))
  2037. define ('PDF_TYPE_HEX', 3);
  2038. if (!defined ('PDF_TYPE_STRING'))
  2039. define ('PDF_TYPE_STRING', 4);
  2040. if (!defined ('PDF_TYPE_DICTIONARY'))
  2041. define ('PDF_TYPE_DICTIONARY', 5);
  2042. if (!defined ('PDF_TYPE_ARRAY'))
  2043. define ('PDF_TYPE_ARRAY', 6);
  2044. if (!defined ('PDF_TYPE_OBJDEC'))
  2045. define ('PDF_TYPE_OBJDEC', 7);
  2046. if (!defined ('PDF_TYPE_OBJREF'))
  2047. define ('PDF_TYPE_OBJREF', 8);
  2048. if (!defined ('PDF_TYPE_OBJECT'))
  2049. define ('PDF_TYPE_OBJECT', 9);
  2050. if (!defined ('PDF_TYPE_STREAM'))
  2051. define ('PDF_TYPE_STREAM', 10);
  2052. class pdf_parser {
  2053. /**
  2054. * Filename
  2055. * @var string
  2056. */
  2057. var $filename;
  2058. /**
  2059. * File resource
  2060. * @var resource
  2061. */
  2062. var $f;
  2063. /**
  2064. * PDF Context
  2065. * @var object pdf_context-Instance
  2066. */
  2067. var $c;
  2068. /**
  2069. * xref-Data
  2070. * @var array
  2071. */
  2072. var $xref;
  2073. /**
  2074. * root-Object
  2075. * @var array
  2076. */
  2077. var $root;
  2078. /**
  2079. * Constructor
  2080. *
  2081. * @param string $filename Source-Filename
  2082. */
  2083. function pdf_parser($filename) {
  2084. $this->filename = $filename;
  2085. $this->f = @fopen($this->filename, "rb");
  2086. //../ppsv2/uploads/Fonetik_Fonologi.pdf
  2087. //$this->f = fopen('../ppsv2/uploads/Fonetik_Fonologi.pdf', "rb");
  2088. //var_dump();
  2089. //ar_dump(file_get_contents($this->filename));
  2090. //var_dump($this->f);
  2091. if (!$this->f)
  2092. $this->error(sprintf("Cannot open %s !", $filename));
  2093. $this->getPDFVersion();
  2094. $this->c = new pdf_context($this->f);
  2095. // Read xref-Data
  2096. $this->pdf_read_xref($this->xref, $this->pdf_find_xref());
  2097. // Check for Encryption
  2098. $this->getEncryption();
  2099. // Read root
  2100. $this->pdf_read_root();
  2101. }
  2102. /**
  2103. * Close the opened file
  2104. */
  2105. function closeFile() {
  2106. if (isset($this->f)) {
  2107. fclose($this->f);
  2108. unset($this->f);
  2109. }
  2110. }
  2111. /**
  2112. * Print Error and die
  2113. *
  2114. * @param string $msg Error-Message
  2115. */
  2116. function error($msg) {
  2117. die("<b>PDF-Parser Error:</b> ".$msg);
  2118. }
  2119. /**
  2120. * Check Trailer for Encryption
  2121. */
  2122. function getEncryption() {
  2123. if (isset($this->xref['trailer'][1]['/Encrypt'])) {
  2124. $this->error("File is encrypted!");
  2125. }
  2126. }
  2127. /**
  2128. * Find/Return /Root
  2129. *
  2130. * @return array
  2131. */
  2132. function pdf_find_root() {
  2133. if ($this->xref['trailer'][1]['/Root'][0] != PDF_TYPE_OBJREF) {
  2134. $this->error("Wrong Type of Root-Element! Must be an indirect reference");
  2135. }
  2136. return $this->xref['trailer'][1]['/Root'];
  2137. }
  2138. /**
  2139. * Read the /Root
  2140. */
  2141. function pdf_read_root() {
  2142. // read root
  2143. $this->root = $this->pdf_resolve_object($this->c, $this->pdf_find_root());
  2144. }
  2145. /**
  2146. * Get PDF-Version
  2147. *
  2148. * And reset the PDF Version used in FPDI if needed
  2149. */
  2150. function getPDFVersion() {
  2151. fseek($this->f, 0);
  2152. preg_match("/\d\.\d/",fread($this->f,16),$m);
  2153. $this->pdfVersion = $m[0];
  2154. }
  2155. /**
  2156. * Find the xref-Table
  2157. */
  2158. function pdf_find_xref() {
  2159. fseek ($this->f, -min(filesize($this->filename),1500), SEEK_END);
  2160. $data = fread($this->f, 1500);
  2161. $pos = strlen($data) - strpos(strrev($data), strrev('startxref'));
  2162. $data = substr($data, $pos);
  2163. if (!preg_match('/\s*(\d+).*$/s', $data, $matches)) {
  2164. $this->error("Unable to find pointer to xref table");
  2165. }
  2166. return (int) $matches[1];
  2167. }
  2168. /**
  2169. * Read xref-table
  2170. *
  2171. * @param array $result Array of xref-table
  2172. * @param integer $offset of xref-table
  2173. * @param integer $start start-position in xref-table
  2174. * @param integer $end end-position in xref-table
  2175. */
  2176. function pdf_read_xref(&$result, $offset, $start = null, $end = null) {
  2177. if (is_null ($start) || is_null ($end)) {
  2178. fseek($this->f, $o_pos = $offset);
  2179. $data = trim(fgets($this->f,1024));
  2180. if (strlen($data) == 0)
  2181. $data = trim(fgets($this->f,1024));
  2182. if ($data !== 'xref') {
  2183. fseek($this->f, $o_pos);
  2184. $data = trim(_fgets($this->f, true));
  2185. if ($data !== 'xref') {
  2186. if (preg_match('/(.*xref)(.*)/m', $data, $m)) { // xref 0 128 - in one line
  2187. fseek($this->f, $o_pos+strlen($m[1]));
  2188. } elseif (preg_match('/(x|r|e|f)+/', $data, $m)) { // correct invalid xref-pointer
  2189. $tmpOffset = $offset-4+strlen($m[0]);
  2190. $this->pdf_read_xref($result, $tmpOffset, $start, $end);
  2191. return;
  2192. } else {
  2193. $this->error("Unable to find xref table - Maybe a Problem with 'auto_detect_line_endings'");
  2194. }
  2195. }
  2196. }
  2197. $o_pos = ftell($this->f);
  2198. $data = explode(' ', trim(fgets($this->f,1024)));
  2199. if (count($data) != 2) {
  2200. fseek($this->f, $o_pos);
  2201. $data = explode(' ', trim(_fgets($this->f, true)));
  2202. if (count($data) != 2) {
  2203. if (count($data) > 2) { // no lineending
  2204. $n_pos = $o_pos+strlen($data[0])+strlen($data[1])+2;
  2205. fseek($this->f, $n_pos);
  2206. } else {
  2207. $this->error("Unexpected header in xref table");
  2208. }
  2209. }
  2210. }
  2211. $start = $data[0];
  2212. $end = $start + $data[1];
  2213. }
  2214. if (!isset($result['xref_location'])) {
  2215. $result['xref_location'] = $offset;
  2216. }
  2217. if (!isset($result['max_object']) || $end > $result['max_object']) {
  2218. $result['max_object'] = $end;
  2219. }
  2220. for (; $start < $end; $start++) {
  2221. $data = ltrim(fread($this->f, 20)); // Spezifications says: 20 bytes including newlines
  2222. $offset = substr($data, 0, 10);
  2223. $generation = substr($data, 11, 5);
  2224. if (!isset ($result['xref'][$start][(int) $generation])) {
  2225. $result['xref'][$start][(int) $generation] = (int) $offset;
  2226. }
  2227. }
  2228. $o_pos = ftell($this->f);
  2229. $data = fgets($this->f,1024);
  2230. if (strlen(trim($data)) == 0)
  2231. $data = fgets($this->f, 1024);
  2232. if (preg_match("/trailer/",$data)) {
  2233. if (preg_match("/(.*trailer[ \n\r]*)/",$data,$m)) {
  2234. fseek($this->f, $o_pos+strlen($m[1]));
  2235. }
  2236. $c = new pdf_context($this->f);
  2237. $trailer = $this->pdf_read_value($c);
  2238. if (isset($trailer[1]['/Prev'])) {
  2239. $this->pdf_read_xref($result, $trailer[1]['/Prev'][1]);
  2240. $result['trailer'][1] = array_merge($result['trailer'][1], $trailer[1]);
  2241. } else {
  2242. $result['trailer'] = $trailer;
  2243. }
  2244. } else {
  2245. $data = explode(' ', trim($data));
  2246. if (count($data) != 2) {
  2247. fseek($this->f, $o_pos);
  2248. $data = explode(' ', trim (_fgets ($this->f, true)));
  2249. if (count($data) != 2) {
  2250. $this->error("Unexpected data in xref table");
  2251. }
  2252. }
  2253. $this->pdf_read_xref($result, null, (int) $data[0], (int) $data[0] + (int) $data[1]);
  2254. }
  2255. }
  2256. /**
  2257. * Reads an Value
  2258. *
  2259. * @param object $c pdf_context
  2260. * @param string $token a Token
  2261. * @return mixed
  2262. */
  2263. function pdf_read_value(&$c, $token = null) {
  2264. if (is_null($token)) {
  2265. $token = $this->pdf_read_token($c);
  2266. }
  2267. if ($token === false) {
  2268. return false;
  2269. }
  2270. switch ($token) {
  2271. case '<':
  2272. // This is a hex string.
  2273. // Read the value, then the terminator
  2274. $pos = $c->offset;
  2275. while(1) {
  2276. $match = strpos ($c->buffer, '>', $pos);
  2277. // If you can't find it, try
  2278. // reading more data from the stream
  2279. if ($match === false) {
  2280. if (!$c->increase_length()) {
  2281. return false;
  2282. } else {
  2283. continue;
  2284. }
  2285. }
  2286. $result = substr ($c->buffer, $c->offset, $match - $c->offset);
  2287. $c->offset = $match+1;
  2288. return array (PDF_TYPE_HEX, $result);
  2289. }
  2290. break;
  2291. case '<<':
  2292. // This is a dictionary.
  2293. $result = array();
  2294. // Recurse into this function until we reach
  2295. // the end of the dictionary.
  2296. while (($key = $this->pdf_read_token($c)) !== '>>') {
  2297. if ($key === false) {
  2298. return false;
  2299. }
  2300. if (($value = $this->pdf_read_value($c)) === false) {
  2301. return false;
  2302. }
  2303. $result[$key] = $value;
  2304. }
  2305. return array (PDF_TYPE_DICTIONARY, $result);
  2306. case '[':
  2307. // This is an array.
  2308. $result = array();
  2309. // Recurse into this function until we reach
  2310. // the end of the array.
  2311. while (($token = $this->pdf_read_token($c)) !== ']') {
  2312. if ($token === false) {
  2313. return false;
  2314. }
  2315. if (($value = $this->pdf_read_value($c, $token)) === false) {
  2316. return false;
  2317. }
  2318. $result[] = $value;
  2319. }
  2320. return array (PDF_TYPE_ARRAY, $result);
  2321. case '(' :
  2322. // This is a string
  2323. $pos = $c->offset;
  2324. while(1) {
  2325. // Start by finding the next closed
  2326. // parenthesis
  2327. $match = strpos ($c->buffer, ')', $pos);
  2328. // If you can't find it, try
  2329. // reading more data from the stream
  2330. if ($match === false) {
  2331. if (!$c->increase_length()) {
  2332. return false;
  2333. } else {
  2334. continue;
  2335. }
  2336. }
  2337. // Make sure that there is no backslash
  2338. // before the parenthesis. If there is,
  2339. // move on. Otherwise, return the string.
  2340. $esc = preg_match('/([\\\\]+)$/', $tmpresult = substr($c->buffer, $c->offset, $match - $c->offset), $m);
  2341. if ($esc === 0 || strlen($m[1]) % 2 == 0) {
  2342. $result = $tmpresult;
  2343. $c->offset = $match + 1;
  2344. return array (PDF_TYPE_STRING, $result);
  2345. } else {
  2346. $pos = $match + 1;
  2347. if ($pos > $c->offset + $c->length) {
  2348. $c->increase_length();
  2349. }
  2350. }
  2351. }
  2352. case "stream":
  2353. $o_pos = ftell($c->file)-strlen($c->buffer);
  2354. $o_offset = $c->offset;
  2355. $c->reset($startpos = $o_pos + $o_offset);
  2356. $e = 0; // ensure line breaks in front of the stream
  2357. if ($c->buffer[0] == chr(10) || $c->buffer[0] == chr(13))
  2358. $e++;
  2359. if ($c->buffer[1] == chr(10) && $c->buffer[0] != chr(10))
  2360. $e++;
  2361. if ($this->actual_obj[1][1]['/Length'][0] == PDF_TYPE_OBJREF) {
  2362. $tmp_c = new pdf_context($this->f);
  2363. $tmp_length = $this->pdf_resolve_object($tmp_c,$this->actual_obj[1][1]['/Length']);
  2364. $length = $tmp_length[1][1];
  2365. } else {
  2366. $length = $this->actual_obj[1][1]['/Length'][1];
  2367. }
  2368. if ($length > 0) {
  2369. $c->reset($startpos+$e,$length);
  2370. $v = $c->buffer;
  2371. } else {
  2372. $v = '';
  2373. }
  2374. $c->reset($startpos+$e+$length+9); // 9 = strlen("endstream")
  2375. return array(PDF_TYPE_STREAM, $v);
  2376. default :
  2377. if (is_numeric ($token)) {
  2378. // A numeric token. Make sure that
  2379. // it is not part of something else.
  2380. if (($tok2 = $this->pdf_read_token ($c)) !== false) {
  2381. if (is_numeric ($tok2)) {
  2382. // Two numeric tokens in a row.
  2383. // In this case, we're probably in
  2384. // front of either an object reference
  2385. // or an object specification.
  2386. // Determine the case and return the data
  2387. if (($tok3 = $this->pdf_read_token ($c)) !== false) {
  2388. switch ($tok3) {
  2389. case 'obj' :
  2390. return array (PDF_TYPE_OBJDEC, (int) $token, (int) $tok2);
  2391. case 'R' :
  2392. return array (PDF_TYPE_OBJREF, (int) $token, (int) $tok2);
  2393. }
  2394. // If we get to this point, that numeric value up
  2395. // there was just a numeric value. Push the extra
  2396. // tokens back into the stack and return the value.
  2397. array_push ($c->stack, $tok3);
  2398. }
  2399. }
  2400. array_push ($c->stack, $tok2);
  2401. }
  2402. return array (PDF_TYPE_NUMERIC, $token);
  2403. } else {
  2404. // Just a token. Return it.
  2405. return array (PDF_TYPE_TOKEN, $token);
  2406. }
  2407. }
  2408. }
  2409. /**
  2410. * Resolve an object
  2411. *
  2412. * @param object $c pdf_context
  2413. * @param array $obj_spec The object-data
  2414. * @param boolean $encapsulate Must set to true, cause the parsing and fpdi use this method only without this para
  2415. */
  2416. function pdf_resolve_object(&$c, $obj_spec, $encapsulate = true) {
  2417. // Exit if we get invalid data
  2418. if (!is_array($obj_spec)) {
  2419. return false;
  2420. }
  2421. if ($obj_spec[0] == PDF_TYPE_OBJREF) {
  2422. // This is a reference, resolve it
  2423. if (isset($this->xref['xref'][$obj_spec[1]][$obj_spec[2]])) {
  2424. // Save current file position
  2425. // This is needed if you want to resolve
  2426. // references while you're reading another object
  2427. // (e.g.: if you need to determine the length
  2428. // of a stream)
  2429. $old_pos = ftell($c->file);
  2430. // Reposition the file pointer and
  2431. // load the object header.
  2432. $c->reset($this->xref['xref'][$obj_spec[1]][$obj_spec[2]]);
  2433. $header = $this->pdf_read_value($c,null,true);
  2434. if ($header[0] != PDF_TYPE_OBJDEC || $header[1] != $obj_spec[1] || $header[2] != $obj_spec[2]) {
  2435. $this->error("Unable to find object ({$obj_spec[1]}, {$obj_spec[2]}) at expected location");
  2436. }
  2437. // If we're being asked to store all the information
  2438. // about the object, we add the object ID and generation
  2439. // number for later use
  2440. $this->actual_obj =& $result;
  2441. if ($encapsulate) {
  2442. $result = array (
  2443. PDF_TYPE_OBJECT,
  2444. 'obj' => $obj_spec[1],
  2445. 'gen' => $obj_spec[2]
  2446. );
  2447. } else {
  2448. $result = array();
  2449. }
  2450. // Now simply read the object data until
  2451. // we encounter an end-of-object marker
  2452. while(1) {
  2453. $value = $this->pdf_read_value($c);
  2454. if ($value === false || count($result) > 4) {
  2455. // in this case the parser coudn't find an endobj so we break here
  2456. break;
  2457. }
  2458. if ($value[0] == PDF_TYPE_TOKEN && $value[1] === 'endobj') {
  2459. break;
  2460. }
  2461. $result[] = $value;
  2462. }
  2463. $c->reset($old_pos);
  2464. if (isset($result[2][0]) && $result[2][0] == PDF_TYPE_STREAM) {
  2465. $result[0] = PDF_TYPE_STREAM;
  2466. }
  2467. return $result;
  2468. }
  2469. } else {
  2470. return $obj_spec;
  2471. }
  2472. }
  2473. /**
  2474. * Reads a token from the file
  2475. *
  2476. * @param object $c pdf_context
  2477. * @return mixed
  2478. */
  2479. function pdf_read_token(&$c)
  2480. {
  2481. // If there is a token available
  2482. // on the stack, pop it out and
  2483. // return it.
  2484. if (count($c->stack)) {
  2485. return array_pop($c->stack);
  2486. }
  2487. // Strip away any whitespace
  2488. do {
  2489. if (!$c->ensure_content()) {
  2490. return false;
  2491. }
  2492. $c->offset += _strspn($c->buffer, " \n\r\t", $c->offset);
  2493. } while ($c->offset >= $c->length - 1);
  2494. // Get the first character in the stream
  2495. $char = $c->buffer[$c->offset++];
  2496. switch ($char) {
  2497. case '[' :
  2498. case ']' :
  2499. case '(' :
  2500. case ')' :
  2501. // This is either an array or literal string
  2502. // delimiter, Return it
  2503. return $char;
  2504. case '<' :
  2505. case '>' :
  2506. // This could either be a hex string or
  2507. // dictionary delimiter. Determine the
  2508. // appropriate case and return the token
  2509. if ($c->buffer[$c->offset] == $char) {
  2510. if (!$c->ensure_content()) {
  2511. return false;
  2512. }
  2513. $c->offset++;
  2514. return $char . $char;
  2515. } else {
  2516. return $char;
  2517. }
  2518. default :
  2519. // This is "another" type of token (probably
  2520. // a dictionary entry or a numeric value)
  2521. // Find the end and return it.
  2522. if (!$c->ensure_content()) {
  2523. return false;
  2524. }
  2525. while(1) {
  2526. // Determine the length of the token
  2527. $pos = _strcspn($c->buffer, " []<>()\r\n\t/", $c->offset);
  2528. if ($c->offset + $pos <= $c->length - 1) {
  2529. break;
  2530. } else {
  2531. // If the script reaches this point,
  2532. // the token may span beyond the end
  2533. // of the current buffer. Therefore,
  2534. // we increase the size of the buffer
  2535. // and try again--just to be safe.
  2536. $c->increase_length();
  2537. }
  2538. }
  2539. $result = substr($c->buffer, $c->offset - 1, $pos + 1);
  2540. $c->offset += $pos;
  2541. return $result;
  2542. }
  2543. }
  2544. }
  2545. ///////////////////////////////////////////////////////
  2546. //// FPDI_PDF_PARSER START ////
  2547. ///////////////////////////////////////////////////////
  2548. class fpdi_pdf_parser extends pdf_parser {
  2549. /**
  2550. * Pages
  2551. * Index beginns at 0
  2552. *
  2553. * @var array
  2554. */
  2555. var $pages;
  2556. /**
  2557. * Page count
  2558. * @var integer
  2559. */
  2560. var $page_count;
  2561. /**
  2562. * actual page number
  2563. * @var integer
  2564. */
  2565. var $pageno;
  2566. /**
  2567. * PDF Version of imported Document
  2568. * @var string
  2569. */
  2570. var $pdfVersion;
  2571. /**
  2572. * FPDI Reference
  2573. * @var object
  2574. */
  2575. var $fpdi;
  2576. /**
  2577. * Available BoxTypes
  2578. *
  2579. * @var array
  2580. */
  2581. var $availableBoxes = array("/MediaBox","/CropBox","/BleedBox","/TrimBox","/ArtBox");
  2582. /**
  2583. * Constructor
  2584. *
  2585. * @param string $filename Source-Filename
  2586. * @param object $fpdi Object of type fpdi
  2587. */
  2588. function fpdi_pdf_parser($filename,&$fpdi) {
  2589. $this->fpdi =& $fpdi;
  2590. $this->filename = $filename;
  2591. parent::pdf_parser($filename);
  2592. // resolve Pages-Dictonary
  2593. $pages = $this->pdf_resolve_object($this->c, $this->root[1][1]['/Pages']);
  2594. // Read pages
  2595. $this->read_pages($this->c, $pages, $this->pages);
  2596. // count pages;
  2597. $this->page_count = count($this->pages);
  2598. }
  2599. /**
  2600. * Overwrite parent::error()
  2601. *
  2602. * @param string $msg Error-Message
  2603. */
  2604. function error($msg) {
  2605. $this->fpdi->error($msg);
  2606. }
  2607. /**
  2608. * Get pagecount from sourcefile
  2609. *
  2610. * @return int
  2611. */
  2612. function getPageCount() {
  2613. return $this->page_count;
  2614. }
  2615. /**
  2616. * Set pageno
  2617. *
  2618. * @param int $pageno Pagenumber to use
  2619. */
  2620. function setPageno($pageno) {
  2621. $pageno = ((int) $pageno) - 1;
  2622. if ($pageno < 0 || $pageno >= $this->getPageCount()) {
  2623. $this->fpdi->error("Pagenumber is wrong!");
  2624. }
  2625. $this->pageno = $pageno;
  2626. }
  2627. /**
  2628. * Get page-resources from current page
  2629. *
  2630. * @return array
  2631. */
  2632. function getPageResources() {
  2633. return $this->_getPageResources($this->pages[$this->pageno]);
  2634. }
  2635. /**
  2636. * Get page-resources from /Page
  2637. *
  2638. * @param array $obj Array of pdf-data
  2639. */
  2640. function _getPageResources ($obj) { // $obj = /Page
  2641. $obj = $this->pdf_resolve_object($this->c, $obj);
  2642. // If the current object has a resources
  2643. // dictionary associated with it, we use
  2644. // it. Otherwise, we move back to its
  2645. // parent object.
  2646. if (isset ($obj[1][1]['/Resources'])) {
  2647. $res = $this->pdf_resolve_object($this->c, $obj[1][1]['/Resources']);
  2648. if ($res[0] == PDF_TYPE_OBJECT)
  2649. return $res[1];
  2650. return $res;
  2651. } else {
  2652. if (!isset ($obj[1][1]['/Parent'])) {
  2653. return false;
  2654. } else {
  2655. $res = $this->_getPageResources($obj[1][1]['/Parent']);
  2656. if ($res[0] == PDF_TYPE_OBJECT)
  2657. return $res[1];
  2658. return $res;
  2659. }
  2660. }
  2661. }
  2662. /**
  2663. * Get content of current page
  2664. *
  2665. * If more /Contents is an array, the streams are concated
  2666. *
  2667. * @return string
  2668. */
  2669. function getContent() {
  2670. $buffer = "";
  2671. if (isset($this->pages[$this->pageno][1][1]['/Contents'])) {
  2672. $contents = $this->_getPageContent($this->pages[$this->pageno][1][1]['/Contents']);
  2673. foreach($contents AS $tmp_content) {
  2674. $buffer .= $this->_rebuildContentStream($tmp_content).' ';
  2675. }
  2676. }
  2677. return $buffer;
  2678. }
  2679. /**
  2680. * Resolve all content-objects
  2681. *
  2682. * @param array $content_ref
  2683. * @return array
  2684. */
  2685. function _getPageContent($content_ref) {
  2686. $contents = array();
  2687. if ($content_ref[0] == PDF_TYPE_OBJREF) {
  2688. $content = $this->pdf_resolve_object($this->c, $content_ref);
  2689. if ($content[1][0] == PDF_TYPE_ARRAY) {
  2690. $contents = $this->_getPageContent($content[1]);
  2691. } else {
  2692. $contents[] = $content;
  2693. }
  2694. } else if ($content_ref[0] == PDF_TYPE_ARRAY) {
  2695. foreach ($content_ref[1] AS $tmp_content_ref) {
  2696. $contents = array_merge($contents,$this->_getPageContent($tmp_content_ref));
  2697. }
  2698. }
  2699. return $contents;
  2700. }
  2701. /**
  2702. * Rebuild content-streams
  2703. *
  2704. * @param array $obj
  2705. * @return string
  2706. */
  2707. function _rebuildContentStream($obj) {
  2708. $filters = array();
  2709. if (isset($obj[1][1]['/Filter'])) {
  2710. $_filter = $obj[1][1]['/Filter'];
  2711. if ($_filter[0] == PDF_TYPE_TOKEN) {
  2712. $filters[] = $_filter;
  2713. } else if ($_filter[0] == PDF_TYPE_ARRAY) {
  2714. $filters = $_filter[1];
  2715. }
  2716. }
  2717. $stream = $obj[2][1];
  2718. foreach ($filters AS $_filter) {
  2719. switch ($_filter[1]) {
  2720. case "/FlateDecode":
  2721. if (function_exists('gzuncompress')) {
  2722. $stream = (strlen($stream) > 0) ? @gzuncompress($stream) : '';
  2723. } else {
  2724. $this->fpdi->error(sprintf("To handle %s filter, please compile php with zlib support.",$_filter[1]));
  2725. }
  2726. if ($stream === false) {
  2727. $this->fpdi->error("Error while decompressing stream.");
  2728. }
  2729. break;
  2730. case null:
  2731. $stream = $stream;
  2732. break;
  2733. default:
  2734. if (preg_match("/^\/[a-z85]*$/i", $_filter[1], $filterName) && @include_once('decoders'.$_filter[1].'.php')) {
  2735. $filterName = substr($_filter[1],1);
  2736. if (class_exists($filterName)) {
  2737. $decoder = new $filterName($this->fpdi);
  2738. $stream = $decoder->decode(trim($stream));
  2739. } else {
  2740. $this->fpdi->error(sprintf("Unsupported Filter: %s",$_filter[1]));
  2741. }
  2742. } else {
  2743. $this->fpdi->error(sprintf("Unsupported Filter: %s",$_filter[1]));
  2744. }
  2745. }
  2746. }
  2747. return $stream;
  2748. }
  2749. /**
  2750. * Get a Box from a page
  2751. * Arrayformat is same as used by fpdf_tpl
  2752. *
  2753. * @param array $page a /Page
  2754. * @param string $box_index Type of Box @see $availableBoxes
  2755. * @return array
  2756. */
  2757. function getPageBox($page, $box_index) {
  2758. $page = $this->pdf_resolve_object($this->c,$page);
  2759. $box = null;
  2760. if (isset($page[1][1][$box_index]))
  2761. $box =& $page[1][1][$box_index];
  2762. if (!is_null($box) && $box[0] == PDF_TYPE_OBJREF) {
  2763. $tmp_box = $this->pdf_resolve_object($this->c,$box);
  2764. $box = $tmp_box[1];
  2765. }
  2766. if (!is_null($box) && $box[0] == PDF_TYPE_ARRAY) {
  2767. $b =& $box[1];
  2768. return array("x" => $b[0][1]/$this->fpdi->k,
  2769. "y" => $b[1][1]/$this->fpdi->k,
  2770. "w" => abs($b[0][1]-$b[2][1])/$this->fpdi->k,
  2771. "h" => abs($b[1][1]-$b[3][1])/$this->fpdi->k);
  2772. } else if (!isset ($page[1][1]['/Parent'])) {
  2773. return false;
  2774. } else {
  2775. return $this->getPageBox($this->pdf_resolve_object($this->c, $page[1][1]['/Parent']), $box_index);
  2776. }
  2777. }
  2778. function getPageBoxes($pageno) {
  2779. return $this->_getPageBoxes($this->pages[$pageno-1]);
  2780. }
  2781. /**
  2782. * Get all Boxes from /Page
  2783. *
  2784. * @param array a /Page
  2785. * @return array
  2786. */
  2787. function _getPageBoxes($page) {
  2788. $boxes = array();
  2789. foreach($this->availableBoxes AS $box) {
  2790. if ($_box = $this->getPageBox($page,$box)) {
  2791. $boxes[$box] = $_box;
  2792. }
  2793. }
  2794. return $boxes;
  2795. }
  2796. function getPageRotation($pageno) {
  2797. return $this->_getPageRotation($this->pages[$pageno-1]);
  2798. }
  2799. function _getPageRotation ($obj) { // $obj = /Page
  2800. $obj = $this->pdf_resolve_object($this->c, $obj);
  2801. if (isset ($obj[1][1]['/Rotate'])) {
  2802. $res = $this->pdf_resolve_object($this->c, $obj[1][1]['/Rotate']);
  2803. if ($res[0] == PDF_TYPE_OBJECT)
  2804. return $res[1];
  2805. return $res;
  2806. } else {
  2807. if (!isset ($obj[1][1]['/Parent'])) {
  2808. return false;
  2809. } else {
  2810. $res = $this->_getPageRotation($obj[1][1]['/Parent']);
  2811. if ($res[0] == PDF_TYPE_OBJECT)
  2812. return $res[1];
  2813. return $res;
  2814. }
  2815. }
  2816. }
  2817. /**
  2818. * Read all /Page(es)
  2819. *
  2820. * @param object pdf_context
  2821. * @param array /Pages
  2822. * @param array the result-array
  2823. */
  2824. function read_pages (&$c, &$pages, &$result) {
  2825. // Get the kids dictionary
  2826. $kids = $this->pdf_resolve_object ($c, $pages[1][1]['/Kids']);
  2827. if (!is_array($kids))
  2828. $this->fpdi->Error("Cannot find /Kids in current /Page-Dictionary");
  2829. foreach ($kids[1] as $v) {
  2830. $pg = $this->pdf_resolve_object ($c, $v);
  2831. if ($pg[1][1]['/Type'][1] === '/Pages') {
  2832. // If one of the kids is an embedded
  2833. // /Pages array, resolve it as well.
  2834. $this->read_pages ($c, $pg, $result);
  2835. } else {
  2836. $result[] = $pg;
  2837. }
  2838. }
  2839. }
  2840. /**
  2841. * Get PDF-Version
  2842. *
  2843. * And reset the PDF Version used in FPDI if needed
  2844. */
  2845. function getPDFVersion() {
  2846. parent::getPDFVersion();
  2847. if (isset($this->fpdi->importVersion) && $this->pdfVersion > $this->fpdi->importVersion) {
  2848. $this->fpdi->importVersion = $this->pdfVersion;
  2849. }
  2850. }
  2851. }
  2852. ///////////////////////////////////////////////////////
  2853. //// FPDI START ////
  2854. ///////////////////////////////////////////////////////
  2855. //
  2856. // FPDI - Version 1.2
  2857. //
  2858. // Copyright 2004-2007 Setasign - Jan Slabon
  2859. //
  2860. // Licensed under the Apache License, Version 2.0 (the "License");
  2861. // you may not use this file except in compliance with the License.
  2862. // You may obtain a copy of the License at
  2863. //
  2864. // http://www.apache.org/licenses/LICENSE-2.0
  2865. //
  2866. // Unless required by applicable law or agreed to in writing, software
  2867. // distributed under the License is distributed on an "AS IS" BASIS,
  2868. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  2869. // See the License for the specific language governing permissions and
  2870. // limitations under the License.
  2871. //
  2872. define('FPDI_VERSION','1.2');
  2873. ini_set('auto_detect_line_endings',1); // Strongly required!
  2874. //require_once("fpdf_tpl.php");
  2875. //require('fpdi_pdf_parser.php');
  2876. //require_once("fpdi_pdf_parser.php");
  2877. class FPDI extends FPDF_TPL {
  2878. /**
  2879. * Actual filename
  2880. * @var string
  2881. */
  2882. var $current_filename;
  2883. /**
  2884. * Parser-Objects
  2885. * @var array
  2886. */
  2887. var $parsers;
  2888. /**
  2889. * Current parser
  2890. * @var object
  2891. */
  2892. var $current_parser;
  2893. /**
  2894. * Highest version of imported PDF
  2895. * @var double
  2896. */
  2897. var $importVersion = 1.3;
  2898. /**
  2899. * object stack
  2900. * @var array
  2901. */
  2902. var $_obj_stack;
  2903. /**
  2904. * done object stack
  2905. * @var array
  2906. */
  2907. var $_don_obj_stack;
  2908. /**
  2909. * Current Object Id.
  2910. * @var integer
  2911. */
  2912. var $_current_obj_id;
  2913. /**
  2914. * The name of the last imported page box
  2915. * @var string
  2916. */
  2917. var $lastUsedPageBox;
  2918. /**
  2919. * Constructor
  2920. * See FPDF-Manual
  2921. */
  2922. function FPDI($orientation='P',$unit='mm',$format='A4') {
  2923. parent::FPDF_TPL($orientation,$unit,$format);
  2924. }
  2925. /**
  2926. * Set a source-file
  2927. *
  2928. * @param string $filename a valid filename
  2929. * @return int number of available pages
  2930. */
  2931. function setSourceFile($filename) {
  2932. $this->current_filename = $filename;
  2933. $fn =& $this->current_filename;
  2934. if (!isset($this->parsers[$fn]))
  2935. $this->parsers[$fn] = new fpdi_pdf_parser($fn,$this);
  2936. $this->current_parser = $this->parsers[$fn];
  2937. return $this->parsers[$fn]->getPageCount();
  2938. }
  2939. /**
  2940. * Import a page
  2941. *
  2942. * @param int $pageno pagenumber
  2943. * @return int Index of imported page - to use with fpdf_tpl::useTemplate()
  2944. */
  2945. function importPage($pageno, $boxName='/CropBox') {
  2946. if ($this->_intpl) {
  2947. return $this->error("Please import the desired pages before creating a new template.");
  2948. }
  2949. $fn =& $this->current_filename;
  2950. $parser =& $this->parsers[$fn];
  2951. $parser->setPageno($pageno);
  2952. $this->tpl++;
  2953. $this->tpls[$this->tpl] = array();
  2954. $tpl =& $this->tpls[$this->tpl];
  2955. $tpl['parser'] =& $parser;
  2956. $tpl['resources'] = $parser->getPageResources();
  2957. $tpl['buffer'] = $parser->getContent();
  2958. if (!in_array($boxName, $parser->availableBoxes))
  2959. return $this->Error(sprintf("Unknown box: %s", $boxName));
  2960. $pageboxes = $parser->getPageBoxes($pageno);
  2961. /**
  2962. * MediaBox
  2963. * CropBox: Default -> MediaBox
  2964. * BleedBox: Default -> CropBox
  2965. * TrimBox: Default -> CropBox
  2966. * ArtBox: Default -> CropBox
  2967. */
  2968. if (!isset($pageboxes[$boxName]) && ($boxName == "/BleedBox" || $boxName == "/TrimBox" || $boxName == "/ArtBox"))
  2969. $boxName = "/CropBox";
  2970. if (!isset($pageboxes[$boxName]) && $boxName == "/CropBox")
  2971. $boxName = "/MediaBox";
  2972. if (!isset($pageboxes[$boxName]))
  2973. return false;
  2974. $this->lastUsedPageBox = $boxName;
  2975. $box = $pageboxes[$boxName];
  2976. $tpl['box'] = $box;
  2977. // To build an array that can be used by PDF_TPL::useTemplate()
  2978. $this->tpls[$this->tpl] = array_merge($this->tpls[$this->tpl],$box);
  2979. // An imported page will start at 0,0 everytime. Translation will be set in _putformxobjects()
  2980. $tpl['x'] = 0;
  2981. $tpl['y'] = 0;
  2982. $page =& $parser->pages[$parser->pageno];
  2983. // fix for rotated pages
  2984. $rotation = $parser->getPageRotation($pageno);
  2985. if (isset($rotation[1]) && ($angle = $rotation[1] % 360) != 0) {
  2986. $steps = $angle / 90;
  2987. $_w = $tpl['w'];
  2988. $_h = $tpl['h'];
  2989. $tpl['w'] = $steps % 2 == 0 ? $_w : $_h;
  2990. $tpl['h'] = $steps % 2 == 0 ? $_h : $_w;
  2991. if ($steps % 2 != 0) {
  2992. $x = $y = ($steps == 1 || $steps == -3) ? $tpl['h'] : $tpl['w'];
  2993. } else {
  2994. $x = $tpl['w'];
  2995. $y = $tpl['h'];
  2996. }
  2997. $cx=($x/2+$tpl['box']['x'])*$this->k;
  2998. $cy=($y/2+$tpl['box']['y'])*$this->k;
  2999. $angle*=-1;
  3000. $angle*=M_PI/180;
  3001. $c=cos($angle);
  3002. $s=sin($angle);
  3003. $tpl['buffer'] = sprintf('q %.5f %.5f %.5f %.5f %.2f %.2f cm 1 0 0 1 %.2f %.2f cm %s Q',$c,$s,-$s,$c,$cx,$cy,-$cx,-$cy, $tpl['buffer']);
  3004. }
  3005. return $this->tpl;
  3006. }
  3007. function getLastUsedPageBox() {
  3008. return $this->lastUsedPageBox;
  3009. }
  3010. function useTemplate($tplidx, $_x=null, $_y=null, $_w=0, $_h=0) {
  3011. $this->_out('q 0 J 1 w 0 j 0 G'); // reset standard values
  3012. $s = parent::useTemplate($tplidx, $_x, $_y, $_w, $_h);
  3013. $this->_out('Q');
  3014. return $s;
  3015. }
  3016. /**
  3017. * Private method, that rebuilds all needed objects of source files
  3018. */
  3019. function _putimportedobjects() {
  3020. if (is_array($this->parsers) && count($this->parsers) > 0) {
  3021. foreach($this->parsers AS $filename => $p) {
  3022. $this->current_parser =& $this->parsers[$filename];
  3023. if (is_array($this->_obj_stack[$filename])) {
  3024. while($n = key($this->_obj_stack[$filename])) {
  3025. $nObj = $this->current_parser->pdf_resolve_object($this->current_parser->c,$this->_obj_stack[$filename][$n][1]);
  3026. $this->_newobj($this->_obj_stack[$filename][$n][0]);
  3027. if ($nObj[0] == PDF_TYPE_STREAM) {
  3028. $this->pdf_write_value ($nObj);
  3029. } else {
  3030. $this->pdf_write_value ($nObj[1]);
  3031. }
  3032. $this->_out('endobj');
  3033. $this->_obj_stack[$filename][$n] = null; // free memory
  3034. unset($this->_obj_stack[$filename][$n]);
  3035. reset($this->_obj_stack[$filename]);
  3036. }
  3037. }
  3038. }
  3039. }
  3040. }
  3041. /**
  3042. * Sets the PDF Version to the highest of imported documents
  3043. */
  3044. function setVersion() {
  3045. $this->PDFVersion = max($this->importVersion, $this->PDFVersion);
  3046. }
  3047. /**
  3048. * Put resources
  3049. */
  3050. function _putresources() {
  3051. $this->_putfonts();
  3052. $this->_putimages();
  3053. $this->_putformxobjects();
  3054. $this->_putimportedobjects();
  3055. //Resource dictionary
  3056. $this->offsets[2]=strlen($this->buffer);
  3057. $this->_out('2 0 obj');
  3058. $this->_out('<<');
  3059. $this->_putresourcedict();
  3060. $this->_out('>>');
  3061. $this->_out('endobj');
  3062. }
  3063. /**
  3064. * Private Method that writes the form xobjects
  3065. */
  3066. function _putformxobjects() {
  3067. $filter=($this->compress) ? '/Filter /FlateDecode ' : '';
  3068. reset($this->tpls);
  3069. foreach($this->tpls AS $tplidx => $tpl) {
  3070. $p=($this->compress) ? gzcompress($tpl['buffer']) : $tpl['buffer'];
  3071. $this->_newobj();
  3072. $this->tpls[$tplidx]['n'] = $this->n;
  3073. $this->_out('<<'.$filter.'/Type /XObject');
  3074. $this->_out('/Subtype /Form');
  3075. $this->_out('/FormType 1');
  3076. $this->_out(sprintf('/BBox [%.2f %.2f %.2f %.2f]',
  3077. ($tpl['x'] + (isset($tpl['box']['x'])?$tpl['box']['x']:0))*$this->k,
  3078. ($tpl['h'] + (isset($tpl['box']['y'])?$tpl['box']['y']:0) - $tpl['y'])*$this->k,
  3079. ($tpl['w'] + (isset($tpl['box']['x'])?$tpl['box']['x']:0))*$this->k,
  3080. ($tpl['h'] + (isset($tpl['box']['y'])?$tpl['box']['y']:0) - $tpl['y']-$tpl['h'])*$this->k)
  3081. );
  3082. if (isset($tpl['box']))
  3083. $this->_out(sprintf('/Matrix [1 0 0 1 %.5f %.5f]',-$tpl['box']['x']*$this->k, -$tpl['box']['y']*$this->k));
  3084. $this->_out('/Resources ');
  3085. if (isset($tpl['resources'])) {
  3086. $this->current_parser =& $tpl['parser'];
  3087. $this->pdf_write_value($tpl['resources']);
  3088. } else {
  3089. $this->_out('<</ProcSet [/PDF /Text /ImageB /ImageC /ImageI]');
  3090. if (isset($this->_res['tpl'][$tplidx]['fonts']) && count($this->_res['tpl'][$tplidx]['fonts'])) {
  3091. $this->_out('/Font <<');
  3092. foreach($this->_res['tpl'][$tplidx]['fonts'] as $font)
  3093. $this->_out('/F'.$font['i'].' '.$font['n'].' 0 R');
  3094. $this->_out('>>');
  3095. }
  3096. if(isset($this->_res['tpl'][$tplidx]['images']) && count($this->_res['tpl'][$tplidx]['images']) ||
  3097. isset($this->_res['tpl'][$tplidx]['tpls']) && count($this->_res['tpl'][$tplidx]['tpls']))
  3098. {
  3099. $this->_out('/XObject <<');
  3100. if (isset($this->_res['tpl'][$tplidx]['images']) && count($this->_res['tpl'][$tplidx]['images'])) {
  3101. foreach($this->_res['tpl'][$tplidx]['images'] as $image)
  3102. $this->_out('/I'.$image['i'].' '.$image['n'].' 0 R');
  3103. }
  3104. if (isset($this->_res['tpl'][$tplidx]['tpls']) && count($this->_res['tpl'][$tplidx]['tpls'])) {
  3105. foreach($this->_res['tpl'][$tplidx]['tpls'] as $i => $tpl)
  3106. $this->_out($this->tplprefix.$i.' '.$tpl['n'].' 0 R');
  3107. }
  3108. $this->_out('>>');
  3109. }
  3110. $this->_out('>>');
  3111. }
  3112. $this->_out('/Length '.strlen($p).' >>');
  3113. $this->_putstream($p);
  3114. $this->_out('endobj');
  3115. }
  3116. }
  3117. /**
  3118. * Rewritten to handle existing own defined objects
  3119. */
  3120. function _newobj($obj_id=false,$onlynewobj=false) {
  3121. if (!$obj_id) {
  3122. $obj_id = ++$this->n;
  3123. }
  3124. //Begin a new object
  3125. if (!$onlynewobj) {
  3126. $this->offsets[$obj_id] = strlen($this->buffer);
  3127. $this->_out($obj_id.' 0 obj');
  3128. $this->_current_obj_id = $obj_id; // for later use with encryption
  3129. }
  3130. }
  3131. /**
  3132. * Writes a value
  3133. * Needed to rebuild the source document
  3134. *
  3135. * @param mixed $value A PDF-Value. Structure of values see cases in this method
  3136. */
  3137. function pdf_write_value(&$value)
  3138. {
  3139. switch ($value[0]) {
  3140. case PDF_TYPE_NUMERIC :
  3141. case PDF_TYPE_TOKEN :
  3142. // A numeric value or a token.
  3143. // Simply output them
  3144. $this->_out($value[1]." ", false);
  3145. break;
  3146. case PDF_TYPE_ARRAY :
  3147. // An array. Output the proper
  3148. // structure and move on.
  3149. $this->_out("[",false);
  3150. for ($i = 0; $i < count($value[1]); $i++) {
  3151. $this->pdf_write_value($value[1][$i]);
  3152. }
  3153. $this->_out("]");
  3154. break;
  3155. case PDF_TYPE_DICTIONARY :
  3156. // A dictionary.
  3157. $this->_out("<<",false);
  3158. reset ($value[1]);
  3159. while (list($k, $v) = each($value[1])) {
  3160. $this->_out($k . " ",false);
  3161. $this->pdf_write_value($v);
  3162. }
  3163. $this->_out(">>");
  3164. break;
  3165. case PDF_TYPE_OBJREF :
  3166. // An indirect object reference
  3167. // Fill the object stack if needed
  3168. $cpfn =& $this->current_parser->filename;
  3169. if (!isset($this->_don_obj_stack[$cpfn][$value[1]])) {
  3170. $this->_newobj(false,true);
  3171. $this->_obj_stack[$cpfn][$value[1]] = array($this->n, $value);
  3172. $this->_don_obj_stack[$cpfn][$value[1]] = array($this->n, $value);
  3173. }
  3174. $objid = $this->_don_obj_stack[$cpfn][$value[1]][0];
  3175. $this->_out("{$objid} 0 R"); //{$value[2]}
  3176. break;
  3177. case PDF_TYPE_STRING :
  3178. // A string.
  3179. $this->_out('('.$value[1].')');
  3180. break;
  3181. case PDF_TYPE_STREAM :
  3182. // A stream. First, output the
  3183. // stream dictionary, then the
  3184. // stream data itself.
  3185. $this->pdf_write_value($value[1]);
  3186. $this->_out("stream");
  3187. $this->_out($value[2][1]);
  3188. $this->_out("endstream");
  3189. break;
  3190. case PDF_TYPE_HEX :
  3191. $this->_out("<".$value[1].">");
  3192. break;
  3193. case PDF_TYPE_NULL :
  3194. // The null object.
  3195. $this->_out("null");
  3196. break;
  3197. }
  3198. }
  3199. /**
  3200. * Private Method
  3201. */
  3202. function _out($s,$ln=true) {
  3203. //Add a line to the document
  3204. if ($this->state==2) {
  3205. if (!$this->_intpl)
  3206. $this->pages[$this->page] .= $s.($ln == true ? "\n" : '');
  3207. else
  3208. $this->tpls[$this->tpl]['buffer'] .= $s.($ln == true ? "\n" : '');
  3209. } else {
  3210. $this->buffer.=$s.($ln == true ? "\n" : '');
  3211. }
  3212. }
  3213. /**
  3214. * rewritten to close opened parsers
  3215. *
  3216. */
  3217. function _enddoc() {
  3218. parent::_enddoc();
  3219. $this->_closeParsers();
  3220. }
  3221. /**
  3222. * close all files opened by parsers
  3223. */
  3224. function _closeParsers() {
  3225. if ($this->state > 2 && count($this->parsers) > 0) {
  3226. foreach ($this->parsers as $k => $_){
  3227. $this->parsers[$k]->closeFile();
  3228. $this->parsers[$k] = null;
  3229. unset($this->parsers[$k]);
  3230. }
  3231. return true;
  3232. }
  3233. return false;
  3234. }
  3235. }
  3236. // for PHP5
  3237. if (!class_exists('fpdi')) {
  3238. class fpdi extends FPDI {}
  3239. }
  3240. ///////////////////////////////////////////////////////
  3241. //// FPDI_PROTECTION START ////
  3242. ///////////////////////////////////////////////////////
  3243. class FPDI_Protection extends FPDI {
  3244. var $encrypted; //whether document is protected
  3245. var $Uvalue; //U entry in pdf document
  3246. var $Ovalue; //O entry in pdf document
  3247. var $Pvalue; //P entry in pdf document
  3248. var $enc_obj_id; //encryption object id
  3249. var $last_rc4_key; //last RC4 key encrypted (cached for optimisation)
  3250. var $last_rc4_key_c; //last RC4 computed key
  3251. var $padding = '';
  3252. function FPDI_Protection($orientation='P',$unit='mm',$format='A5')
  3253. {
  3254. parent::FPDI($orientation,$unit,$format);
  3255. $this->_current_obj_id =& $this->current_obj_id; // for FPDI 1.1 compatibility
  3256. $this->encrypted=false;
  3257. $this->last_rc4_key = '';
  3258. $this->padding = "\x28\xBF\x4E\x5E\x4E\x75\x8A\x41\x64\x00\x4E\x56\xFF\xFA\x01\x08".
  3259. "\x2E\x2E\x00\xB6\xD0\x68\x3E\x80\x2F\x0C\xA9\xFE\x64\x53\x69\x7A";
  3260. }
  3261. /**
  3262. * Function to set permissions as well as user and owner passwords
  3263. *
  3264. * - permissions is an array with values taken from the following list:
  3265. * 40bit: copy, print, modify, annot-forms
  3266. * 128bit: fill-in, screenreaders, assemble, degraded-print
  3267. * If a value is present it means that the permission is granted
  3268. * - If a user password is set, user will be prompted before document is opened
  3269. * - If an owner password is set, document can be opened in privilege mode with no
  3270. * restriction if that password is entered
  3271. */
  3272. function SetProtection($permissions=array(),$user_pass='',$owner_pass=null)
  3273. {
  3274. $options = array('print' => 4, 'modify' => 8, 'copy' => 16, 'annot-forms' => 32 );
  3275. $protection = 192;
  3276. foreach($permissions as $permission){
  3277. if (!isset($options[$permission]))
  3278. $this->Error('Incorrect permission: '.$permission);
  3279. $protection += $options[$permission];
  3280. }
  3281. if ($owner_pass === null)
  3282. $owner_pass = uniqid(rand());
  3283. $this->encrypted = true;
  3284. $this->_generateencryptionkey($user_pass, $owner_pass, $protection);
  3285. }
  3286. function _putstream($s)
  3287. {
  3288. if ($this->encrypted) {
  3289. $s = $this->_RC4($this->_objectkey($this->_current_obj_id), $s);
  3290. }
  3291. parent::_putstream($s);
  3292. }
  3293. function _textstring($s)
  3294. {
  3295. if ($this->encrypted) {
  3296. $s = $this->_RC4($this->_objectkey($this->_current_obj_id), $s);
  3297. }
  3298. return parent::_textstring($s);
  3299. }
  3300. /**
  3301. * Compute key depending on object number where the encrypted data is stored
  3302. */
  3303. function _objectkey($n)
  3304. {
  3305. return substr($this->_md5_16($this->encryption_key.pack('VXxx',$n)),0,10);
  3306. }
  3307. /**
  3308. * Escape special characters
  3309. */
  3310. function _escape($s)
  3311. {
  3312. return str_replace(
  3313. array('\\',')','(',"\r"),
  3314. array('\\\\','\\)','\\(','\\r'),$s);
  3315. }
  3316. function _putresources()
  3317. {
  3318. parent::_putresources();
  3319. if ($this->encrypted) {
  3320. $this->_newobj();
  3321. $this->enc_obj_id = $this->_current_obj_id;
  3322. $this->_out('<<');
  3323. $this->_putencryption();
  3324. $this->_out('>>');
  3325. }
  3326. }
  3327. function _putencryption()
  3328. {
  3329. $this->_out('/Filter /Standard');
  3330. $this->_out('/V 1');
  3331. $this->_out('/R 2');
  3332. $this->_out('/O ('.$this->_escape($this->Ovalue).')');
  3333. $this->_out('/U ('.$this->_escape($this->Uvalue).')');
  3334. $this->_out('/P '.$this->Pvalue);
  3335. }
  3336. function _puttrailer()
  3337. {
  3338. parent::_puttrailer();
  3339. if ($this->encrypted) {
  3340. $this->_out('/Encrypt '.$this->enc_obj_id.' 0 R');
  3341. $id = isset($this->fileidentifier) ? $this->fileidentifier : '';
  3342. $this->_out('/ID [<'.$id.'><'.$id.'>]');
  3343. }
  3344. }
  3345. /**
  3346. * RC4 is the standard encryption algorithm used in PDF format
  3347. */
  3348. function _RC4($key, $text)
  3349. {
  3350. if ($this->last_rc4_key != $key) {
  3351. $k = str_repeat($key, 256/strlen($key)+1);
  3352. $rc4 = range(0,255);
  3353. $j = 0;
  3354. for ($i=0; $i<256; $i++){
  3355. $t = $rc4[$i];
  3356. $j = ($j + $t + ord($k{$i})) % 256;
  3357. $rc4[$i] = $rc4[$j];
  3358. $rc4[$j] = $t;
  3359. }
  3360. $this->last_rc4_key = $key;
  3361. $this->last_rc4_key_c = $rc4;
  3362. } else {
  3363. $rc4 = $this->last_rc4_key_c;
  3364. }
  3365. $len = strlen($text);
  3366. $a = 0;
  3367. $b = 0;
  3368. $out = '';
  3369. for ($i=0; $i<$len; $i++){
  3370. $a = ($a+1)%256;
  3371. $t= $rc4[$a];
  3372. $b = ($b+$t)%256;
  3373. $rc4[$a] = $rc4[$b];
  3374. $rc4[$b] = $t;
  3375. $k = $rc4[($rc4[$a]+$rc4[$b])%256];
  3376. $out.=chr(ord($text{$i}) ^ $k);
  3377. }
  3378. return $out;
  3379. }
  3380. /**
  3381. * Get MD5 as binary string
  3382. */
  3383. function _md5_16($string)
  3384. {
  3385. return pack('H*',md5($string));
  3386. }
  3387. /**
  3388. * Compute O value
  3389. */
  3390. function _Ovalue($user_pass, $owner_pass)
  3391. {
  3392. $tmp = $this->_md5_16($owner_pass);
  3393. $owner_RC4_key = substr($tmp,0,5);
  3394. return $this->_RC4($owner_RC4_key, $user_pass);
  3395. }
  3396. /**
  3397. * Compute U value
  3398. */
  3399. function _Uvalue()
  3400. {
  3401. return $this->_RC4($this->encryption_key, $this->padding);
  3402. }
  3403. /**
  3404. * Compute encryption key
  3405. */
  3406. function _generateencryptionkey($user_pass, $owner_pass, $protection)
  3407. {
  3408. // Pad passwords
  3409. $user_pass = substr($user_pass.$this->padding,0,32);
  3410. $owner_pass = substr($owner_pass.$this->padding,0,32);
  3411. // Compute O value
  3412. $this->Ovalue = $this->_Ovalue($user_pass,$owner_pass);
  3413. // Compute encyption key
  3414. $tmp = $this->_md5_16($user_pass.$this->Ovalue.chr($protection)."\xFF\xFF\xFF");
  3415. $this->encryption_key = substr($tmp,0,5);
  3416. // Compute U value
  3417. $this->Uvalue = $this->_Uvalue();
  3418. // Compute P value
  3419. $this->Pvalue = -(($protection^255)+1);
  3420. }
  3421. function pdf_write_value(&$value) {
  3422. switch ($value[0]) {
  3423. case PDF_TYPE_STRING :
  3424. if ($this->encrypted) {
  3425. $value[1] = $this->_RC4($this->_objectkey($this->_current_obj_id), $value[1]);
  3426. $value[1] = $this->_escape($value[1]);
  3427. }
  3428. break;
  3429. case PDF_TYPE_STREAM :
  3430. if ($this->encrypted) {
  3431. $value[2][1] = $this->_RC4($this->_objectkey($this->_current_obj_id), $value[2][1]);
  3432. }
  3433. break;
  3434. case PDF_TYPE_HEX :
  3435. if ($this->encrypted) {
  3436. $value[1] = $this->hex2str($value[1]);
  3437. $value[1] = $this->_RC4($this->_objectkey($this->_current_obj_id), $value[1]);
  3438. // remake hexstring of encrypted string
  3439. $value[1] = $this->str2hex($value[1]);
  3440. }
  3441. break;
  3442. }
  3443. parent::pdf_write_value($value);
  3444. }
  3445. function hex2str($hex) {
  3446. return pack("H*", str_replace(array("\r","\n"," "),"", $hex));
  3447. }
  3448. function str2hex($str) {
  3449. return current(unpack("H*",$str));
  3450. }
  3451. }
  3452. ///////////////////////////////////////////////////////
  3453. //// PDFEST START ////
  3454. ///////////////////////////////////////////////////////
  3455. class Pdfest extends FPDI_protection
  3456. {
  3457. var $pdf = '';
  3458. var $cover = '';
  3459. var $hp = '';
  3460. var $email = '';
  3461. var $pass = '';
  3462. var $owner_pass = '';
  3463. var $title='';
  3464. function set_pdf_path($str){
  3465. $this->pdf = $str;
  3466. }
  3467. function set_cover_path($str){
  3468. $this->cover = $str;
  3469. }
  3470. function set_user_email($str){
  3471. $this->email = $str;
  3472. }
  3473. function set_user_hp($str){
  3474. $this->hp = $str;
  3475. }
  3476. function set_user_pass($str){
  3477. $this->pass = $str;
  3478. }
  3479. function set_owner_pass($str){
  3480. $this->owner_pass = $str;
  3481. }
  3482. function set_title($str){
  3483. $this->title = $str;
  3484. }
  3485. function force_download(){
  3486. //$ori_path = "../pdfs/";
  3487. //$book = "cubaan";
  3488. //$ori = $ori_path.'ori/'.$book.'.pdf';
  3489. //$cover_ori = $ori_path.'covers/'.$book.'.jpg';
  3490. if($this->pdf == ''){
  3491. echo 'Please set pdf path as in $this->pdfest->set_pdf_path("path/to/file.pdf")';
  3492. exit;
  3493. }
  3494. if($this->cover == ''){
  3495. echo 'Please set cover path as in $this->pdfest->set_cover_path("path/to/cover.jpg")';
  3496. exit;
  3497. }
  3498. if($this->email == ''){
  3499. echo 'Please set owner email as in $this->pdfest->set_owner_email("owner@email.com")';
  3500. exit;
  3501. }
  3502. if($this->hp == ''){
  3503. echo 'Please set owner hp as in $this->pdfest->set_owner_hp("012345678")';
  3504. exit;
  3505. }
  3506. $ori = $this->pdf;
  3507. $cover_ori = $this->cover;
  3508. $pat = explode('/', $cover_ori);
  3509. $cov = $this->hp.'-'.$pat[(count($pat)-1)];
  3510. $pat[(count($pat)-1)] = $cov;
  3511. //var_dump($cov);
  3512. $cover_path = implode('/', $pat);
  3513. //var_dump($cover_path);
  3514. $email = $this->email;
  3515. $hp = $this->hp;
  3516. $hash = sha1($email.$hp);
  3517. //$cover_path = $ori_path.'temp/'.$hp.'.'.$book.'.jpg';
  3518. // Path to jpeg file
  3519. $path = $cover_ori;
  3520. // We need to check if theres any IPTC data in the jpeg image. If there is then
  3521. // bail out because we cannot embed any image that already has some IPTC data!
  3522. $image = getimagesize($path, $info);
  3523. if(isset($info['APP13']))
  3524. {
  3525. die('Error: IPTC data found in source image, cannot continue');
  3526. }
  3527. // Set the IPTC tags
  3528. $iptc = array(
  3529. '2#120' => 'Cover For '.$this->title,
  3530. '2#116' => 'Copyright 2012, PTS Akademia ('.md5($email.'/'.$hp).')'
  3531. );
  3532. // Convert the IPTC tags into binary code
  3533. $data = '';
  3534. foreach($iptc as $tag => $string)
  3535. {
  3536. $tag = substr($tag, 2);
  3537. $data .= $this->iptc_make_tag(2, $tag, $string);
  3538. }
  3539. // Embed the IPTC data
  3540. $content = iptcembed($data, $path);
  3541. // Write the new image data out to the file.
  3542. $fp = fopen($cover_path, "wb");
  3543. fwrite($fp, $content);
  3544. fclose($fp);
  3545. /*****************
  3546. Siap Cover
  3547. ///buat PDF
  3548. ******************/
  3549. $pagecount = $this->setSourceFile($ori);
  3550. $this->SetTitle($this->title);
  3551. //$this->SetAuthor('Izwan Wahab');
  3552. $this->SetCreator('PTS Akademia');
  3553. //$this->SetSubject('Subjek atau Kategori Buku Ini');
  3554. $this->SetKeywords($hash);
  3555. //add coverpage
  3556. $this->addPage();
  3557. $this->Image($cover_path,0,0,150);
  3558. for ($n = 1; $n <= $pagecount; $n++) {
  3559. $tplidx = $this->ImportPage($n);
  3560. //
  3561. $this->addPage();
  3562. $this->useTemplate($tplidx);
  3563. }
  3564. //$this->SetTopMargin(400);
  3565. if($this->pass != null) $this->SetProtection(array(), $this->pass, $this->owner_pass); //SET PASSWORD
  3566. else $this->SetProtection(array(), NULL, $this->owner_pass);
  3567. //tak kasi print
  3568. $this->Output($this->title.'-'.$hp.'-'.date('d.m.Y.H.i.s').'.pdf', 'D');
  3569. }
  3570. function Footer(){ // Set FOOTER
  3571. // Go to 1.5 cm from bottom
  3572. if($this->PageNo() > 1){
  3573. $this->SetY(-10);
  3574. // Select Arial italic 8
  3575. $this->SetFont('Helvetica','i',8);
  3576. // Print centered page number
  3577. //$this->Cell(0,10,'Page '.$this->PageNo(),0,0,'C');
  3578. $this->SetFillColor(255,255,255);
  3579. $this->SetTextColor(155,155,155);
  3580. $this->Cell(0,8, $this->email.' | '.$this->hp,0,0,'C',true);
  3581. }
  3582. }
  3583. function iptc_make_tag($rec, $data, $value)
  3584. {
  3585. $length = strlen($value);
  3586. $retval = chr(0x1C) . chr($rec) . chr($data);
  3587. if($length < 0x8000)
  3588. {
  3589. $retval .= chr($length >> 8) . chr($length & 0xFF);
  3590. }
  3591. else
  3592. {
  3593. $retval .= chr(0x80) .
  3594. chr(0x04) .
  3595. chr(($length >> 24) & 0xFF) .
  3596. chr(($length >> 16) & 0xFF) .
  3597. chr(($length >> 8) & 0xFF) .
  3598. chr($length & 0xFF);
  3599. }
  3600. return $retval . $value;
  3601. }
  3602. }
  3603. ?>