0

I have large amount of data that needs to be process and add as tables in PDF using Codeigniter. PDF contain some HTML elements, images, icons, and need lots of alignment using CSS.

So, to achieve this goal I did some search and checked which PDF generation plugin will be suitable for me. So, rather than selecting FPDF, TCPDF or HTMLTOPDF, mPDF etc. I chosen DOMPDF as it is supporting most of the css styles and it serves my purpose as well.

Initially, it was working fine as data was not much heavy and PDF with 10-20 pages was generating fine. But, as data gets increased and need to put about 35-45 pages in my PDF, system gets time out.

So, I changed server settings and made "4GB of RAM with 1 CPU to 8GB of RAM with 4 CPUs". And, increased max_execution_time to 90, memory_limit to 384M but still DOMPDF not rendering large PDFs having pages more than about 35.

I have set landscape mode to download PDF instead save it.

Below is the PHP code in my controller of Codeigniter.

    $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));
    }

For small size PDFs it is working fine. But, when long PDF need to generate execution stops and showing error "Internal Server Error" on page.

When I just print output on browser by putting exit, it works within a minute, but when I send that html to render on PDF and stream it, system stops execution.

I tried to set max_execution_time and memory_limit through htaccess and ini_set but it not helped me.

Hope so anyone will help me.

Gokul Shinde
  • 957
  • 3
  • 10
  • 30
  • I know this is an old question but some time ago I faced a similar problem. Is the view you are using (`backend/Pdf/teammeeting_pdf`) embedding a big external CSS such as bootstrap or similar? – Javier Larroulet Jul 29 '19 at 20:39

2 Answers2

0

Dompdf very slow,please use tcpdf https://tcpdf.org/examples/ very simple,

  • I was compared the both pdf generation tools and know that TCPDF is faster. But, tcpdf is not supporting to CSS styles that is plus point of dompdf. And, so that's why I was chosen it. Thanks for your time. – Gokul Shinde Feb 24 '17 at 10:39
  • $html ='"; – thamothara kannan Feb 24 '17 at 10:47
  • Hi @PathikVejani, Thank you for suggestion. mPDF is really nice tool and more faster than Dompdf. But, one problem is that it not support all CSS styles and also have low pdf quality than dompdf. – Gokul Shinde Feb 24 '17 at 11:58
  • which CSS is not supported. it covers almost all the CSS – Pathik Vejani Feb 24 '17 at 12:01
0

please check this

require_once APPPATH. 'third_party/TCPDF-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');