Html2Pdf, classe php per convertire file HTML in PDF
Venerdì, Febbraio 29, 2008 16:11 scritto da neryoHtml2Pdf, classe php per convertire file HTML in PDF
Qualche volta capita di dover convertire tramite i propri script php i file HTML in file PDF. Questo esempio mostra l’utilizzo di una classe php che utilizza in modo trasparente web services di ABCPDF o EasySW per convertire online i file sorgente HTML in documenti PDF, restituendoci un file pdf sul nostro web server.
Per la conversione esistono 2 metodi della classe HTML_TO_PDF:
- convertHTML, che accetta una strigna contenente il codice HTML da scrivere su PDF.
- convertURL, che accetta sempre una stringa, ma che é l’url del file da convertire.
E’ possibile chiaramente salvare il file con saveFile e inviare una richiesta di download automatico tramite il metodo downloadFile.
Qui sotto un semplice esempio di utilizzo:
-
< ?php
-
-
require(‘html_to_pdf.inc.php’);
-
-
$htmltopdf = new HTML_TO_PDF();
-
$htmltopdf->useURL(HKC_USE_EASYW); // default HKC_USE_ABC other HKC_USE_EASYW$htmltopdf->saveFile("abc.pdf");
-
-
$htmltopdf->downloadFile("abc.pdf");
-
-
$result = $htmltopdf->convertHTML("<strong>MY TEST");
-
-
//$result = $htmltopdf->convertURL("http://www.google.it");
-
-
if($result==false)
-
-
?></strong>
La classe é stata sviluppata da un programmatore indiano di nome Harish Cahuan ed é scaricabile gratuitamente da phpclasses.org .
Per completezza e per la consultazione vi inserisco il codice php della classe, in ogni caso fate riferimento alla versione scaricabile su phpclasses per evitare di utilizzare questa versione che potrebbe diventare obsoleta.
-
< ?php
-
-
/**
-
* Genereate PDF from HTML
-
* @author Harish Chauhan
-
* @version 1.0.0
-
* @name HTML_TO_PDF
-
*/
-
-
-
class HTML_TO_PDF
-
{
-
var $html = "";
-
var $htmlurl= "";
-
var $error = "";
-
var $host = "";
-
var $port = 80;
-
var $url = "";
-
var $_useurl = "";
-
-
var $saveFile = "";
-
var $downloadFile = "";
-
var $_cookie = "";
-
-
function HTML_TO_PDF($html="",$useurl = HKC_USE_ABC)
-
{
-
$this->html = $html;
-
$this->_useurl=$useurl;
-
}
-
-
function useURL($useurl)
-
{
-
$this->_useurl = $useurl;
-
}
-
-
function saveFile($file="")
-
{
-
else
-
$this->saveFile =$file;
-
}
-
-
function downloadFile($file="")
-
{
-
else
-
$this->downloadFile =$file;
-
}
-
-
function error()
-
{
-
return $this->error;
-
}
-
-
function convertHTML($html="")
-
{
-
$this->html=$html;
-
-
$this->write_file($htmlfile,$this->html);
-
-
$return = $this->convertURL($url);
-
return $return;
-
}
-
-
function convertURL($url)
-
{
-
$this->htmlurl = $url;
-
if($this->_useurl == HKC_USE_ABC)
-
return $this->_convertABC();
-
elseif ($this->_useurl == HKC_USE_EASYW)
-
return $this->_convertEASYW();
-
}
-
-
function _convertABC()
-
{
-
$this->host = "64.39.14.230";
-
-
$this->url = "/pdf-net/cleardoc.aspx";
-
$this->_sendRequest($s_POST_DATA);
-
$s_POST_DATA.= "&PagedOutput=on";
-
$s_POST_DATA.= "&AddLinks=on";
-
$s_POST_DATA.= "&x=30";
-
$s_POST_DATA.= "&y=30";
-
$s_POST_DATA.= "&w=550";
-
$s_POST_DATA.= "&h=704";
-
$s_POST_DATA.= "&UserName=";
-
$s_POST_DATA.= "&Password=";
-
$s_POST_DATA.= "&Timeout=15550";
-
$s_POST_DATA.= "&Submit=Add URL";
-
-
$this->url = "/pdf-net/addurl.aspx";
-
$this->_sendRequest($s_POST_DATA);
-
$this->url = "/pdf-net/showdoc.aspx";
-
$s_POST_DATA = "";
-
-
$pdfdata = $this->_sendRequest($s_POST_DATA);
-
if($pdfdata===false) return false;
-
-
$this->write_file($this->saveFile,$pdfdata);
-
$this->download_file($pdfdata);
-
return $pdfdata;
-
}
-
-
function _convertEASYW()
-
{
-
//http://www.easysw.com/htmldoc/pdf-o-matic.php
-
$this->url= "/htmldoc/pdf-o-matic.php";
-
$this->host="www.easysw.com";
-
$s_POST_DATA .= "&FORMAT=.pdf";
-
{
-
$this->write_file($this->saveFile,$pdfdata);
-
$this->download_file($pdfdata);
-
return true;
-
}
-
-
$pdfdata = $this->_sendRequest($s_POST_DATA);
-
if($pdfdata===false) return false;
-
-
$this->write_file($this->saveFile,$pdfdata);
-
$this->download_file($pdfdata);
-
-
return $pdfdata;
-
}
-
-
function _sendRequest($s_POST_DATA)
-
{
-
return $this->_sendCRequest($s_POST_DATA);
-
else
-
return $this->_sendSRequest($s_POST_DATA);
-
}
-
-
function _sendSRequest($s_POST_DATA)
-
{
-
$s_Request = "POST ".$this->url." HTTP/1.0\n";
-
$s_Request .="Host: ".$this->host.":".$this->port."\n";
-
$s_Request .="Content-Type: application/x-www-form-urlencoded\n";
-
$s_Request .="Cookie: ".$this->_cookie."\n";
-
$s_Request .="\n".$s_POST_DATA."\n\n";
-
-
if(!$fp)
-
{
-
$this->error = "ERROR: $errno - $errstr\n";
-
return false;
-
}
-
}
-
-
{
-
$this->_cookie = $match[0];
-
}
-
-
if($this->_useurl == HKC_USE_ABC)
-
$this->GatewayResponse =$match[2];
-
-
return $this->GatewayResponse;
-
}
-
-
function _sendCRequest($s_POST_DATA)
-
{
-
$ch = curl_init();
-
//"http://".$this->host.":".$this->port.$this->url;
-
curl_setopt( $ch, CURLOPT_URL, "http://".$this->host.":".$this->port.$this->url );
-
curl_setopt( $ch, CURLOPT_POST, 1 );
-
curl_setopt( $ch, CURLOPT_POSTFIELDS,$s_POST_DATA);
-
curl_setopt( $ch, CURLOPT_COOKIE,$this->_cookie);
-
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
-
curl_setopt( $ch, CURLOPT_TIMEOUT,30 );
-
curl_setopt($ch, CURLOPT_HEADER, 1);
-
$this->GatewayResponse=curl_exec( $ch );
-
if(curl_error($ch)!="")
-
{
-
$this->error = "ERROR: ".curl_error($ch)."\n";
-
return false;
-
}
-
curl_close($ch);
-
-
{
-
$this->_cookie = $match[0];
-
}
-
-
if($this->_useurl == HKC_USE_ABC)
-
$this->GatewayResponse =$match[2];
-
-
return $this->GatewayResponse;
-
}
-
-
function write_file($file,$content,$mode="w")
-
{
-
return false;
-
return true;
-
}
-
-
function download_file($pdfdata)
-
{
-
echo $pdfdata;
-
}
-
-
}
-
?>





