Advertisement
Guest User

Untitled

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