首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >html2pdf包含来自页面的html

html2pdf包含来自页面的html
EN

Stack Overflow用户
提问于 2013-01-23 03:32:59
回答 2查看 7.2K关注 0票数 1

正在尝试使用html2pdf生成PDF。我可以通过在函数本身中创建标记来创建PDF,但我真正想做的是使用URL包含来自单独文件的标记。

我的代码生成了一个PDF,但是PDF是空白的,我认为这意味着没有从指定的url中拉出HTML。

代码语言:javascript
复制
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');

有人熟悉html2pdf吗?我已经浏览了文档和示例,但我找不到任何对此方法的引用。我找到了定义here,但它并没有说明太多问题。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-01-23 03:35:49

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

票数 3
EN

Stack Overflow用户

发布于 2013-11-19 22:18:51

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

代码语言:javascript
复制
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;
        }
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14466577

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档