Visualizzazione dei risultati da 1 a 7 su 7

Discussione: convertire url in pdf

  1. #1

    convertire url in pdf

    ciao, vorrei utilizzare lo script sotto riportato in modo da convertire in pdf una pagina a partire da un url. Lo script però include l'url nel codice, mentre ciò che vorrei fare è dare la possibilità all'utente di inserire l'url da convertire in pdf. Come posso modificare lo script affinchè al posto di ("http://test.ultraglobal.info/govazo/") ci vada l'url proveniente da un form html?

    Codice PHP:
    <?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("[b]MY TEST[/b]");
        
    $result $htmltopdf->convertURL("http://test.ultraglobal.info/govazo/");
        if(
    $result==false)
            echo 
    $htmltopdf->error();
    ?>
    Grazie

  2. #2
    Moderatore di PHP L'avatar di Alhazred
    Registrato dal
    Oct 2003
    Messaggi
    12,505
    Puoi farlo con un form con un campo di input di tipo testo nel quale inserire l'url e poi nella pagina php recuperi tale indirizzo e lo passi alla funzione per la conversione.
    Una cosa così:
    form.html
    codice:
    <form name="form_url" method="post" action="script.php">
        <input type="text" name="url" /> <input type="submit" name="submit" value="Invia" />
    </form>
    script.php
    Codice PHP:
    <?php
        
        
    require('html_to_pdf.inc.php');
        
    $url $_POST["url"];
        
    $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("[b]MY TEST[/b]");
        
    $result $htmltopdf->convertURL($url);
        if(
    $result==false)
            echo 
    $htmltopdf->error();
    ?>

  3. #3
    In alternativa, prova a dare un'occhiata a dompdf

  4. #4
    ti ringrazio per l'aiuto, la modifica che mi hai scritto, sicuramente funziona, ma lo script mi restituisce un errore che non riesce a collegarsi all'host. Scuramente l'errore sta nel secondo file php che viene incluso dentro script.php. Per cui riepilogando i file utilizzati sono i seguneti:

    form.html
    codice:
    <form name="form_url" method="post" action="script.php">     
    <input type="text" name="url" /> 
    <input type="submit" name="submit" value="Invia" /> 
    </form>
    script.php
    Codice PHP:
    <?php
        
        
    require('html_to_pdf.inc.php');
        
    $url $_POST["url"];
        
    $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("[b]MY TEST[/b]");
        
    $result $htmltopdf->convertURL($url);
        if(
    $result==false)
            echo 
    $htmltopdf->error();
    ?>
    html_to_pdf.inc.php
    Codice PHP:
    <?php

        
    /**
         * Genereate PDF from HTML
         * @author Harish Chauhan
         * @version 1.0.0
         * @name HTML_TO_PDF
         */
        
        
    define ("HKC_USE_ABC",1);
        
    define ("HKC_USE_EASYW",2);

        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="")
            {
                if(empty(
    $file))
                    
    $this->saveFile time().".pdf";
                else 
                    
    $this->saveFile =$file;
            }
            
            function 
    downloadFile($file="")
            {
                if(empty(
    $file))
                    
    $this->downloadFile time().".pdf";
                else 
                    
    $this->downloadFile =$file;
            }
            
            function 
    error()
            {
                return  
    $this->error;
            }
            
            function 
    convertHTML($html="")
            {
                if(!empty(
    $html))
                    
    $this->html=$html;
                
    $htmlfile time().".html";
                
    $url "http://".$_SERVER['SERVER_NAME'].dirname($_SERVER['PHP_SELF'])."/".$htmlfile;
                
                
    $this->write_file($htmlfile,$this->html);

                
    $return $this->convertURL($url);
                if(
    is_file($htmlfile))
                    @
    unlink($htmlfile);
                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 "url=".urlencode($this->htmlurl);
                
    $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;

                if(!empty(
    $this->saveFile))        
                    
    $this->write_file($this->saveFile,$pdfdata);
                if(!empty(
    $this->downloadFile))
                    
    $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 "URL=".urlencode($this->htmlurl);
                
    $s_POST_DATA .= "&FORMAT=.pdf";
                
    $pdfdata = @file_get_contents("http://".$this->host.$this->url."?".$s_POST_DATA);
                if(!empty(
    $pdfdata))
                {
                    if(!empty(
    $this->saveFile))        
                        
    $this->write_file($this->saveFile,$pdfdata);
                    if(!empty(
    $this->downloadFile))
                        
    $this->download_file($pdfdata);
                    return 
    true;
                }
                
                
    $pdfdata $this->_sendRequest($s_POST_DATA);
                if(
    $pdfdata===false) return false;
                
                if(!empty(
    $this->saveFile))        
                    
    $this->write_file($this->saveFile,$pdfdata);
                if(!empty(
    $this->downloadFile))
                    
    $this->download_file($pdfdata);

                return 
    $pdfdata;            
            }
            
            function 
    _sendRequest($s_POST_DATA)
            {
                if(
    function_exists("curl_init"))
                    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 .="Content-Length: ".strlen($s_POST_DATA)."\n";
                if(
    $this->_useurl == HKC_USE_ABC && !empty($this->_cookie))
                    
    $s_Request .="Cookie: ".$this->_cookie."\n";
                
    $s_Request .="\n".$s_POST_DATA."\n\n";
                
                
    $fp fsockopen ($this->host$this->port$errno$errstr30);
                if(!
    $fp)
                {
                    
    $this->error "ERROR: $errno - $errstr
    \n"
    ;
                    return 
    false;
                }
                
    fputs ($fp$s_Request);
                while (!
    feof($fp)) {
                    
    $this->GatewayResponse .= fgets ($fp128);
                }
                
    fclose ($fp);

                if(empty(
    $this->_cookie))
                {
                    @
    preg_match("/ASP.NET_SessionId[^;]*/s"$this->GatewayResponse$match);
                    
    $this->_cookie $match[0];
                }

                @
    preg_match("/^(.*?)\r?\n\r?\n(.*)/s"$this->GatewayResponse$match);
                if(
    $this->_useurl == HKC_USE_ABC)
                    @
    preg_match("/^(.*?)\r?\n\r?\n(.*)/s"$match[2], $match);
                
    $this->GatewayResponse =$match[2];

                return 
    $this->GatewayResponse;
            }
            
            function 
    _sendCRequest($s_POST_DATA)
            {
                
    $ch curl_init();
                
    //"http://".$this->host.":".$this->port.$this->url;
                
    curl_setopt$chCURLOPT_URL"http://".$this->host.":".$this->port.$this->url );
                
    curl_setopt$chCURLOPT_POST);
                
    curl_setopt$chCURLOPT_POSTFIELDS,$s_POST_DATA);
                if(
    $this->_useurl == HKC_USE_ABC && !empty($this->_cookie))
                    
    curl_setopt$chCURLOPT_COOKIE,$this->_cookie);
                
    curl_setopt$chCURLOPT_RETURNTRANSFER);
                
    curl_setopt$chCURLOPT_TIMEOUT,30 );
                
    curl_setopt($chCURLOPT_HEADER1);
                
    $this->GatewayResponse=curl_exec$ch );
                if(
    curl_error($ch)!="")
                {
                    
    $this->error "ERROR: ".curl_error($ch)."
    \n"
    ;
                    return 
    false;
                }
                
    curl_close($ch);
                
                if(empty(
    $this->_cookie))
                {
                    @
    preg_match("/ASP.NET_SessionId[^;]*/s"$this->GatewayResponse$match);
                    
    $this->_cookie $match[0];
                }

                @
    preg_match("/^(.*?)\r?\n\r?\n(.*)/s"$this->GatewayResponse$match);
                if(
    $this->_useurl == HKC_USE_ABC)
                    @
    preg_match("/^(.*?)\r?\n\r?\n(.*)/s"$match[2], $match);
                
    $this->GatewayResponse =$match[2];

                return 
    $this->GatewayResponse;
            }

            function 
    write_file($file,$content,$mode="w")
            {
                
    $fp=@fopen($file,$mode);
                if(!
    is_resource($fp))
                    return 
    false;
                
    fwrite($fp,$content);
                
    fclose($fp);
                return 
    true;
            }

            function 
    download_file($pdfdata)
            {
                @
    header("Cache-Control: ");// leave blank to avoid IE errors
                
    @header("Pragma: ");// leave blank to avoid IE errors
                
    @header("Content-type: application/octet-stream");
                @
    header("Content-Disposition: attachment; filename=".$this->downloadFile);
                echo 
    $pdfdata;
            }

        }
    ?>
    probabilmente non riesce a connettersi agli host scritti nel codice?

    Grazie

  5. #5
    Originariamente inviato da Samleo
    In alternativa, prova a dare un'occhiata a dompdf
    dompdf l'ho testato. E' una buona soluzione, ma per ora mi interessa che da un url riesca a creare un pdf

  6. #6
    Se non rircordo male anche dompdf dovrebbe farla una cosa del genere

  7. #7
    Originariamente inviato da Samleo
    Se non rircordo male anche dompdf dovrebbe farla una cosa del genere
    ho fatto dei test su dompdf e permette solo di creare il pdf a partire dal codice html...ho scaricato l'ultima versione...tu a quele ti riferisci? sai indicarmi il nome della pagina php di dompdf che permette di fare questo?

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.