Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: HTML to PDF
Forum PHP.pl > Inne > Hydepark
Indeo
Zobaczcie jaką usługe WEBSERVICES znalazłem - tworzy pliki PDF z dokumentów HTML łącznie z grafiką: http://tomek.granty.pl/ ale jazda smile.gif
nospor
to ma byc dla nas ciekawostka? czy moze chcesz sie jakims kodem podzielic? Poki co kodu nie widze, traktuje wiec to jako ciekawostkę i przenosze na Hydepark
GrayHat
moze byc fajne. udostepnij kod tego smile.gif

ps: ten konwerter troche nie jest zgodny z W3C... sproboj zrobic pdf ze strony h4x.pl

top i logo nie bedzie wyswietlone czyli nie pobiera grafiki z background-image: url();
Fuzja
php.pl -> wyświetliło tylko witaj gościu na szarym tle winksmiley.jpg
coś nie za dobrze działa:]
Seth
Bo musisz dac http://phppl.ezpublish.no/

btw: to nie jest webservices.
Vomit
No fajne. Kodu raczej nie zobaczymy, bo wydaje mi sie, ze Indeo nie jest autorem.
Indeo
Definicja klasy odpowiedzialnej za komunikacje z usługą (html_to_pdf.inc.php) - klasa pochodzi z phpclasses.org:
  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. if(!empty($this->downloadFile))
  112. $this->download_file($pdfdata);
  113. return $pdfdata;
  114. }
  115.  
  116. function _convertEASYW()
  117. {
  118. //http://www.easysw.com/htmldoc/pdf-o-matic.php
  119. $this->url= "/htmldoc/pdf-o-matic.php";
  120. $this->host="www.easysw.com";
  121. $s_POST_DATA = "URL=".urlencode($this->htmlurl);
  122. $s_POST_DATA .= "&FORMAT=.pdf";
  123. $pdfdata = @file_get_contents("http://".$this->host.$this->url."?".$s_POST_DATA);
  124. if(!empty($pdfdata))
  125. {
  126. if(!empty($this->saveFile))
  127. $this->write_file($this->saveFile,$pdfdata);
  128. if(!empty($this->downloadFile))
  129. $this->download_file($pdfdata);
  130. return true;
  131. }
  132.  
  133. $pdfdata = $this->_sendRequest($s_POST_DATA);
  134. if($pdfdata===false) return false;
  135.  
  136. if(!empty($this->saveFile))
  137. $this->write_file($this->saveFile,$pdfdata);
  138. if(!empty($this->downloadFile))
  139. $this->download_file($pdfdata);
  140.  
  141. return $pdfdata;
  142. }
  143.  
  144. function _sendRequest($s_POST_DATA)
  145. {
  146. if(function_exists("curl_init"))
  147. return $this->_sendCRequest($s_POST_DATA);
  148. else
  149. return $this->_sendSRequest($s_POST_DATA);
  150. }
  151.  
  152. function _sendSRequest($s_POST_DATA)
  153. {
  154. $s_Request = "POST ".$this->url." HTTP/1.0\n";
  155. $s_Request .="Host: ".$this->host.":".$this->port."\n";
  156. $s_Request .="Content-Type: application/x-www-form-urlencoded\n";
  157. $s_Request .="Content-Length: ".strlen($s_POST_DATA)."\n";
  158. if($this->_useurl == HKC_USE_ABC && !empty($this->_cookie))
  159. $s_Request .="Cookie: ".$this->_cookie."\n";
  160. $s_Request .="\n".$s_POST_DATA."\n\n";
  161.  
  162. $fp = fsockopen ($this->host, $this->port, $errno, $errstr, 30);
  163. if(!$fp)
  164. {
  165. $this->error = "ERROR: $errno - $errstr<br />\n";
  166. return false;
  167. }
  168. fputs ($fp, $s_Request);
  169. while (!feof($fp)) {
  170. $this->GatewayResponse .= fgets ($fp, 128);
  171. }
  172. fclose ($fp);
  173.  
  174. if(empty($this->_cookie))
  175. {
  176. @preg_match("/ASP.NET_SessionId[^;]*/s", $this->GatewayResponse, $match);
  177. $this->_cookie = $match[0];
  178. }
  179.  
  180. @preg_match("/^(.*?)\r?\n\r?\n(.*)/s", $this->GatewayResponse, $match);
  181. if($this->_useurl == HKC_USE_ABC)
  182. @preg_match("/^(.*?)\r?\n\r?\n(.*)/s", $match[2], $match);
  183. $this->GatewayResponse =$match[2];
  184.  
  185. return $this->GatewayResponse;
  186. }
  187.  
  188. function _sendCRequest($s_POST_DATA)
  189. {
  190. $ch = curl_init();
  191. //"http://".$this->host.":".$this->port.$this->url;
  192. curl_setopt( $ch, CURLOPT_URL, "http://".$this->host.":".$this->port.$this->url );
  193. curl_setopt( $ch, CURLOPT_POST, 1 );
  194. curl_setopt( $ch, CURLOPT_POSTFIELDS,$s_POST_DATA);
  195. if($this->_useurl == HKC_USE_ABC && !empty($this->_cookie))
  196. curl_setopt( $ch, CURLOPT_COOKIE,$this->_cookie);
  197. curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
  198. curl_setopt( $ch, CURLOPT_TIMEOUT,30 );
  199. curl_setopt($ch, CURLOPT_HEADER, 1);
  200. $this->GatewayResponse=curl_exec( $ch );
  201. if(curl_error($ch)!="")
  202. {
  203. $this->error = "ERROR: ".curl_error($ch)."<br />\n";
  204. return false;
  205. }
  206. curl_close($ch);
  207.  
  208. if(empty($this->_cookie))
  209. {
  210. @preg_match("/ASP.NET_SessionId[^;]*/s", $this->GatewayResponse, $match);
  211. $this->_cookie = $match[0];
  212. }
  213.  
  214. @preg_match("/^(.*?)\r?\n\r?\n(.*)/s", $this->GatewayResponse, $match);
  215. if($this->_useurl == HKC_USE_ABC)
  216. @preg_match("/^(.*?)\r?\n\r?\n(.*)/s", $match[2], $match);
  217. $this->GatewayResponse =$match[2];
  218.  
  219. return $this->GatewayResponse;
  220. }
  221.  
  222. function write_file($file,$content,$mode="w")
  223. {
  224. $fp=@fopen($file,$mode);
  225. if(!is_resource($fp))
  226. return false;
  227. fwrite($fp,$content);
  228. fclose($fp);
  229. return true;
  230. }
  231.  
  232. function download_file($pdfdata)
  233. {
  234. @header("Cache-Control: ");// leave blank to avoid IE errors
  235. @header("Pragma: ");// leave blank to avoid IE errors
  236. @header("Content-type: application/octet-stream");
  237. @header("Content-Disposition: attachment; filename=".$this->downloadFile);
  238. echo $pdfdata;
  239. }
  240.  
  241. }
  242. ?>


