mpdf:如何使用包含大页面的 mpdf 生成 pdf 文件

Posted

技术标签:

【中文标题】mpdf:如何使用包含大页面的 mpdf 生成 pdf 文件【英文标题】:mpdf: How to generate a pdf file with mpdf which contain huge pages 【发布时间】:2018-03-13 11:23:49 【问题描述】:

我正在尝试使用 mpdf 生成一个 pdf 文件。它包含超过 50000 页。问题是浏览器可以处理并显示 pdf 文件,当它有 2000 页时,但是当我尝试生成更多页面时,浏览器没有响应,最后,它显示 max_execution_time 错误,尽管我将 max_execution_time 30 增加到 300 表单 php。 ini 文件。

这是我的 mpdf 代码...

<?php
// header('Content-type: application/force-download');
// header("Content-type:application/pdf");
ini_set("memory_limit","128M");
ob_start();


include("fpdf/mpdf/mpdf.php");
// $mpdf=new mPDF('mm','array(140,215)',14,'nikosh'); 
$mpdf = new mPDF('0',array(280,216),12,'nikosh',3,3,'L'); 
$mpdf->SetTopMargin(0);
$mpdf->SetRightMargin(0);
$mpdf->useSubstitutions = false;
$mpdf->simpleTables = true;
$stylesheet = file_get_contents('style.css');

$html='
<div style="display:inline-table">
<div style="float:right;width:48.5%;">
<p style="margin-left:55mm;margin-bottom:0mm;padding-bottom:0mm;font-size:8mm;float:left;"> যশোর  <span class="hide">পৌরসভা </span>   </p>
<div style="margin-left:48mm;margin-top:0mm;padding-top :0mm;"><span style="width:20mm;">পৌরকরের বিল</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="margin-left:50mm;margin-bottom:-10mm;font-size:4mm"> অর্থ বছরঃ   </span></div> 
<div style="margin-left:48mm;margin-top:0mm;padding-top :0mm;"><span style="width:20mm;color:red;">পৌরসভার কপি </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="margin-left:50mm;margin-bottom:-10mm;font-size:4mm"> কিস্তিঃ   </span></div> 

<div class="all_o_i">
        <p  class="o_in">হোল্ডিং নং &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;: ৩৮৯৪-৩৪৩ </p>
        <p  class="o_in">কর দাতার আই ডি &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: </p>
        <p  class="o_in">করদাতার নাম &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: </p>
        <p  class="o_in">রাস্তা/মৌজা/এলাকা/মহল্লা &nbsp;: </p>
        <p  class="o_in">বিলের ঠিকানা &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : </p>
        <p  class="o_in">বিল ইস্যুর তারিখ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : </p>
        <p class="o_in" style="color:red">জমাদানের শেষ তারিখ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: </p>
    </div>
</div>
<div style="float:left;width:48.5%;">
<p style="margin-left:55mm;margin-bottom:0mm;padding-bottom:0mm;font-size:8mm;float:left;"> পৌরসভা   </p>
<div style="margin-left:48mm;margin-top:0mm;padding-top :0mm;"><span style="width:20mm;">পৌরকরের বিল</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="margin-left:50mm;margin-bottom:-10mm;font-size:4mm"> অর্থ বছরঃ   </span></div> 
<div style="margin-left:48mm;margin-top:0mm;padding-top :0mm;"><span style="width:20mm;color:red;">পৌরসভার কপি </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="margin-left:50mm;margin-bottom:-10mm;font-size:4mm"> কিস্তিঃ   </span></div> 
    <div class="all_o_i" style="">
    <p  class="o_in">হোল্ডিং নং &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;: </p>
        <p  class="o_in">কর দাতার আই ডি &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: </p>
        <p  class="o_in">করদাতার নাম &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: </p>
        <p  class="o_in">রাস্তা/মৌজা/এলাকা/মহল্লা &nbsp;: </p>
        <p  class="o_in">বিলের ঠিকানা &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : </p>
        <p  class="o_in">বিল ইস্যুর তারিখ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : </p>
        <p class="o_in" style="color:red">জমাদানের শেষ তারিখ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: </p>
    </div>
</div>
</div>

<div class="tax_secion1"> 
    <table class="tax_table"> 
        <tr>
            <td rowspan="2">করের বিবরন</td>
            <td colspan="1">বকেয়া </td>
            <td colspan="5">চলতি</td>
        </tr>

        <tr>
            <td style="line-height: 85%;"> &nbsp;  &nbsp;  &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp; <span style="float:right">অর্থ </span><br />বছরে   &nbsp;  &nbsp;কিস্তি থেকে </td>
            <td>১ম কিস্তি </td>
            <td>২য় কিস্তি </td>
            <td>৩য় কিস্তি </td>
            <td>৪র্থ কিস্তি </td>
            <td>মোট </td>
        </tr>

        <tr>
            <td>-</td>
            <td>-</td>
            <td>- </td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
        </tr>

        <tr>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
        </tr>

        <tr>
        <tr>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
        </tr>

        <tr>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
        </tr>
        <tr>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
        </tr>
        <tr>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
        </tr>

    </table>
