【问题标题】:html2pdf include html from pagehtml2pdf 包含页面中的 html
【发布时间】:2013-01-22 19:32:59
【问题描述】:

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

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

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 的定义,但它并没有说明什么。

【问题讨论】:

    标签: php html2pdf


    【解决方案1】:

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

    【讨论】:

    • 我明白了,但是在他们的 wiki 中,对于最新版本,他们有一种方法可以做到这一点:wiki.spipu.net/… 我正在尝试获得有关该功能的帮助,如标题中所述.
    • 尝试从<body>标签内部抓取标记...我认为它应该可以工作
    【解决方案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;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-05-11
      • 1970-01-01
      • 1970-01-01
      • 2011-03-07
      • 2023-04-07
      • 1970-01-01
      • 2023-03-05
      • 2015-10-30
      • 1970-01-01
      相关资源
      最近更新 更多