使用 TCPDF 和特定 div 作为内容创建 PDF [英] Creating PDF using TCPDF and a specific div as the content

查看:36
本文介绍了使用 TCPDF 和特定 div 作为内容创建 PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过将收据从 HTML 代码转换为 PDF 来为 Web 应用程序创建收据,以便在应用程序完成后下载.我试图通过将目标 div 的内容分配给表单中的隐藏输入字段来将代码发送到 PHP 文件.最后按下按钮,下面的js函数会在onclick上执行,还有PHP文件:

function print() {var elem = document.getElementById("receipt").innerHTML;document.getElementById("hiddenhtml").value = elem;}

这是表格的粗略轮廓:

<div id="收据">------- 我在 PDF 中想要的内容 ----------

<input type="submit" onclick="print()" value="Print"/><input id="hiddenhtml" type="hidden" value=""/></表单>

其中 hiddenhtml 是隐藏的输入字段.按下打印按钮后,收据 div 的内容将添加到隐藏输入字段的值中.然后我尝试按如下方式传递这个值:

$html = $_POST['hiddenhtml'];$pdf->writeHTML($html, true, false, true, false, '');

在 writeHTML 函数中使用它.但是,会返回一个空白的 PDF 文件.我想知道我是否可以将目标 div 的内容作为 writeHTML 方法中的参数传递.div 没有外部样式,并包含所有符合文档的标签.

解决方案

您需要在文档中添加一个页面来呈现您的 html,然后您需要实际输出您的页面.请参阅示例 pdf 页面.您的代码应如下所示:-

$pdf = new TCPPDF();$pdf->AddPage('P');$pdf->writeHTML($html, true, false, true, false, '');$pdf->输出();

我做了一些进一步的测试并创建了这个代码:-

if(isset($_POST['hidden'])){$pdf = 新的 TCPDF();$pdf->AddPage('P');$html = $_POST['隐藏'];$pdf->writeHTML($html);$pdf->输出();}?><表单方法=POST"><输入名称=隐藏"类型=隐藏"值=<div><h1>收据</h1><p>一些东西</p><p>更多内容</p></div>"/><input type="submit" value="提交"/></表单>

提交时,给了我这个输出:-

如您所见,一个不错的 pdf 页面,其中包含了我的 html.

我建议您将代码简化为这样简单的代码,以帮助排除故障.一旦你开始工作,你就可以增加复杂性.

I am trying to create a receipt for a web application by turning the receipt from the HTML code into a PDF so that it can be downloaded upon completion of the application. I have tried to send the code to a PHP file by assigning the contents of the targeted div to a hidden input field within a form. Upon pressing the button at the end, the js function below is executed onclick, as well as the PHP file:

function print() {
    var elem = document.getElementById("receipt").innerHTML;
    document.getElementById("hiddenhtml").value = elem;
}

Here is a rough outline of the form:

<form method="post" action="htmlpdf.php" target="_blank">
    <div id="receipt">
        ------- CONTENT THAT I WANT IN THE PDF ----------
    </div>
    <input type="submit" onclick="print()" value="Print"/>
    <input id="hiddenhtml" type="hidden" value=""/>
</form>

Where hiddenhtml is a hidden input field. Upon pressing the print button, the contents of the receipt div are added to the value of the hidden input field. I then try to pass this value as follows:

$html = $_POST['hiddenhtml'];
$pdf->writeHTML($html, true, false, true, false, '');

To use it in the writeHTML function. However, a blank PDF file is returned. I was wondering wether I can pass the contents of the targeted div as a parameter in the writeHTML method. The div has no external styling, and contains all compliant tags per the documentation.

解决方案

You need to add a page to the document to render your html in and then you need to actually output your page. See the example pdf page. Your code should look like this:-

$pdf = new TCPDF();
$pdf->AddPage('P');
$pdf->writeHTML($html, true, false, true, false, '');
$pdf->Output();

I did some further testing and created this code:-

if(isset($_POST['hidden'])){
    $pdf = new TCPDF();
    $pdf->AddPage('P');
    $html = $_POST['hidden'];
    $pdf->writeHTML($html);
    $pdf->Output();
}
?>
<form method="POST">
    <input name="hidden" type="hidden" value="
        <div>
            <h1>Receipt</h1>
            <p>Some stuff</p>
            <p>More stuff</p>
        </div>"/>
    <input type="submit" value="Submit"/>
</form>

Which, on submition, gave me this output:-

As you can see, a nice pdf page with my html rendered into it.

I would suggest you reduce your code to something as simple as this to aid trouble shooting. Once you have that working, then you can add complexity.

这篇关于使用 TCPDF 和特定 div 作为内容创建 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