【问题标题】:PHP, Codeigniter, Dompdf: HTML generating fine but exectuion time out whlie rederning PDFPHP、Codeigniter、Dompdf:HTML 生成良好但读取 PDF 时执行超时
【发布时间】:2017-02-24 19:07:18
【问题描述】:

我有大量数据需要使用 Codeigniter 处理并添加为 PDF 中的表格。 PDF 包含一些 HTML 元素、图像、图标,并且需要使用 CSS 进行大量对齐。

所以,为了实现这个目标,我进行了一些搜索并检查了哪个 PDF 生成插件适合我。因此,我没有选择 FPDF、TCPDF 或 HTMLTOPDF、mPDF 等,而是选择了 DOMPDF,因为它支持大多数 css 样式,并且也符合我的目的。

最初,它运行良好,因为数据量不大,10-20 页的 PDF 生成良好。但是,随着数据的增加并且需要在我的 PDF 中放置大约 35-45 页,系统会超时。

所以,我更改了服务器设置,并将“4GB RAM 和 1 个 CPU 改为 8GB RAM 和 4 个 CPU”。并且,将 max_execution_time 增加到 90,将 memory_limit 增加到 384M,但 DOMPDF 仍然无法呈现页面超过 35 页的大型 PDF。

我已将横向模式设置为下载 PDF 而不是保存它。

下面是我的 Codeigniter 控制器中的 PHP 代码。

    $this->load->library('pdf');

    $teaminfo=$this->pdf_model->getTeamName($teamid);
    $data=array();
    if($teaminfo != false)
    {
        $teamname=$teaminfo->team_name;
        $data['teamname']=$teamname;
        $formateddate=date('F j, Y');
        $data['teammeetingdate']=$formateddate;

        //add code to get and pass the terms and replacement array
        $termsList=$this->getTermsArray($teamid);
        $data['searchterms']=$termsList['searchterms'];
        $data['replacement']=$termsList['replacement'];

        //get teammeeting roster
        $data['rosterlist']=$this->getRostertable($teamid);

        $personwisedata=$this->getteammemberData($teamid);
        $data['personwiseData']=$personwisedata;

        $kmresponse=$this->get_last_six_month_keymetrics_details_by_userid_without_owner($teamid);
        $data['noownerkmdata']=$kmresponse;

        //set pdf title
        $teamnametitle='My PDF Notes - '.$teamname.' '.date("m.d.Y"); 
        $pdftitle=$teamnametitle.'.pdf';

      $this->pdf->set_paper('a4', 'landscape');
      $this->pdf->set_option( 'enable_font_subsetting' , true );
      $this->pdf->load_view('backend/Pdf/teammeeting_pdf', $data);
      $this->pdf->render();
      $this->pdf->stream($pdftitle,array("Attachment" => 0));
    }

对于小尺寸的 PDF,它可以正常工作。但是,当长 PDF 需要生成执行停止并在页面上显示错误“Internal Server Error”时。

当我通过退出在浏览器上打印输出时,它会在一分钟内工作,但是当我发送该 html 以在 PDF 上呈现并流式传输时,系统会停止执行。

我尝试通过 htaccess 和 ini_set 设置 max_execution_time 和 memory_limit 但它没有帮助我。

希望有人能帮助我。

【问题讨论】:

  • 我知道这是一个老问题,但前段时间我遇到了类似的问题。您正在使用的视图 (backend/Pdf/teammeeting_pdf) 是否嵌入了大型外部 CSS,例如 bootstrap 或类似的?

标签: php css codeigniter pdf dompdf


【解决方案1】:

Dompdf 很慢,请使用 tcpdf https://tcpdf.org/examples/ 很简单,

【讨论】:

  • 我比较了这两种 pdf 生成工具,知道 TCPDF 更快。但是,tcpdf 不支持作为 dompdf 优点的 CSS 样式。而且,这就是我被选中的原因。感谢您的宝贵时间。
  • $html ='";
  • 嗨@PathikVejani,谢谢你的建议。 mPDF 是一个非常好的工具,而且比 Dompdf 更快。但是,一个问题是它不支持所有的 CSS 样式,而且 pdf 的质量也比 dompdf 低。
  • 不支持哪些 CSS。它涵盖了几乎所有的 CSS
【解决方案2】:

请检查一下

