Convert HTML/PHP into PDF using PHP

Submitted by: 
Language: 
Visitors have accessed this post 4911 times.

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

  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. <meta" rel="nofollow">http://www.w3.org/1999/xhtml">
  5. <meta</a> http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <title>Untitled Document</title>
  7. <script src="<a href="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
  8. </head>
  9.  
  10.  
  11. <div" rel="nofollow">http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></scri...</a> class="floatL cont_lft_side">
  12.  
  13.  
  14. <p>Converting HTML to PDF</p>
  15.  
  16.  
  17. <div style="width:448px; margin:auto;"></div>
  18. <form method="POST" action="generatePdf.php" id="frmCtnt" name="frmCtnt">
  19.  
  20. <div class="txt_stl">
  21. <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='';">
  22. </div>
  23. <br>
  24. <div class="txt_stl">
  25. <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='';">
  26. </div>
  27. <br>
  28. <div class="txt_stl">
  29. <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='';">
  30. </div>
  31. <br>
  32. <div class="txt_stl">
  33. <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>
  34. </div>
  35. <br>
  36. <input class="or_btn_icon" type="submit" value="Submit">
  37.  
  38. </form>
  39. </div>
  40.  
  41.  
  42.  
  43. </body>
  44. </html>
  45.  
  46.  
  47.  
  48. <script type="text/javascript">
  49.  
  50. var _gaq = _gaq || [];
  51. _gaq.push(['_setAccount', 'UA-38304687-1']);
  52. _gaq.push(['_trackPageview']);
  53.  
  54. (function() {
  55. var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  56. 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';
  57. var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  58. })();
  59.  

Creating Our fpdf.php page

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

Creating Our generatePdf.php

  1. <?php
  2. require('fpdf.php');
  3.  
  4.  
  5. $conect = mysql_connect("localhost", "root", "");
  6. mysql_select_db("db_name", $conect);
  7.  
  8. $sql = "INSERT INTO table_name (name, email, mobile, comment) VALUES ('".$_POST["name"]."', '".$_POST["email"]."', '".$_POST["mobile"]."', '".$_POST["comment"]."');";
  9.  
  10.  
  11.  
  12. class PDF extends FPDF
  13. {
  14. // Page header
  15. function Header()
  16. {
  17. // Logo
  18. $this->Image('logo.jpg',10,6,30);
  19. // Arial bold 15
  20. $this->SetFont('Arial','B',15);
  21. // Move to the right
  22. $this->Cell(80);
  23. // Title
  24. $this->Cell(60,10,'Convert HTML TO PDF',1,0,'C');
  25. // Line break
  26. $this->Ln(20);
  27. }
  28.  
  29. // Page footer
  30. function Footer()
  31. {
  32. // Position at 1.5 cm from bottom
  33. $this->SetY(-15);
  34. // Arial italic 8
  35. $this->SetFont('Arial','I',8);
  36. // Page number
  37. $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
  38. }
  39. }
  40.  
  41. // Instanciation of inherited class
  42. $pdf = new PDF();
  43. $pdf->AliasNbPages();
  44. $pdf->AddPage();
  45. $pdf->SetFont('Times','',12);
  46.  
  47. $pdf->Cell(0,10,'Name : '.$_POST["name"],0,1);
  48. $pdf->Cell(0,10,'Email : '.$_POST["email"],0,1);
  49. $pdf->Cell(0,10,'Mobile : '.$_POST["mobile"],0,1);
  50. $pdf->Cell(0,10,'Comment : '.$_POST["comment"],0,1);
  51.  
  52. $pdf->Output();
  53. ?>

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 @[email protected]


Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.