Convert HTML/PHP into PDF PHP Script


Convert HTML/PHP into PDF PHP Script

In this simple code, it will show on how to convert html/php into pdf. just down the file and follow the instructions below.

Creating Our index.php page

 

Code:
 
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<a href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html" rel="nofollow">http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html</a> xmlns="<a href="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta" rel="nofollow">http://www.w3.org/1999/xhtml">
  6. <head>
  7. <meta</a> http-equiv="Content-Type" content="text/html; charset=utf-8" />
  8. <title>Untitled Document</title>
  9. <script src="<a href="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
  10. </head>
  11. <body>
  12. <div" rel="nofollow">//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></scri...</a> class="floatL cont_lft_side">
  13. <p>Converting HTML to PDF</p>
  14. <div style="width:448px; margin:auto;"></div>
  15. <form method="POST" action="generatePdf.php" id="frmCtnt" name="frmCtnt">
  16. <div class="txt_stl">
  17. <input type="text" value="Enter Your Full Name" name="name" onblur="if(this.value=='')this.value=this.defaultValue;" onfocus="if(this.value==this.defaultValue)this.value='';">
  18. </div>
  19. <br>
  20. <div class="txt_stl">
  21. <input type="text" value="Enter Your Email Id" name="email" onblur="if(this.value=='')this.value=this.defaultValue;" onfocus="if(this.value==this.defaultValue)this.value='';">
  22. </div>
  23. <br>
  24. <div class="txt_stl">
  25. <input type="text" value="Enter Your Contact Number" name="mobile" onblur="if(this.value=='')this.value=this.defaultValue;" onfocus="if(this.value==this.defaultValue)this.value='';">
  26. </div>
  27. <br>
  28. <div class="txt_stl">
  29. <textarea onblur="if(this.value=='')this.value=this.defaultValue;" onfocus="if(this.value==this.defaultValue)this.value='';" rows="" cols="" name="comment">Please Write your Query or Comment here.</textarea>
  30. </div>
  31. <br>
  32. <input class="or_btn_icon" type="submit" value="Submit">
  33. </form>
  34. </div>
  35. </body>
  36. </html>
  37. <script type="text/javascript">
  38. var _gaq = _gaq || [];
  39. _gaq.push(['_setAccount', 'UA-38304687-1']);
  40. _gaq.push(['_trackPageview']);
  41. (function() {
  42. var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  43. ga.src = ('https:' == document.location.protocol ? '<a href="https://ssl'" rel="nofollow">https://ssl'</a> : '<a href="http://www'" rel="nofollow">http://www'</a>) + '.google-analytics.com/ga.js';
  44. var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  45. })();
  46. </script>

 

Creating Our fpdf.php page

 

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

 

Creating Our generatePdf.php

 

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

 

You have successfully created a program that will convert your html/php data into pdf. For suggestions feel free to comment your thoughts below for email me at

 

 


Related Post


phpFreeChat v1.0 Beta2 PHP Script Download phpFreeChat v1.0 Beta2 PHP Script Download phpFreeChat is a free, simple to install, fast, customizable and multi languages chat that uses a simple...
AltPRO – ICO Lending & Altcoin Platform... AltPRO - ICO Lending & Altcoin Platform - nulled PHP Script AltPRO is a Alternative Crypto Currency System Developed with PHP Laravel. AltPRO I...
Youtube Api Files For Free PHP Script Download Youtube Api Files For Free PHP Script Download Every Coders Sharing Youtube Clone Script, But No one Sharing The Files For Download The Videos From...
BitrixEnv PHP Script Free Download BitrixEnv PHP Script Free Download Setting Up Web Environment with PHP Version below 5.2.6 ... free disk space;; Internet connection if you install...
PDO Crud v3.5 – Advanced PHP CRUD application (For... PDO Crud v3.5 – Advanced PHP CRUD application (Form Builder & Database Management) - nulled PHP Script PDOCrud is an advance PHP based CRUD app...

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *

"