Skip to main content

HTML to PDF Converter Script

Sometimes you need to convert your HTML file to PDF file. This
example will be useful in converting you online HTML file as well as
your HTML source to PDF document.
Here I will send URL to convert
file to PDF. If your page is not accessible online, then you can use
function:convertHTML for converting your HTML source to pdf. This line
is commented below. saveFile method will save your generated data to
your desired location, If your location (folder) is world writable
place.

Page:html_to_pdf_test.php

  1. <?php  
  2.  require('html_to_pdf.inc.php');  
  3.  $htmltopdf = new HTML_TO_PDF();  
  4.   
  5.  //$htmltopdf->useURL(HKC_USE_EASYW);  //default HKC_USE_ABC other HKC_USE_EASYW  
  6.  $htmltopdf->saveFile("abc.pdf");  
  7.  $htmltopdf->downloadFile("abc.pdf");  
  8.  //$result = $htmltopdf->convertHTML("<strong>MY TEST</strong>");  
  9.  $result = $htmltopdf->convertURL("http://satya61229.blogspot.com/");  
  10.  if($result==false)  
  11.  echo $htmltopdf->error();  
  12. ?>  

Page: html_to_pdf.inc.php

  1. <?php  
  2.   
  3. /** 
  4. * Genereate PDF from HTML 
  5. * @author Harish Chauhan 
  6. * @version 1.0.0 
  7. * @name HTML_TO_PDF 
  8. */  
  9.   
  10. define ("HKC_USE_ABC",1);  
  11. define ("HKC_USE_EASYW",2);  
  12.   
  13. class HTML_TO_PDF  
  14. {  
  15. var $html     = "";  
  16. var $htmlurl"";  
  17. var $error     = "";  
  18. var $host    = "";  
  19. var $port    = 80;  
  20. var $url    = "";  
  21. var $_useurl  = "";  
  22.   
  23. var $saveFile = "";  
  24. var $downloadFile = "";  
  25. var $_cookie = "";  
  26.   
  27. function HTML_TO_PDF($html="",$useurl = HKC_USE_ABC)  
  28. {  
  29. $this->html = $html;  
  30. $this->_useurl=$useurl;  
  31. }  
  32.   
  33. function useURL($useurl)  
  34. {  
  35.     $this->_useurl = $useurl;  
  36. }  
  37.   
  38. function saveFile($file="")  
  39. {  
  40.     if(empty($file))  
  41.     $this->saveFile = time().".pdf";  
  42.     else  
  43.     $this->saveFile =$file;  
  44. }  
  45.   
  46. function downloadFile($file="")  
  47. {  
  48.     if(empty($file))  
  49.     $this->downloadFile = time().".pdf";  
  50.     else  
  51.     $this->downloadFile =$file;  
  52. }  
  53.   
  54. function error()  
  55. {  
  56.     return  $this->error;  
  57. }  
  58.   
  59. function convertHTML($html="")  
  60. {  
  61.     if(!empty($html))  
  62.     $this->html=$html;  
  63.     $htmlfile = time().".html";  
  64.     $url = "http://".$_SERVER['SERVER_NAME'].dirname($_SERVER['PHP_SELF'])."/".$htmlfile;  
  65.   
  66.     $this->write_file($htmlfile,$this->html);  
  67.   
  68.     $return = $this->convertURL($url);  
  69.     if(is_file($htmlfile))  
  70.     @unlink($htmlfile);  
  71.     return $return;  
  72. }  
  73.   
  74. function convertURL($url)  
  75. {  
  76.     $this->htmlurl = $url;  
  77.     if($this->_useurl == HKC_USE_ABC)  
  78.     return $this->_convertABC();  
  79.     elseif ($this->_useurl == HKC_USE_EASYW)  
  80.     return $this->_convertEASYW();  
  81. }  
  82.   
  83. function _convertABC()  
  84. {  
  85.     $this->host = "64.39.14.230";  
  86.   
  87.     $this->url = "/pdf-net/cleardoc.aspx";  
  88.     $this->_sendRequest($s_POST_DATA);  
  89.     $s_POST_DATA = "url=".urlencode($this->htmlurl);  
  90.     $s_POST_DATA.= "&PagedOutput=on";  
  91.     $s_POST_DATA.= "&AddLinks=on";  
  92.     $s_POST_DATA.= "&x=30";  
  93.     $s_POST_DATA.= "&y=30";  
  94.     $s_POST_DATA.= "&w=550";  
  95.     $s_POST_DATA.= "&h=704";  
  96.     $s_POST_DATA.= "&UserName=";  
  97.     $s_POST_DATA.= "&Password=";  
  98.     $s_POST_DATA.= "&Timeout=15550";  
  99.     $s_POST_DATA.= "&Submit=Add URL";  
  100.   
  101.     $this->url = "/pdf-net/addurl.aspx";  
  102.     $this->_sendRequest($s_POST_DATA);  
  103.     $this->url = "/pdf-net/showdoc.aspx";  
  104.     $s_POST_DATA = "";  
  105.   
  106.     $pdfdata = $this->_sendRequest($s_POST_DATA);  
  107.     if($pdfdata===false) return false;  
  108.   
  109.     if(!empty($this->saveFile))  
  110.         $this->write_file($this->saveFile,$pdfdata);  
  111.   
  112.     if(!empty($this->downloadFile))  
  113.         $this->download_file($pdfdata);  
  114.   
  115.     return $pdfdata;  
  116. }  
  117.   
  118. function _convertEASYW()  
  119. {  
  120.     //http://www.easysw.com/htmldoc/pdf-o-matic.php  
  121.     $this->url= "/htmldoc/pdf-o-matic.php";  
  122.     $this->host="www.easysw.com";  
  123.     $s_POST_DATA = "URL=".urlencode($this->htmlurl);  
  124.     $s_POST_DATA .= "&FORMAT=.pdf";  
  125.     $pdfdata = @file_get_contents("http://".$this->host.$this->url."?".$s_POST_DATA);  
  126.     if(!empty($pdfdata))  
  127.     {  
  128.         if(!empty($this->saveFile))  
  129.         $this->write_file($this->saveFile,$pdfdata);  
  130.         if(!empty($this->downloadFile))  
  131.         $this->download_file($pdfdata);  
  132.   
  133.         return true;  
  134.     }  
  135.   
  136.     $pdfdata = $this->_sendRequest($s_POST_DATA);  
  137.     if($pdfdata===false) return false;  
  138.   
  139.     if(!empty($this->saveFile))  
  140.         $this->write_file($this->saveFile,$pdfdata);  
  141.     if(!empty($this->downloadFile))  
  142.         $this->download_file($pdfdata);  
  143.   
  144.     return $pdfdata;  
  145. }  
  146.   
  147. function _sendRequest($s_POST_DATA)  
  148. {  
  149.     if(function_exists("curl_init"))  
  150.     return $this->_sendCRequest($s_POST_DATA);  
  151.     else  
  152.     return $this->_sendSRequest($s_POST_DATA);  
  153. }  
  154.   
  155. function _sendSRequest($s_POST_DATA)  
  156. {  
  157.     $s_Request = "POST ".$this->url." HTTP/1.0\n";  
  158.     $s_Request .="Host: ".$this->host.":".$this->port."\n";  
  159.     $s_Request .="Content-Type: application/x-www-form-urlencoded\n";  
  160.     $s_Request .="Content-Length: ".strlen($s_POST_DATA)."\n";  
  161.     if($this->_useurl == HKC_USE_ABC && !empty($this->_cookie))  
  162.     $s_Request .="Cookie: ".$this->_cookie."\n";  
  163.     $s_Request .="\n".$s_POST_DATA."\n\n";  
  164.   
  165.     $fp = fsockopen ($this->host, $this->port, $errno$errstr, 30);  
  166.     if(!$fp)  
  167.     {  
  168.     $this->error = "ERROR: $errno - $errstr 
  169.     \n";  
  170.     return false;  
  171.     }  
  172.     fputs ($fp$s_Request);  
  173.     while (!feof($fp)) {  
  174.     $this->GatewayResponse .= fgets ($fp, 128);  
  175.     }  
  176.     fclose ($fp);  
  177.   
  178.     if(empty($this->_cookie))  
  179.     {  
  180.     @preg_match("/ASP.NET_SessionId[^;]*/s"$this->GatewayResponse, $match);  
  181.     $this->_cookie = $match[0];  
  182.     }  
  183.   
  184.     @preg_match("/^(.*?)\r?\n\r?\n(.*)/s"$this->GatewayResponse, $match);  
  185.     if($this->_useurl == HKC_USE_ABC)  
  186.     @preg_match("/^(.*?)\r?\n\r?\n(.*)/s"$match[2], $match);  
  187.     $this->GatewayResponse =$match[2];  
  188.   
  189.     return $this->GatewayResponse;  
  190. }  
  191.   
  192. function _sendCRequest($s_POST_DATA)  
  193. {  
  194.     $ch = curl_init();  
  195.     //"http://".$this->host.":".$this->port.$this->url;  
  196.     curl_setopt( $ch, CURLOPT_URL, "http://".$this->host.":".$this->port.$this->url );  
  197.     curl_setopt( $ch, CURLOPT_POST, 1 );  
  198.     curl_setopt( $ch, CURLOPT_POSTFIELDS,$s_POST_DATA);  
  199.     if($this->_useurl == HKC_USE_ABC && !empty($this->_cookie))  
  200.     curl_setopt( $ch, CURLOPT_COOKIE,$this->_cookie);  
  201.     curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );  
  202.     curl_setopt( $ch, CURLOPT_TIMEOUT,30 );  
  203.     curl_setopt($ch, CURLOPT_HEADER, 1);  
  204.     $this->GatewayResponse=curl_exec( $ch );  
  205.     if(curl_error($ch)!="")  
  206.     {  
  207.     $this->error = "ERROR: ".curl_error($ch)." 
  208.     \n";  
  209.     return false;  
  210.     }  
  211.     curl_close($ch);  
  212.   
  213.     if(empty($this->_cookie))  
  214.     {  
  215.     @preg_match("/ASP.NET_SessionId[^;]*/s"$this->GatewayResponse, $match);  
  216.     $this->_cookie = $match[0];  
  217.     }  
  218.   
  219.     @preg_match("/^(.*?)\r?\n\r?\n(.*)/s"$this->GatewayResponse, $match);  
  220.     if($this->_useurl == HKC_USE_ABC)  
  221.     @preg_match("/^(.*?)\r?\n\r?\n(.*)/s"$match[2], $match);  
  222.     $this->GatewayResponse =$match[2];  
  223.   
  224.     return $this->GatewayResponse;  
  225. }  
  226.   
  227. function write_file($file,$content,$mode="w")  
  228. {  
  229.     $fp=@fopen($file,$mode);  
  230.     if(!is_resource($fp))  
  231.     return false;  
  232.     fwrite($fp,$content);  
  233.     fclose($fp);  
  234.     return true;  
  235. }  
  236.   
  237. function download_file($pdfdata)  
  238. {  
  239.     @header("Cache-Control: ");// leave blank to avoid IE errors  
  240.     @header("Pragma: ");// leave blank to avoid IE errors  
  241.     @header("Content-type: application/octet-stream");  
  242.     @header("Content-Disposition: attachment; filename=".$this->downloadFile);  
  243.     echo $pdfdata;  
  244. }  
  245.   
  246. }  
  247. ?>  

Above example is provided on phpclasses.org written by Harish.

ref: http://www.satya-weblog.com/2007/05/html-to-pdf-converter-script.html