fpdf.php 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802
  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 __construct($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. $txt = iconv('utf-8', 'cp1252', $txt);
  609. if($align=='R')
  610. $dx = $w-$this->cMargin-$this->GetStringWidth($txt);
  611. elseif($align=='C')
  612. $dx = ($w-$this->GetStringWidth($txt))/2;
  613. else
  614. $dx = $this->cMargin;
  615. if($this->ColorFlag)
  616. $s .= 'q '.$this->TextColor.' ';
  617. $txt2 = str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$txt)));
  618. $s .= sprintf('BT %.2F %.2F Td (%s) Tj ET',($this->x+$dx)*$k,($this->h-($this->y+.5*$h+.3*$this->FontSize))*$k,$txt2);
  619. if($this->underline)
  620. $s .= ' '.$this->_dounderline($this->x+$dx,$this->y+.5*$h+.3*$this->FontSize,$txt);
  621. if($this->ColorFlag)
  622. $s .= ' Q';
  623. if($link)
  624. $this->Link($this->x+$dx,$this->y+.5*$h-.5*$this->FontSize,$this->GetStringWidth($txt),$this->FontSize,$link);
  625. }
  626. if($s)
  627. $this->_out($s);
  628. $this->lasth = $h;
  629. if($ln>0)
  630. {
  631. // Go to next line
  632. $this->y += $h;
  633. if($ln==1)
  634. $this->x = $this->lMargin;
  635. }
  636. else
  637. $this->x += $w;
  638. }
  639. function MultiCell($w, $h, $txt, $border=0, $align='J', $fill=false)
  640. {
  641. // Output text with automatic or explicit line breaks
  642. $cw = &$this->CurrentFont['cw'];
  643. if($w==0)
  644. $w = $this->w-$this->rMargin-$this->x;
  645. $wmax = ($w-2*$this->cMargin)*1000/$this->FontSize;
  646. $s = str_replace("\r",'',$txt);
  647. $nb = strlen($s);
  648. if($nb>0 && $s[$nb-1]=="\n")
  649. $nb--;
  650. $b = 0;
  651. if($border)
  652. {
  653. if($border==1)
  654. {
  655. $border = 'LTRB';
  656. $b = 'LRT';
  657. $b2 = 'LR';
  658. }
  659. else
  660. {
  661. $b2 = '';
  662. if(strpos($border,'L')!==false)
  663. $b2 .= 'L';
  664. if(strpos($border,'R')!==false)
  665. $b2 .= 'R';
  666. $b = (strpos($border,'T')!==false) ? $b2.'T' : $b2;
  667. }
  668. }
  669. $sep = -1;
  670. $i = 0;
  671. $j = 0;
  672. $l = 0;
  673. $ns = 0;
  674. $nl = 1;
  675. while($i<$nb)
  676. {
  677. // Get next character
  678. $c = $s[$i];
  679. if($c=="\n")
  680. {
  681. // Explicit line break
  682. if($this->ws>0)
  683. {
  684. $this->ws = 0;
  685. $this->_out('0 Tw');
  686. }
  687. $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
  688. $i++;
  689. $sep = -1;
  690. $j = $i;
  691. $l = 0;
  692. $ns = 0;
  693. $nl++;
  694. if($border && $nl==2)
  695. $b = $b2;
  696. continue;
  697. }
  698. if($c==' ')
  699. {
  700. $sep = $i;
  701. $ls = $l;
  702. $ns++;
  703. }
  704. $l += $cw[$c];
  705. if($l>$wmax)
  706. {
  707. // Automatic line break
  708. if($sep==-1)
  709. {
  710. if($i==$j)
  711. $i++;
  712. if($this->ws>0)
  713. {
  714. $this->ws = 0;
  715. $this->_out('0 Tw');
  716. }
  717. $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
  718. }
  719. else
  720. {
  721. if($align=='J')
  722. {
  723. $this->ws = ($ns>1) ? ($wmax-$ls)/1000*$this->FontSize/($ns-1) : 0;
  724. $this->_out(sprintf('%.3F Tw',$this->ws*$this->k));
  725. }
  726. $this->Cell($w,$h,substr($s,$j,$sep-$j),$b,2,$align,$fill);
  727. $i = $sep+1;
  728. }
  729. $sep = -1;
  730. $j = $i;
  731. $l = 0;
  732. $ns = 0;
  733. $nl++;
  734. if($border && $nl==2)
  735. $b = $b2;
  736. }
  737. else
  738. $i++;
  739. }
  740. // Last chunk
  741. if($this->ws>0)
  742. {
  743. $this->ws = 0;
  744. $this->_out('0 Tw');
  745. }
  746. if($border && strpos($border,'B')!==false)
  747. $b .= 'B';
  748. $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
  749. $this->x = $this->lMargin;
  750. }
  751. function Write($h, $txt, $link='')
  752. {
  753. // Output text in flowing mode
  754. $cw = &$this->CurrentFont['cw'];
  755. $w = $this->w-$this->rMargin-$this->x;
  756. $wmax = ($w-2*$this->cMargin)*1000/$this->FontSize;
  757. $s = str_replace("\r",'',$txt);
  758. $nb = strlen($s);
  759. $sep = -1;
  760. $i = 0;
  761. $j = 0;
  762. $l = 0;
  763. $nl = 1;
  764. while($i<$nb)
  765. {
  766. // Get next character
  767. $c = $s[$i];
  768. if($c=="\n")
  769. {
  770. // Explicit line break
  771. $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
  772. $i++;
  773. $sep = -1;
  774. $j = $i;
  775. $l = 0;
  776. if($nl==1)
  777. {
  778. $this->x = $this->lMargin;
  779. $w = $this->w-$this->rMargin-$this->x;
  780. $wmax = ($w-2*$this->cMargin)*1000/$this->FontSize;
  781. }
  782. $nl++;
  783. continue;
  784. }
  785. if($c==' ')
  786. $sep = $i;
  787. $l += $cw[$c];
  788. if($l>$wmax)
  789. {
  790. // Automatic line break
  791. if($sep==-1)
  792. {
  793. if($this->x>$this->lMargin)
  794. {
  795. // Move to next line
  796. $this->x = $this->lMargin;
  797. $this->y += $h;
  798. $w = $this->w-$this->rMargin-$this->x;
  799. $wmax = ($w-2*$this->cMargin)*1000/$this->FontSize;
  800. $i++;
  801. $nl++;
  802. continue;
  803. }
  804. if($i==$j)
  805. $i++;
  806. $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
  807. }
  808. else
  809. {
  810. $this->Cell($w,$h,substr($s,$j,$sep-$j),0,2,'',0,$link);
  811. $i = $sep+1;
  812. }
  813. $sep = -1;
  814. $j = $i;
  815. $l = 0;
  816. if($nl==1)
  817. {
  818. $this->x = $this->lMargin;
  819. $w = $this->w-$this->rMargin-$this->x;
  820. $wmax = ($w-2*$this->cMargin)*1000/$this->FontSize;
  821. }
  822. $nl++;
  823. }
  824. else
  825. $i++;
  826. }
  827. // Last chunk
  828. if($i!=$j)
  829. $this->Cell($l/1000*$this->FontSize,$h,substr($s,$j),0,0,'',0,$link);
  830. }
  831. function Ln($h=null)
  832. {
  833. // Line feed; default value is last cell height
  834. $this->x = $this->lMargin;
  835. if($h===null)
  836. $this->y += $this->lasth;
  837. else
  838. $this->y += $h;
  839. }
  840. function Image($file, $x=null, $y=null, $w=0, $h=0, $type='', $link='')
  841. {
  842. // Put an image on the page
  843. if(!isset($this->images[$file]))
  844. {
  845. // First use of this image, get info
  846. if($type=='')
  847. {
  848. $pos = strrpos($file,'.');
  849. if(!$pos)
  850. $this->Error('Image file has no extension and no type was specified: '.$file);
  851. $type = substr($file,$pos+1);
  852. }
  853. $type = strtolower($type);
  854. if($type=='jpeg')
  855. $type = 'jpg';
  856. $mtd = '_parse'.$type;
  857. if(!method_exists($this,$mtd))
  858. $this->Error('Unsupported image type: '.$type);
  859. $info = $this->$mtd($file);
  860. $info['i'] = count($this->images)+1;
  861. $this->images[$file] = $info;
  862. }
  863. else
  864. $info = $this->images[$file];
  865. // Automatic width and height calculation if needed
  866. if($w==0 && $h==0)
  867. {
  868. // Put image at 96 dpi
  869. $w = -96;
  870. $h = -96;
  871. }
  872. if($w<0)
  873. $w = -$info['w']*72/$w/$this->k;
  874. if($h<0)
  875. $h = -$info['h']*72/$h/$this->k;
  876. if($w==0)
  877. $w = $h*$info['w']/$info['h'];
  878. if($h==0)
  879. $h = $w*$info['h']/$info['w'];
  880. // Flowing mode
  881. if($y===null)
  882. {
  883. if($this->y+$h>$this->PageBreakTrigger && !$this->InHeader && !$this->InFooter && $this->AcceptPageBreak())
  884. {
  885. // Automatic page break
  886. $x2 = $this->x;
  887. $this->AddPage($this->CurOrientation,$this->CurPageSize);
  888. $this->x = $x2;
  889. }
  890. $y = $this->y;
  891. $this->y += $h;
  892. }
  893. if($x===null)
  894. $x = $this->x;
  895. $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']));
  896. if($link)
  897. $this->Link($x,$y,$w,$h,$link);
  898. }
  899. function GetX()
  900. {
  901. // Get x position
  902. return $this->x;
  903. }
  904. function SetX($x)
  905. {
  906. // Set x position
  907. if($x>=0)
  908. $this->x = $x;
  909. else
  910. $this->x = $this->w+$x;
  911. }
  912. function GetY()
  913. {
  914. // Get y position
  915. return $this->y;
  916. }
  917. function SetY($y)
  918. {
  919. // Set y position and reset x
  920. $this->x = $this->lMargin;
  921. if($y>=0)
  922. $this->y = $y;
  923. else
  924. $this->y = $this->h+$y;
  925. }
  926. function SetXY($x, $y)
  927. {
  928. // Set x and y positions
  929. $this->SetY($y);
  930. $this->SetX($x);
  931. }
  932. function Output($name='', $dest='')
  933. {
  934. // Output PDF to some destination
  935. if($this->state<3)
  936. $this->Close();
  937. $dest = strtoupper($dest);
  938. if($dest=='')
  939. {
  940. if($name=='')
  941. {
  942. $name = 'doc.pdf';
  943. $dest = 'I';
  944. }
  945. else
  946. $dest = 'F';
  947. }
  948. switch($dest)
  949. {
  950. case 'I':
  951. // Send to standard output
  952. $this->_checkoutput();
  953. if(PHP_SAPI!='cli')
  954. {
  955. // We send to a browser
  956. header('Content-Type: application/pdf');
  957. header('Content-Disposition: inline; filename="'.$name.'"');
  958. header('Cache-Control: private, max-age=0, must-revalidate');
  959. header('Pragma: public');
  960. }
  961. echo $this->buffer;
  962. break;
  963. case 'D':
  964. // Download file
  965. $this->_checkoutput();
  966. header('Content-Type: application/x-download');
  967. header('Content-Disposition: attachment; filename="'.$name.'"');
  968. header('Cache-Control: private, max-age=0, must-revalidate');
  969. header('Pragma: public');
  970. echo $this->buffer;
  971. break;
  972. case 'F':
  973. // Save to local file
  974. $f = fopen($name,'wb');
  975. if(!$f)
  976. $this->Error('Unable to create output file: '.$name);
  977. fwrite($f,$this->buffer,strlen($this->buffer));
  978. fclose($f);
  979. break;
  980. case 'S':
  981. // Return as a string
  982. return $this->buffer;
  983. default:
  984. $this->Error('Incorrect output destination: '.$dest);
  985. }
  986. return '';
  987. }
  988. /*******************************************************************************
  989. * *
  990. * Protected methods *
  991. * *
  992. *******************************************************************************/
  993. function _dochecks()
  994. {
  995. // Check availability of %F
  996. if(sprintf('%.1F',1.0)!='1.0')
  997. $this->Error('This version of PHP is not supported');
  998. // Check mbstring overloading
  999. if(ini_get('mbstring.func_overload') & 2)
  1000. $this->Error('mbstring overloading must be disabled');
  1001. }
  1002. function _checkoutput()
  1003. {
  1004. if(PHP_SAPI!='cli')
  1005. {
  1006. if(headers_sent($file,$line))
  1007. $this->Error("Some data has already been output, can't send PDF file (output started at $file:$line)");
  1008. }
  1009. if(ob_get_length())
  1010. {
  1011. // The output buffer is not empty
  1012. if(preg_match('/^(\xEF\xBB\xBF)?\s*$/',ob_get_contents()))
  1013. {
  1014. // It contains only a UTF-8 BOM and/or whitespace, let's clean it
  1015. ob_clean();
  1016. }
  1017. else
  1018. $this->Error("Some data has already been output, can't send PDF file");
  1019. }
  1020. }
  1021. function _getpagesize($size)
  1022. {
  1023. if(is_string($size))
  1024. {
  1025. $size = strtolower($size);
  1026. if(!isset($this->StdPageSizes[$size]))
  1027. $this->Error('Unknown page size: '.$size);
  1028. $a = $this->StdPageSizes[$size];
  1029. return array($a[0]/$this->k, $a[1]/$this->k);
  1030. }
  1031. else
  1032. {
  1033. if($size[0]>$size[1])
  1034. return array($size[1], $size[0]);
  1035. else
  1036. return $size;
  1037. }
  1038. }
  1039. function _beginpage($orientation, $size)
  1040. {
  1041. $this->page++;
  1042. $this->pages[$this->page] = '';
  1043. $this->state = 2;
  1044. $this->x = $this->lMargin;
  1045. $this->y = $this->tMargin;
  1046. $this->FontFamily = '';
  1047. // Check page size and orientation
  1048. if($orientation=='')
  1049. $orientation = $this->DefOrientation;
  1050. else
  1051. $orientation = strtoupper($orientation[0]);
  1052. if($size=='')
  1053. $size = $this->DefPageSize;
  1054. else
  1055. $size = $this->_getpagesize($size);
  1056. if($orientation!=$this->CurOrientation || $size[0]!=$this->CurPageSize[0] || $size[1]!=$this->CurPageSize[1])
  1057. {
  1058. // New size or orientation
  1059. if($orientation=='P')
  1060. {
  1061. $this->w = $size[0];
  1062. $this->h = $size[1];
  1063. }
  1064. else
  1065. {
  1066. $this->w = $size[1];
  1067. $this->h = $size[0];
  1068. }
  1069. $this->wPt = $this->w*$this->k;
  1070. $this->hPt = $this->h*$this->k;
  1071. $this->PageBreakTrigger = $this->h-$this->bMargin;
  1072. $this->CurOrientation = $orientation;
  1073. $this->CurPageSize = $size;
  1074. }
  1075. if($orientation!=$this->DefOrientation || $size[0]!=$this->DefPageSize[0] || $size[1]!=$this->DefPageSize[1])
  1076. $this->PageSizes[$this->page] = array($this->wPt, $this->hPt);
  1077. }
  1078. function _endpage()
  1079. {
  1080. $this->state = 1;
  1081. }
  1082. function _loadfont($font)
  1083. {
  1084. // Load a font definition file from the font directory
  1085. include($this->fontpath.$font);
  1086. $a = get_defined_vars();
  1087. if(!isset($a['name']))
  1088. $this->Error('Could not include font definition file');
  1089. return $a;
  1090. }
  1091. function _escape($s)
  1092. {
  1093. // Escape special characters in strings
  1094. $s = str_replace('\\','\\\\',$s);
  1095. $s = str_replace('(','\\(',$s);
  1096. $s = str_replace(')','\\)',$s);
  1097. $s = str_replace("\r",'\\r',$s);
  1098. return $s;
  1099. }
  1100. function _textstring($s)
  1101. {
  1102. // Format a text string
  1103. return '('.$this->_escape($s).')';
  1104. }
  1105. function _UTF8toUTF16($s)
  1106. {
  1107. // Convert UTF-8 to UTF-16BE with BOM
  1108. $res = "\xFE\xFF";
  1109. $nb = strlen($s);
  1110. $i = 0;
  1111. while($i<$nb)
  1112. {
  1113. $c1 = ord($s[$i++]);
  1114. if($c1>=224)
  1115. {
  1116. // 3-byte character
  1117. $c2 = ord($s[$i++]);
  1118. $c3 = ord($s[$i++]);
  1119. $res .= chr((($c1 & 0x0F)<<4) + (($c2 & 0x3C)>>2));
  1120. $res .= chr((($c2 & 0x03)<<6) + ($c3 & 0x3F));
  1121. }
  1122. elseif($c1>=192)
  1123. {
  1124. // 2-byte character
  1125. $c2 = ord($s[$i++]);
  1126. $res .= chr(($c1 & 0x1C)>>2);
  1127. $res .= chr((($c1 & 0x03)<<6) + ($c2 & 0x3F));
  1128. }
  1129. else
  1130. {
  1131. // Single-byte character
  1132. $res .= "\0".chr($c1);
  1133. }
  1134. }
  1135. return $res;
  1136. }
  1137. function _dounderline($x, $y, $txt)
  1138. {
  1139. // Underline text
  1140. $up = $this->CurrentFont['up'];
  1141. $ut = $this->CurrentFont['ut'];
  1142. $w = $this->GetStringWidth($txt)+$this->ws*substr_count($txt,' ');
  1143. 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);
  1144. }
  1145. function _parsejpg($file)
  1146. {
  1147. // Extract info from a JPEG file
  1148. $a = getimagesize($file);
  1149. if(!$a)
  1150. $this->Error('Missing or incorrect image file: '.$file);
  1151. if($a[2]!=2)
  1152. $this->Error('Not a JPEG file: '.$file);
  1153. if(!isset($a['channels']) || $a['channels']==3)
  1154. $colspace = 'DeviceRGB';
  1155. elseif($a['channels']==4)
  1156. $colspace = 'DeviceCMYK';
  1157. else
  1158. $colspace = 'DeviceGray';
  1159. $bpc = isset($a['bits']) ? $a['bits'] : 8;
  1160. $data = file_get_contents($file);
  1161. return array('w'=>$a[0], 'h'=>$a[1], 'cs'=>$colspace, 'bpc'=>$bpc, 'f'=>'DCTDecode', 'data'=>$data);
  1162. }
  1163. function _parsepng($file)
  1164. {
  1165. // Extract info from a PNG file
  1166. $f = fopen($file,'rb');
  1167. if(!$f)
  1168. $this->Error('Can\'t open image file: '.$file);
  1169. $info = $this->_parsepngstream($f,$file);
  1170. fclose($f);
  1171. return $info;
  1172. }
  1173. function _parsepngstream($f, $file)
  1174. {
  1175. // Check signature
  1176. if($this->_readstream($f,8)!=chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10))
  1177. $this->Error('Not a PNG file: '.$file);
  1178. // Read header chunk
  1179. $this->_readstream($f,4);
  1180. if($this->_readstream($f,4)!='IHDR')
  1181. $this->Error('Incorrect PNG file: '.$file);
  1182. $w = $this->_readint($f);
  1183. $h = $this->_readint($f);
  1184. $bpc = ord($this->_readstream($f,1));
  1185. if($bpc>8)
  1186. $this->Error('16-bit depth not supported: '.$file);
  1187. $ct = ord($this->_readstream($f,1));
  1188. if($ct==0 || $ct==4)
  1189. $colspace = 'DeviceGray';
  1190. elseif($ct==2 || $ct==6)
  1191. $colspace = 'DeviceRGB';
  1192. elseif($ct==3)
  1193. $colspace = 'Indexed';
  1194. else
  1195. $this->Error('Unknown color type: '.$file);
  1196. if(ord($this->_readstream($f,1))!=0)
  1197. $this->Error('Unknown compression method: '.$file);
  1198. if(ord($this->_readstream($f,1))!=0)
  1199. $this->Error('Unknown filter method: '.$file);
  1200. if(ord($this->_readstream($f,1))!=0)
  1201. $this->Error('Interlacing not supported: '.$file);
  1202. $this->_readstream($f,4);
  1203. $dp = '/Predictor 15 /Colors '.($colspace=='DeviceRGB' ? 3 : 1).' /BitsPerComponent '.$bpc.' /Columns '.$w;
  1204. // Scan chunks looking for palette, transparency and image data
  1205. $pal = '';
  1206. $trns = '';
  1207. $data = '';
  1208. do
  1209. {
  1210. $n = $this->_readint($f);
  1211. $type = $this->_readstream($f,4);
  1212. if($type=='PLTE')
  1213. {
  1214. // Read palette
  1215. $pal = $this->_readstream($f,$n);
  1216. $this->_readstream($f,4);
  1217. }
  1218. elseif($type=='tRNS')
  1219. {
  1220. // Read transparency info
  1221. $t = $this->_readstream($f,$n);
  1222. if($ct==0)
  1223. $trns = array(ord(substr($t,1,1)));
  1224. elseif($ct==2)
  1225. $trns = array(ord(substr($t,1,1)), ord(substr($t,3,1)), ord(substr($t,5,1)));
  1226. else
  1227. {
  1228. $pos = strpos($t,chr(0));
  1229. if($pos!==false)
  1230. $trns = array($pos);
  1231. }
  1232. $this->_readstream($f,4);
  1233. }
  1234. elseif($type=='IDAT')
  1235. {
  1236. // Read image data block
  1237. $data .= $this->_readstream($f,$n);
  1238. $this->_readstream($f,4);
  1239. }
  1240. elseif($type=='IEND')
  1241. break;
  1242. else
  1243. $this->_readstream($f,$n+4);
  1244. }
  1245. while($n);
  1246. if($colspace=='Indexed' && empty($pal))
  1247. $this->Error('Missing palette in '.$file);
  1248. $info = array('w'=>$w, 'h'=>$h, 'cs'=>$colspace, 'bpc'=>$bpc, 'f'=>'FlateDecode', 'dp'=>$dp, 'pal'=>$pal, 'trns'=>$trns);
  1249. if($ct>=4)
  1250. {
  1251. // Extract alpha channel
  1252. if(!function_exists('gzuncompress'))
  1253. $this->Error('Zlib not available, can\'t handle alpha channel: '.$file);
  1254. $data = gzuncompress($data);
  1255. $color = '';
  1256. $alpha = '';
  1257. if($ct==4)
  1258. {
  1259. // Gray image
  1260. $len = 2*$w;
  1261. for($i=0;$i<$h;$i++)
  1262. {
  1263. $pos = (1+$len)*$i;
  1264. $color .= $data[$pos];
  1265. $alpha .= $data[$pos];
  1266. $line = substr($data,$pos+1,$len);
  1267. $color .= preg_replace('/(.)./s','$1',$line);
  1268. $alpha .= preg_replace('/.(.)/s','$1',$line);
  1269. }
  1270. }
  1271. else
  1272. {
  1273. // RGB image
  1274. $len = 4*$w;
  1275. for($i=0;$i<$h;$i++)
  1276. {
  1277. $pos = (1+$len)*$i;
  1278. $color .= $data[$pos];
  1279. $alpha .= $data[$pos];
  1280. $line = substr($data,$pos+1,$len);
  1281. $color .= preg_replace('/(.{3})./s','$1',$line);
  1282. $alpha .= preg_replace('/.{3}(.)/s','$1',$line);
  1283. }
  1284. }
  1285. unset($data);
  1286. $data = gzcompress($color);
  1287. $info['smask'] = gzcompress($alpha);
  1288. if($this->PDFVersion<'1.4')
  1289. $this->PDFVersion = '1.4';
  1290. }
  1291. $info['data'] = $data;
  1292. return $info;
  1293. }
  1294. function _readstream($f, $n)
  1295. {
  1296. // Read n bytes from stream
  1297. $res = '';
  1298. while($n>0 && !feof($f))
  1299. {
  1300. $s = fread($f,$n);
  1301. if($s===false)
  1302. $this->Error('Error while reading stream');
  1303. $n -= strlen($s);
  1304. $res .= $s;
  1305. }
  1306. if($n>0)
  1307. $this->Error('Unexpected end of stream');
  1308. return $res;
  1309. }
  1310. function _readint($f)
  1311. {
  1312. // Read a 4-byte integer from stream
  1313. $a = unpack('Ni',$this->_readstream($f,4));
  1314. return $a['i'];
  1315. }
  1316. function _parsegif($file)
  1317. {
  1318. // Extract info from a GIF file (via PNG conversion)
  1319. if(!function_exists('imagepng'))
  1320. $this->Error('GD extension is required for GIF support');
  1321. if(!function_exists('imagecreatefromgif'))
  1322. $this->Error('GD has no GIF read support');
  1323. $im = imagecreatefromgif($file);
  1324. if(!$im)
  1325. $this->Error('Missing or incorrect image file: '.$file);
  1326. imageinterlace($im,0);
  1327. $f = @fopen('php://temp','rb+');
  1328. if($f)
  1329. {
  1330. // Perform conversion in memory
  1331. ob_start();
  1332. imagepng($im);
  1333. $data = ob_get_clean();
  1334. imagedestroy($im);
  1335. fwrite($f,$data);
  1336. rewind($f);
  1337. $info = $this->_parsepngstream($f,$file);
  1338. fclose($f);
  1339. }
  1340. else
  1341. {
  1342. // Use temporary file
  1343. $tmp = tempnam('.','gif');
  1344. if(!$tmp)
  1345. $this->Error('Unable to create a temporary file');
  1346. if(!imagepng($im,$tmp))
  1347. $this->Error('Error while saving to temporary file');
  1348. imagedestroy($im);
  1349. $info = $this->_parsepng($tmp);
  1350. unlink($tmp);
  1351. }
  1352. return $info;
  1353. }
  1354. function _newobj()
  1355. {
  1356. // Begin a new object
  1357. $this->n++;
  1358. $this->offsets[$this->n] = strlen($this->buffer);
  1359. $this->_out($this->n.' 0 obj');
  1360. }
  1361. function _putstream($s)
  1362. {
  1363. $this->_out('stream');
  1364. $this->_out($s);
  1365. $this->_out('endstream');
  1366. }
  1367. function _out($s)
  1368. {
  1369. // Add a line to the document
  1370. if($this->state==2)
  1371. $this->pages[$this->page] .= $s."\n";
  1372. else
  1373. $this->buffer .= $s."\n";
  1374. }
  1375. function _putpages()
  1376. {
  1377. $nb = $this->page;
  1378. if(!empty($this->AliasNbPages))
  1379. {
  1380. // Replace number of pages
  1381. for($n=1;$n<=$nb;$n++)
  1382. $this->pages[$n] = str_replace($this->AliasNbPages,$nb,$this->pages[$n]);
  1383. }
  1384. if($this->DefOrientation=='P')
  1385. {
  1386. $wPt = $this->DefPageSize[0]*$this->k;
  1387. $hPt = $this->DefPageSize[1]*$this->k;
  1388. }
  1389. else
  1390. {
  1391. $wPt = $this->DefPageSize[1]*$this->k;
  1392. $hPt = $this->DefPageSize[0]*$this->k;
  1393. }
  1394. $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
  1395. for($n=1;$n<=$nb;$n++)
  1396. {
  1397. // Page
  1398. $this->_newobj();
  1399. $this->_out('<</Type /Page');
  1400. $this->_out('/Parent 1 0 R');
  1401. if(isset($this->PageSizes[$n]))
  1402. $this->_out(sprintf('/MediaBox [0 0 %.2F %.2F]',$this->PageSizes[$n][0],$this->PageSizes[$n][1]));
  1403. $this->_out('/Resources 2 0 R');
  1404. if(isset($this->PageLinks[$n]))
  1405. {
  1406. // Links
  1407. $annots = '/Annots [';
  1408. foreach($this->PageLinks[$n] as $pl)
  1409. {
  1410. $rect = sprintf('%.2F %.2F %.2F %.2F',$pl[0],$pl[1],$pl[0]+$pl[2],$pl[1]-$pl[3]);
  1411. $annots .= '<</Type /Annot /Subtype /Link /Rect ['.$rect.'] /Border [0 0 0] ';
  1412. if(is_string($pl[4]))
  1413. $annots .= '/A <</S /URI /URI '.$this->_textstring($pl[4]).'>>>>';
  1414. else
  1415. {
  1416. $l = $this->links[$pl[4]];
  1417. $h = isset($this->PageSizes[$l[0]]) ? $this->PageSizes[$l[0]][1] : $hPt;
  1418. $annots .= sprintf('/Dest [%d 0 R /XYZ 0 %.2F null]>>',1+2*$l[0],$h-$l[1]*$this->k);
  1419. }
  1420. }
  1421. $this->_out($annots.']');
  1422. }
  1423. if($this->PDFVersion>'1.3')
  1424. $this->_out('/Group <</Type /Group /S /Transparency /CS /DeviceRGB>>');
  1425. $this->_out('/Contents '.($this->n+1).' 0 R>>');
  1426. $this->_out('endobj');
  1427. // Page content
  1428. $p = ($this->compress) ? gzcompress($this->pages[$n]) : $this->pages[$n];
  1429. $this->_newobj();
  1430. $this->_out('<<'.$filter.'/Length '.strlen($p).'>>');
  1431. $this->_putstream($p);
  1432. $this->_out('endobj');
  1433. }
  1434. // Pages root
  1435. $this->offsets[1] = strlen($this->buffer);
  1436. $this->_out('1 0 obj');
  1437. $this->_out('<</Type /Pages');
  1438. $kids = '/Kids [';
  1439. for($i=0;$i<$nb;$i++)
  1440. $kids .= (3+2*$i).' 0 R ';
  1441. $this->_out($kids.']');
  1442. $this->_out('/Count '.$nb);
  1443. $this->_out(sprintf('/MediaBox [0 0 %.2F %.2F]',$wPt,$hPt));
  1444. $this->_out('>>');
  1445. $this->_out('endobj');
  1446. }
  1447. function _putfonts()
  1448. {
  1449. $nf = $this->n;
  1450. foreach($this->diffs as $diff)
  1451. {
  1452. // Encodings
  1453. $this->_newobj();
  1454. $this->_out('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.']>>');
  1455. $this->_out('endobj');
  1456. }
  1457. foreach($this->FontFiles as $file=>$info)
  1458. {
  1459. // Font file embedding
  1460. $this->_newobj();
  1461. $this->FontFiles[$file]['n'] = $this->n;
  1462. $font = file_get_contents($this->fontpath.$file,true);
  1463. if(!$font)
  1464. $this->Error('Font file not found: '.$file);
  1465. $compressed = (substr($file,-2)=='.z');
  1466. if(!$compressed && isset($info['length2']))
  1467. $font = substr($font,6,$info['length1']).substr($font,6+$info['length1']+6,$info['length2']);
  1468. $this->_out('<</Length '.strlen($font));
  1469. if($compressed)
  1470. $this->_out('/Filter /FlateDecode');
  1471. $this->_out('/Length1 '.$info['length1']);
  1472. if(isset($info['length2']))
  1473. $this->_out('/Length2 '.$info['length2'].' /Length3 0');
  1474. $this->_out('>>');
  1475. $this->_putstream($font);
  1476. $this->_out('endobj');
  1477. }
  1478. foreach($this->fonts as $k=>$font)
  1479. {
  1480. // Font objects
  1481. $this->fonts[$k]['n'] = $this->n+1;
  1482. $type = $font['type'];
  1483. $name = $font['name'];
  1484. if($type=='Core')
  1485. {
  1486. // Core font
  1487. $this->_newobj();
  1488. $this->_out('<</Type /Font');
  1489. $this->_out('/BaseFont /'.$name);
  1490. $this->_out('/Subtype /Type1');
  1491. if($name!='Symbol' && $name!='ZapfDingbats')
  1492. $this->_out('/Encoding /WinAnsiEncoding');
  1493. $this->_out('>>');
  1494. $this->_out('endobj');
  1495. }
  1496. elseif($type=='Type1' || $type=='TrueType')
  1497. {
  1498. // Additional Type1 or TrueType/OpenType font
  1499. $this->_newobj();
  1500. $this->_out('<</Type /Font');
  1501. $this->_out('/BaseFont /'.$name);
  1502. $this->_out('/Subtype /'.$type);
  1503. $this->_out('/FirstChar 32 /LastChar 255');
  1504. $this->_out('/Widths '.($this->n+1).' 0 R');
  1505. $this->_out('/FontDescriptor '.($this->n+2).' 0 R');
  1506. if(isset($font['diffn']))
  1507. $this->_out('/Encoding '.($nf+$font['diffn']).' 0 R');
  1508. else
  1509. $this->_out('/Encoding /WinAnsiEncoding');
  1510. $this->_out('>>');
  1511. $this->_out('endobj');
  1512. // Widths
  1513. $this->_newobj();
  1514. $cw = &$font['cw'];
  1515. $s = '[';
  1516. for($i=32;$i<=255;$i++)
  1517. $s .= $cw[chr($i)].' ';
  1518. $this->_out($s.']');
  1519. $this->_out('endobj');
  1520. // Descriptor
  1521. $this->_newobj();
  1522. $s = '<</Type /FontDescriptor /FontName /'.$name;
  1523. foreach($font['desc'] as $k=>$v)
  1524. $s .= ' /'.$k.' '.$v;
  1525. if(!empty($font['file']))
  1526. $s .= ' /FontFile'.($type=='Type1' ? '' : '2').' '.$this->FontFiles[$font['file']]['n'].' 0 R';
  1527. $this->_out($s.'>>');
  1528. $this->_out('endobj');
  1529. }
  1530. else
  1531. {
  1532. // Allow for additional types
  1533. $mtd = '_put'.strtolower($type);
  1534. if(!method_exists($this,$mtd))
  1535. $this->Error('Unsupported font type: '.$type);
  1536. $this->$mtd($font);
  1537. }
  1538. }
  1539. }
  1540. function _putimages()
  1541. {
  1542. foreach(array_keys($this->images) as $file)
  1543. {
  1544. $this->_putimage($this->images[$file]);
  1545. unset($this->images[$file]['data']);
  1546. unset($this->images[$file]['smask']);
  1547. }
  1548. }
  1549. function _putimage(&$info)
  1550. {
  1551. $this->_newobj();
  1552. $info['n'] = $this->n;
  1553. $this->_out('<</Type /XObject');
  1554. $this->_out('/Subtype /Image');
  1555. $this->_out('/Width '.$info['w']);
  1556. $this->_out('/Height '.$info['h']);
  1557. if($info['cs']=='Indexed')
  1558. $this->_out('/ColorSpace [/Indexed /DeviceRGB '.(strlen($info['pal'])/3-1).' '.($this->n+1).' 0 R]');
  1559. else
  1560. {
  1561. $this->_out('/ColorSpace /'.$info['cs']);
  1562. if($info['cs']=='DeviceCMYK')
  1563. $this->_out('/Decode [1 0 1 0 1 0 1 0]');
  1564. }
  1565. $this->_out('/BitsPerComponent '.$info['bpc']);
  1566. if(isset($info['f']))
  1567. $this->_out('/Filter /'.$info['f']);
  1568. if(isset($info['dp']))
  1569. $this->_out('/DecodeParms <<'.$info['dp'].'>>');
  1570. if(isset($info['trns']) && is_array($info['trns']))
  1571. {
  1572. $trns = '';
  1573. for($i=0;$i<count($info['trns']);$i++)
  1574. $trns .= $info['trns'][$i].' '.$info['trns'][$i].' ';
  1575. $this->_out('/Mask ['.$trns.']');
  1576. }
  1577. if(isset($info['smask']))
  1578. $this->_out('/SMask '.($this->n+1).' 0 R');
  1579. $this->_out('/Length '.strlen($info['data']).'>>');
  1580. $this->_putstream($info['data']);
  1581. $this->_out('endobj');
  1582. // Soft mask
  1583. if(isset($info['smask']))
  1584. {
  1585. $dp = '/Predictor 15 /Colors 1 /BitsPerComponent 8 /Columns '.$info['w'];
  1586. $smask = array('w'=>$info['w'], 'h'=>$info['h'], 'cs'=>'DeviceGray', 'bpc'=>8, 'f'=>$info['f'], 'dp'=>$dp, 'data'=>$info['smask']);
  1587. $this->_putimage($smask);
  1588. }
  1589. // Palette
  1590. if($info['cs']=='Indexed')
  1591. {
  1592. $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
  1593. $pal = ($this->compress) ? gzcompress($info['pal']) : $info['pal'];
  1594. $this->_newobj();
  1595. $this->_out('<<'.$filter.'/Length '.strlen($pal).'>>');
  1596. $this->_putstream($pal);
  1597. $this->_out('endobj');
  1598. }
  1599. }
  1600. function _putxobjectdict()
  1601. {
  1602. foreach($this->images as $image)
  1603. $this->_out('/I'.$image['i'].' '.$image['n'].' 0 R');
  1604. }
  1605. function _putresourcedict()
  1606. {
  1607. $this->_out('/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]');
  1608. $this->_out('/Font <<');
  1609. foreach($this->fonts as $font)
  1610. $this->_out('/F'.$font['i'].' '.$font['n'].' 0 R');
  1611. $this->_out('>>');
  1612. $this->_out('/XObject <<');
  1613. $this->_putxobjectdict();
  1614. $this->_out('>>');
  1615. }
  1616. function _putresources()
  1617. {
  1618. $this->_putfonts();
  1619. $this->_putimages();
  1620. // Resource dictionary
  1621. $this->offsets[2] = strlen($this->buffer);
  1622. $this->_out('2 0 obj');
  1623. $this->_out('<<');
  1624. $this->_putresourcedict();
  1625. $this->_out('>>');
  1626. $this->_out('endobj');
  1627. }
  1628. function _putinfo()
  1629. {
  1630. $this->_out('/Producer '.$this->_textstring('FPDF '.FPDF_VERSION));
  1631. if(!empty($this->title))
  1632. $this->_out('/Title '.$this->_textstring($this->title));
  1633. if(!empty($this->subject))
  1634. $this->_out('/Subject '.$this->_textstring($this->subject));
  1635. if(!empty($this->author))
  1636. $this->_out('/Author '.$this->_textstring($this->author));
  1637. if(!empty($this->keywords))
  1638. $this->_out('/Keywords '.$this->_textstring($this->keywords));
  1639. if(!empty($this->creator))
  1640. $this->_out('/Creator '.$this->_textstring($this->creator));
  1641. $this->_out('/CreationDate '.$this->_textstring('D:'.@date('YmdHis')));
  1642. }
  1643. function _putcatalog()
  1644. {
  1645. $this->_out('/Type /Catalog');
  1646. $this->_out('/Pages 1 0 R');
  1647. if($this->ZoomMode=='fullpage')
  1648. $this->_out('/OpenAction [3 0 R /Fit]');
  1649. elseif($this->ZoomMode=='fullwidth')
  1650. $this->_out('/OpenAction [3 0 R /FitH null]');
  1651. elseif($this->ZoomMode=='real')
  1652. $this->_out('/OpenAction [3 0 R /XYZ null null 1]');
  1653. elseif(!is_string($this->ZoomMode))
  1654. $this->_out('/OpenAction [3 0 R /XYZ null null '.sprintf('%.2F',$this->ZoomMode/100).']');
  1655. if($this->LayoutMode=='single')
  1656. $this->_out('/PageLayout /SinglePage');
  1657. elseif($this->LayoutMode=='continuous')
  1658. $this->_out('/PageLayout /OneColumn');
  1659. elseif($this->LayoutMode=='two')
  1660. $this->_out('/PageLayout /TwoColumnLeft');
  1661. }
  1662. function _putheader()
  1663. {
  1664. $this->_out('%PDF-'.$this->PDFVersion);
  1665. }
  1666. function _puttrailer()
  1667. {
  1668. $this->_out('/Size '.($this->n+1));
  1669. $this->_out('/Root '.$this->n.' 0 R');
  1670. $this->_out('/Info '.($this->n-1).' 0 R');
  1671. }
  1672. function _enddoc()
  1673. {
  1674. $this->_putheader();
  1675. $this->_putpages();
  1676. $this->_putresources();
  1677. // Info
  1678. $this->_newobj();
  1679. $this->_out('<<');
  1680. $this->_putinfo();
  1681. $this->_out('>>');
  1682. $this->_out('endobj');
  1683. // Catalog
  1684. $this->_newobj();
  1685. $this->_out('<<');
  1686. $this->_putcatalog();
  1687. $this->_out('>>');
  1688. $this->_out('endobj');
  1689. // Cross-ref
  1690. $o = strlen($this->buffer);
  1691. $this->_out('xref');
  1692. $this->_out('0 '.($this->n+1));
  1693. $this->_out('0000000000 65535 f ');
  1694. for($i=1;$i<=$this->n;$i++)
  1695. $this->_out(sprintf('%010d 00000 n ',$this->offsets[$i]));
  1696. // Trailer
  1697. $this->_out('trailer');
  1698. $this->_out('<<');
  1699. $this->_puttrailer();
  1700. $this->_out('>>');
  1701. $this->_out('startxref');
  1702. $this->_out($o);
  1703. $this->_out('%%EOF');
  1704. $this->state = 3;
  1705. }
  1706. // End of class
  1707. }
  1708. // Handle special IE contype request
  1709. if(isset($_SERVER['HTTP_USER_AGENT']) && $_SERVER['HTTP_USER_AGENT']=='contype')
  1710. {
  1711. header('Content-Type: application/pdf');
  1712. exit;
  1713. }
  1714. ?>