require_once APPPATH。 'third_party/TCPD-master/tcpdf.php';

            $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
            // remove default header/footer
            $pdf->setPrintHeader(false);
            $pdf->setPrintFooter(false);
            // set default monospaced font
            $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
            // set margins
            $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
            // set auto page breaks
            $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
            //$pdf->SetFont('times', '', 10);// Font Name, Style, Size, Other file name
            $pdf->SetFont('times', '', 12);
            // Add a page
            $pdf->AddPage();
            // set text shadow effect
            $pdf->setTextShadow(array('enabled'=>true, 'depth_w'=>0.2, 'depth_h'=>0.2, 'color'=>array(196,196,196), 'opacity'=>1, 'blend_mode'=>'Normal'));

            $html ='
                <style>
                .invoice_font{
                    font-size:25px;
                    font-family: "Times New Roman", Times, serif;
                }
                .title img{
                    margin-top:20px;
                }
                .addFirst{
                    font-size:10px;
                    font-weight:bold;
                    font-family: "Times New Roman", Times, serif;
                }
                .addAnother{
                    font-size:10px;
                    font-family: "Times New Roman", Times, serif;
                }
                .invoice_sec_font
                {
                    font-size:10px;
                    font-family: "Times New Roman", Times, serif;
                }
                .DescriptionDiv{
                    background-color:#3e3e3e;
                    color:#ffffff;
                    font-size:10px;
                    font-family: "Times New Roman", Times, serif;
                }
                .TermsConditionDiv{
                    font-size:10px;
                    font-family: "Times New Roman", Times, serif;
                }
                </style>

            <table class="first" cellpadding="4" cellspacing="6" border="0">
                <tr>
                    <td>
                        <h1 class="title"><img src="http://cloudnowtech.com/assets/img/cn_logo.png" style="width: 200px;height: 50px;"></h1>
                        <div>
                            <br/>
                            <span class="addFirst">CloudNow Technologies Pvt. Ltd.</span><br/>
                            <span class="addAnother">CIN - U74999TN2014PTC096441</span><br/>
                            <span class="addAnother">No. 61, Chamiers House, Chamiers Road, R.A Puram</span><br/>
                            <span class="addAnother">Chennai Tamil Nadu 600028</span><br/>
                            <span class="addAnother">India</span>
                        </div>
                    </td>
                    <td align="right"></td>
                    <td align="right">
                        <div class="test">
                            <span class="invoice_font">Invoice</span><br/>
                            <span>Invoice ID :';
                    $html .= 'INV-'.$this->session->userdata('ORDER_ID').'</span>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>
                        <div>
                            <br/>
                            <span class="addFirst">Bill To.</span><br/>
                            <span class="addAnother">';
                    $html .= $this->session->userdata('fname')." ".$this->session->userdata('lname').'</span><br/><span class="addAnother">';
                    $html .= $this->session->userdata('username').'</span><br/>';
                    $html .= '<span class="addAnother"></span><br/>
                                        <span class="addAnother"></span>
                            </div>
                                </td>
                                <td align="right">
                                    <div class="test"><br/>
                                        <span class="invoice_sec_font">Invoice Date :</span><br/>
                                    </div>
                                </td>
                                <td align="right">
                                    <div class="test"><br/>
                                        <span class="invoice_sec_font">';
            $html .= date("d-M-Y").'&nbsp;&nbsp;</span><br/></div></td></tr></table>';

            $html .= '<table class="secs" cellpadding="5" cellspacing="0" border="0">
            <tr class="DescriptionDiv"><td>Number Of User</td> <  <td align="right"> Amount </td></tr><tr><td>';
            $html .= $this->session->userdata('new_users').'User</td><td align="right">';
            $html .= $this->session->userdata('amount').'</td>
            </tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
            </tr><tr><td></td> <td align="right"> Total </td> <td align="right">';
            $html .= $this->session->userdata('amount').'</td></tr></table>';

            $html .= "<br></br><br></br><br></br><br></br><br></br><br></br><br></br><br></br><br></br><br></br><br></br><br></br>";

            $html .= '<table cellpadding="5" cellspacing="0" border="0" class="TermsConditionDiv">
                <tr>
                    <td>Terms & Conditions No TDS has to be deducted on the payment of cloudNow Licenses. A declaration will be submitted
                    for the same. Please note that this estimate is just for reference.</td>
                </tr>
            </table>';

            // output the HTML content
            $pdf->writeHTML($html, true, false, true, false, '');
            $pdf->lastPage();
            //$pdf->Output('example_025.pdf', 'I');
            $DynamicNameofPic = rand(1000,10000)."_Invoice.pdf";
            $FileNameDynamic = "path".$DynamicNameofPic;
            $pdf->Output($FileNameDynamic,'F');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-10
    • 1970-01-01
    相关资源
    最近更新 更多