tasso dice:
18-03-2008 alle 14:41
a me da questo errore:
Notice: Undefined variable: s_POST_DATA in C:\Programmi\EasyPHP\www\intranet\prove\fpdf\html_to_pdf-2006-04-19\html_to_pdf.inc.php on line 88
ERROR: Empty reply from server
admin dice:
18-03-2008 alle 14:52
dai un occhiata qui, ciao!
http://phpclasses.phpsoft.it/discuss/package/2905/thread/6/
tasso dice:
19-03-2008 alle 15:21
ora con questo codice:
useURL(HKC_USE_EASYW); //default HKC_USE_ABC other HKC_USE_EASYW
$htmltopdf->saveFile(”abc.pdf”);
$htmltopdf->downloadFile(”abc.pdf”);
//$result = $htmltopdf->convertHTML(”MY TEST“);
$result = $htmltopdf->convertURL(”http://google.com/”);
if($result==false)
echo $htmltopdf->error();
?>
mi segnala questo errore:
ERROR: Empty reply from server
admin dice:
19-03-2008 alle 16:02
fai una ricerca su google con: ERROR: Empty reply from server
sembra essere un problema inerente a CURL
IT Outsourcein dice:
11-04-2008 alle 08:41
its really niec
bob dice:
08-05-2008 alle 08:47
2VOaqi hi nice site thanks
ale dice:
18-11-2008 alle 11:24
a me da questo errore
Warning: fsockopen() [function.fsockopen]: unable to connect to 64.39.14.230:80 (Impossibile stabilire la connessione. Risposta non corretta della parte connessa dopo l’intervallo di tempo oppure mancata risposta dall’host collegato. ) in C:\www\html_to_pdf.inc.php on line 161
Fatal error: Maximum execution time of 30 seconds exceeded in C:\www\html_to_pdf.inc.php on line 161
devo cambiare il valore di $this->host nell’indirizzo della mia macchina?
ale dice:
18-11-2008 alle 11:26
se metto l’indirizzo della mia macchina non mi da l’errore, ma mi restituisce pagina bianca
admin dice:
18-11-2008 alle 11:46
ciao ale, il primo consiglio che ti posso dare è intanto comprendere come funziona la classe.. perchè è vero che è già fatta e basta usarla, ma per capire quando ci fornisce errori è necessario a volte scendere un pò nel dettaglio.
Questa classe si appoggia a dei web services per creare il pdf e la scelta tra i due web services viene fatta da questa configurazione:
$htmltopdf->useURL(HKC_USE_EASYW); // default HKC_USE_ABC other
Quindi evidentemente o è cambiato l’ip del web services che stai usando o è semplicemente attualmente down. Puoi quindi usare l’altro cambiando la costante nel richiamo del metodo useURL().
Se tu metti il tuo ip locale della tua macchina chiaramente fa una richiesta ad un web services locale che non esiste..
spero di esserti stato di aiuto..
a presto
ciao, neryo
ale dice:
18-11-2008 alle 12:00
adesso non mi da più l’errore, ma mi restituisce una pagina bianca, e il file da scaricare non me lo apre, dice che acrobat non riesce ad aprirlo perchè abc.pdf è danneggiato o non è un formato adatto… tieni presente che io al momento sto facendo test, quindi il mio codice è quello che si scarica da phpclasses come esempio… puoi aiutarmi?
ah un’altra cosa… prima che mi ci spacco ancora il craneo, è possibile assegnare un’intestazione e un piè di pagina? non vedo metodi che lo consentono, ma ad esempio fpdf lo consente
aspetto tue notizie, grazie
admin dice:
18-11-2008 alle 12:42
per forza ti restituisce pagina bianca.. come ti ho detto nella riposta precedente non ha senso sostituire l ip 64.39.14.230 del metodo _convertABC() della classe con il tuo ip locale, così non potrà mai funzionare. Eventualmente puoi solo switchare il web services a cui si appoggia come ti ho indicato, o se pubblicano un nuovo ip o ancora meglio il dominio puoi cambiarlo.. ma non certamente con localhost.
Per quanto riguarda le intestazioni come vedi anche tu dalla classe non cè modo di inserirle.. questa è un classe veloce utile nel caso si vogliano generare pdf da html senza troppe complicazioni, perchè il lavoro sporco viene fatto in remoto sul web services a cui ti appoggi.. ma se vuoi qualcosa di più custom allora usa classi come fpdf..
a presto
ciao
ale dice:
18-11-2008 alle 12:45
non ho più localhost, cmq mi sono reso conto che non posso utilizzare delle librerie che si appoggiando ad un web services, quindi devo abbondonare questa nave.. grazie comunque ciao
admin dice:
18-11-2008 alle 13:06
si tra l’altro cè scritto all’inizio della guida che usa i web services
ciao