A to kod jaki wykorzystałem na stronie (index.php):
  1. <?php
  2.  
  3. if($_GET['url']){
  4. require('html_to_pdf.inc.php');
  5. $htmltopdf = new HTML_TO_PDF();
  6.  
  7. //$htmltopdf->useURL(HKC_USE_EASYW); // default HKC_USE_ABC other HKC_USE_EASYW
  8. $htmltopdf->saveFile("abc.pdf");
  9. $htmltopdf->downloadFile("abc.pdf");
  10. //$result = $htmltopdf->convertHTML("<b>MY TEST</b>");
  11. $result = $htmltopdf->convertURL("$_GET[url]");
  12. if($result==false)
  13. echo $htmltopdf->error();
  14. }else{
  15.  
  16. ?>
  17. Podaj adres strony do wygenerowania pliku pdf
  18. <form action=index.php>
  19. <input type=text name=url value='http://'><input type=submit value=send>
  20. </form>
  21.  
  22. <?php
  23.  
  24. }
  25.  
  26. ?>
eai
A co będzie jeśli http://www.easysw.com/htmldoc/pdf-o-matic.php nie będzie odpowiadał sad.gifquestionmark.gif?

Szkoda że to nie jest cały kod jaki jest potrzebny do wygenerowania PDF'a

Klasa fajna. n
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.