</div>

<div class="tax_secion2"> 
    <table class="tax_table"> 
        <tr>
            <td rowspan="2">করের বিবরন</td>
            <td colspan="1">বকেয়া </td>
            <td colspan="5">চলতি</td>
        </tr>

        <tr>
            <td style="line-height: 85%;"> &nbsp;  &nbsp;  &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp; <span style="float:right">অর্থ </span><br />বছরে   &nbsp;  &nbsp;কিস্তি থেকে </td>
            <td>১ম কিস্তি </td>
            <td>২য় কিস্তি </td>
            <td>৩য় কিস্তি </td>
            <td>৪র্থ কিস্তি </td>
            <td>মোট </td>
        </tr>

        <tr>
            <td>-</td>
            <td>-</td>
            <td>- </td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
        </tr>

        <tr>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
        </tr>

        <tr>
        <tr>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
        </tr>

        <tr>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
        </tr>
        <tr>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
        </tr>
        <tr>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
        </tr>
    </table>
</div>

<div class="tax_secion1"> 
    <table class="tax_table"> 
        <tr>
            <td>করের বিবরন <br /> করের বিবরন  <br /> </td>
            <td>কেবল বকেয়া </td>
            <td>বকেয়া ও ১ম কিস্তি একত্রে </td>
            <td>বকেয়া ও (১ম-২য়)কিস্তি একত্রে </td>
            <td>বকেয়া ও (১ম-৩য়) কিস্তি একত্রে </td>
            <td>বকেয়া ও (১ম-৪র্থ) কিস্তি একত্রে </td>
        </tr>



        <tr>
            <td>-</td>
            <td>-</td>
            <td>- </td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
        </tr>

        <tr>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
        </tr>

        <tr>
        <tr>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
        </tr>

        <tr>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
        </tr>
        <tr>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
        </tr>
        <tr>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
        </tr>
    </table>
</div>

<div class="tax_secion2"> 
    <table class="tax_table"> 
        <tr>
            <td>করের বিবরন <br /> করের বিবরন  <br /> </td>
            <td>কেবল বকেয়া </td>
            <td>বকেয়া ও ১ম কিস্তি একত্রে </td>
            <td>বকেয়া ও (১ম-২য়)কিস্তি একত্রে </td>
            <td>বকেয়া ও (১ম-৩য়) কিস্তি একত্রে </td>
            <td>বকেয়া ও (১ম-৪র্থ) কিস্তি একত্রে </td>
        </tr>



        <tr>
            <td>-</td>
            <td>-</td>
            <td>- </td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
        </tr>

        <tr>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
        </tr>

        <tr>
        <tr>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
        </tr>

        <tr>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
        </tr>
        <tr>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
        </tr>
        <tr>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
        </tr>
    </table>
</div>
';
// $html = '<h1>This is test papers</h1>';

$x = 1; 
while($x <= 1000) 
    $mpdf->AddPage();
    $mpdf->WriteHTML($stylesheet,1);
    $mpdf->WriteHTML($html,2);

    $x++;

ob_end_clean();
$mpdf->Output('test.pdf',I);

exit;

?>

【问题讨论】:

这是我的完整代码 【参考方案1】:

你有两个选择:

在没有max_execution_time 的情况下在后台生成您的 PDF

在处理这种规模的东西时,无论如何你都应该这样做。使用cli 脚本生成PDF,cli php.ini 通常没有max_execution_time 集。将您的 PDF 保存在磁盘上,进一步利用(发送、共享、下载链接)。

浏览器并不意味着显示如此大量的数据,请使用适当的桌面 PDF 查看器以获得更好的性能。

在生成这种大小的 PDF 时,您很可能会遇到内存限制问题,为此,有第二种选择:

在后台以较小的块生成您的 PDF 文件,并将它们与外部程序连接

将您的 HTML 拆分为最多一百页的块,使用 cli 生成单独的文档(每个都运行一个新的干净脚本)并将生成的 PDF 文档与外部程序连接起来,例如。鬼脚本:

gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=out.pdf in1.pdf in2.pdf

或pdftk:

pdftk in1.pdf in2.pdf cat output out.pdf

【讨论】:

你建议怎么做?将整个 html 生成到文件中并使用计划的 cron 作业运行 php cli ...并且有一种方法可以导出所有 html 而不是生成 .pdf 文件?我无法将所有 html 存储到文件中,因为函数 SetHeaderSetFooter

以上是关于mpdf:如何使用包含大页面的 mpdf 生成 pdf 文件的主要内容,如果未能解决你的问题,请参考以下文章

如何获取kartik mpdf内容(html布局)中的页码

mPDF 中动态生成页面的页码

将背景图像复制到使用 mpdf 生成的 pdf 中的其他页面

如何在 mPDF 中使用引导程序?

使用 mPDF 更改第二页的上边距

如何通过mPDF将信息导出到一个单独页面的pdf文件中