这个问题有点老了,但这就是我从 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;
}
}
}