fpdf.php 46 KB

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