【问题标题】:dompdf generating PDF in with many blank pages between contentdompdf 在内容之间生成许多空白页的 PDF
【发布时间】:2013-12-29 09:58:44
【问题描述】:

我正在使用 DOMPDF 从 html 生成 pdf 文件。正在生成 PDF 文件,但内容之间出现许多空白页。以下是我的php代码。

<?php
    $html=file_get_contents("views/testnew.html");
//  echo $html;die();
    $paper_orientation = 'landscape';

    ob_start();
    require_once("dompdf/dompdf_config.inc.php");
    $pdfcontent = $html;

    $dompdf = new DOMPDF();
    $dompdf->set_paper('A4', $paper_orientation);
    $dompdf->load_html($pdfcontent);
    $dompdf->render();

//  $pdf = $dompdf->output();
    $pdf=$dompdf->stream("my_pdf.pdf", array("Attachment" => 0));
//  file_put_contents('Brochure.pdf', $pdf);
?>

以下是我在 PDF 文件中编写的 HTML

http://jsfiddle.net/6RmmB/

由于 PDF 正在生成,我认为 PHP 代码没有任何问题。 html中一定有问题,但无法弄清楚到底是什么?

或者我在 PHP 代码中遗漏了什么?

“雇主识别号”旁边的内容出现在大约 8-9 个空白页之后

【问题讨论】:

    标签: php html pdf dompdf html-to-pdf


    【解决方案1】:

    这似乎是由于 dompdf 处理 paging of table cells 的方式存在错误。如果您删除似乎只存在以提供边框的外部表格,则页面将呈现更好。但是,您可能仍需要调整结构/样式,才能得到您想要的。

    例如,而不是这个:

    <table width="100%" border="0" cellpadding="0" cellspacing="0" id="" style="border:1px solid #ccc;color: #000;font-family: Arial,Helvetica,sans-serif;font-size:14px;">
        <tr>
            <td>
                <table width="100%" cellspacing="0" cellpadding="0" border="0" style="border-bottom:1px solid #ccc">
                    <tr>
                        <td width="15%" align="left" valign="top" style="border-right:1px solid #ccc;padding:10px;line-height:20px">Form
                            <img src="/var/www/html/pm5/bodytechniques/working/development/version5/therapist/images/w9-form.JPG" width="83" height="37" style="margin-left:15px" />
                            <br>(Rev. August 2013)
                            <br>Department of the Treasury
                            <br>Internal Revenue Service</td>
                        <td width="69%" align="left" valign="top" style="border-right:1px solid #ccc;padding:10px;text-align:center;line-height:45px">
                            <h1 align="center">Request for Taxpayer Identification Number and Certification</h1>
                        </td>
                        <td width="16%" align="left" valign="top">
                            <h3 style="line-height:25px;padding:10px">Give Form to the requester. Do not send to the IRS</h3>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
    

    这样做:

    <div style="border:1px solid #ccc;color: #000;font-family: Arial,Helvetica,sans-serif;font-size:14px;">
        <table width="100%" cellspacing="0" cellpadding="0" border="0" style="border-bottom:1px solid #ccc">
            <tr>
                <td width="15%" align="left" valign="top" style="border-right:1px solid #ccc;padding:10px;line-height:20px">Form
                    <img src="/var/www/html/pm5/bodytechniques/working/development/version5/therapist/images/w9-form.JPG" width="83" height="37" style="margin-left:15px" />
                    <br>(Rev. August 2013)
                    <br>Department of the Treasury
                    <br>Internal Revenue Service</td>
                <td width="69%" align="left" valign="top" style="border-right:1px solid #ccc;padding:10px;text-align:center;line-height:45px">
                    <h1 align="center">Request for Taxpayer Identification Number and Certification</h1>
                </td>
                <td width="16%" align="left" valign="top">
                    <h3 style="line-height:25px;padding:10px">Give Form to the requester. Do not send to the IRS</h3>
                </td>
            </tr>
        </table>
    </div>
    

    【讨论】:

    • 我认为还有一点需要注意的是 dompdf 在遇到嵌套表时会感到困惑。但是,您可以重组表格以避免嵌套表格
    • 不一定正确(尝试我的debug helper 上的嵌套表示例),但确实 Dompdf 有一些与表格处理相关的怪癖,可能会产生意想不到的结果。同样正确的是,您使用的不必要的嵌套结构越少,Dompdf 呈现文档的效果就越好。
    【解决方案2】:

    当多个表格不适合一页时,我遇到了问题。 解决这个问题很简单,在表格后添加&lt;div style="clear: both;"&gt;。

    【讨论】:

      猜你喜欢
      • 2012-09-13
      • 2013-12-19
      • 2021-11-23
      • 1970-01-01
      • 2021-08-18
      • 2020-07-17
      • 2012-01-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多