1

Trying to generate PDFs with html2pdf. I'm able to create the PDFs from creating markup within the function itself, but what I REALLY want to do is include the markup from a separate file using a URL.

The code I have generates a PDF, but the PDF is blank which, I assume, means not HTML is being pulled from the specified url.

require_once('html2pdf/html2pdf.class.php');
$mlsnum = $_GET['mlsnum'];
$url = 'http://www.nexthometown.com/components/com_singleprop/views/singleprop/tmpl/scripts/oh_usda.php?mlsnum='.$mlsnum;
$html2pdf = new HTML2PDF('P', 'A4', 'en');
$html2pdf->getHtmlFromPage($url);
$html2pdf->Output($mlsnum.'.pdf','D');

Is anyone familiar with html2pdf? I've gone through the docs and the examples, but I can't find any reference to this method. I've found the definition here, but it doesn't tell much.

4

2 回答 2

3

http://html2pdf.fr/en/default 这个库是为了帮助创建 PDF 文件,而不是直接转换 HTML 页面。您不能使用<html>, <head>,<body>标签。

于 2013-01-22T19:35:48.817 回答
2

这个问题有点老了,但这就是我从 getHtmlFromPage 方法解决空白页的方法。我没有使用 getHtmlFromPage 方法,而是简单地使用 curl 来获取我想要 pdf 的页面,然后将其作为字符串传递给 html2pdf 瞧!

require_once 'path_to_html2pdf/html2pdf/html2pdf.class.php';
$str_url = 'http://your_url_here.php';
$str_content = get_page($str_url); //get_page can be file_get_contents if your server allows for that function to open a url or a curl function that im posting down below
try{
    $html2pdf = new HTML2PDF('P', 'A4', 'es');
    $html2pdf->pdf->SetDisplayMode('fullpage');
    $html2pdf->writeHTML($str_content );
    $html2pdf->Output('your_file_name.pdf', 'D'); //The 'D' option downloads the pdf instead of just showing it on screen
}
catch(HTML2PDF_exception $e) {
    echo $e;
    exit;
}

//Here is the curl function for get_page
function get_page($str_url){
    if(strpos($str_url, 'http://') === false){
        return file_get_contents($str_url);
    }else{
        if(ini_get('allow_url_fopen')){
            return file_get_contents($str_url);
        }else{
            $curl = curl_init();
            curl_setopt ($curl, CURLOPT_REFERER, strFOARD);
            curl_setopt ($curl, CURLOPT_URL, $str_url);
            curl_setopt ($curl, CURLOPT_TIMEOUT, 30);
            curl_setopt ($curl, CURLOPT_USERAGENT, sprintf("Mozilla/%d.0",rand(4,5)));
            curl_setopt ($curl, CURLOPT_HEADER, 0);
            curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, 0);
            $html = curl_exec ($curl);
            curl_close ($curl);
            return $html;
        }
    }
}
于 2013-11-19T14:18:51.323 回答