TCPDF Memory Issue Allowed memory size of 134217728 bytes exhausted

rekha s picture rekha s · Jun 29, 2015 · Viewed 10.7k times · Source

I am using TCPDF for downloading PDF. For smaller records, it is working fine.But for large data ie 1000 records, it is showing the following error,

Fatal error:  Maximum execution time of 30 seconds exceeded
Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 72 bytes)

I have set memory limit and max execution time in php using ini_set But it not works.I also tried changing the value of memory_limit in php.ini from 128M to 512M.This also not working.

My file is

 <html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="description" content="">
        <meta name="author" content="">
        <title>Customer Receipts Report</title>
        <!-- Bootstrap Core CSS -->

        <style>
             @font-face {
            font-family: SourceSansPro;
  src: url(../../fonts/SourceSansPro-Regular.ttf);
/*  font-family: Roboto;
  src: url(../fonts/Roboto-Regular.ttf);*/
}
table {
  width: 100%;
  border-collapse: collapse;
  border-spacing: 0;
  margin-bottom: 20px;
   font-family:"Armata", sans-serif;
}

table th,
table td {
  padding: 5px;
  background: #EEEEEE;
  text-align: center;
  border-bottom: 1px solid #FFFFFF;
}

table th {
  white-space: nowrap;        
  font-weight: normal;
  background: #4A7171;
  color:white;
  font-size:12px;
}
            html,body{
                overflow: hidden;
            }
            .login{color:#fff}
            .table-responsive table td
            {
                font-weight: 400;
                font-size: 12px;
                color:#222;
                text-align:center !important;
            } 
            .form-control {
                border:1px solid #eee;
            }
            .input-sm
            {
                border-radius: 3px;
            }
            input.input-sm
            {
                margin-left: 10px
            }
            #dataTables-example_info,#dataTables-example_length label
            {
                color:#666;
            }
            #dataTables-example_wrapper {
  width: 86.5% !important;
}
            label{
                text-transform: lowercase;
            }
            .black_overlay{
    display: none;
    position: absolute;
    top: 0%;
    left: 0%;
    width: 100%;
    height: 100%;
    background-color: grey;
    z-index:1001;
    -moz-opacity: 0.8;
    opacity:.80;
    filter: alpha(opacity=80);
        }

        .white_content {
                display: none;
                position: fixed;
                top: 7%;
                left: 20%;
                padding: 16px;
                border: 2px solid #7B8D8C;
                background-color: white;
                z-index:1002;
                overflow: auto;
                height:auto;                                
                width: 30cm;  
                height: 35cm;   
                overflow-y: scroll;
                max-height: 700px;
        }
        #wrapper {width:auto ;margin-bottom:5px;}
        #left{
        float:left;
        width:45%;
        /*background:#7B8D8C*/

        }
        #right{
        /*background:#7B8D8C;*/
        float:right;
        width:45%;
        height:50%;
        }

    </style>
    </head>
    <body>

            <div id="page-wrapper">
                <div class="row">
                    <div class="col-lg-12">
                        <div class="panel-body">
                           <table style='width:100%;' border=0>
<tr>
<td style='vertical-align:top;text-align:left;background-color:black;'><img src="../../images/logo.png" style="width:140px;margin-left: 20px"/></td>
<td colspan=2 style='background-color:black;color:white;font-size:9px;'> <div style='text-align:right;'>
        <div>Company</div>
        <div>Address</div>
        <div>Address</div>
        <div style='color:white !important;'><a href="mailto:[email protected]" style='text-decoration:none;'>[email protected] </a></div>
      </div>
</td></tr>
    </table>


                                 <div class="table-responsive">
                                         <br>

                                          <h3 class="page-header" style="text-align:center;"><i class="fa fa-road fa-fw" style="padding-right:10px;"></i> Receipts Report </h3>
                                <table class="table table-striped table-bordered table-hover" id="dataTables-example" style='width:100% !important;'>
                                    <thead> 
                                        <tr style="background-color:black;color:white;">
                                          <th><i class="fa fa-user"></i> Customer Name</th>
                                            <th><b>#</b> Invoice Number</th>
                                            <th><i class="fa fa-calendar"></i> Invoice Date</th> 
                                             <th ><i class="fa fa-calendar"></i> Due date</th> 

                                             <th><i class="fa fa-credit-card"></i> Payment Status</th> 
                                            <th><i class="fa fa-dollar" style="min-width:10px"></i> Amount</th>                         

                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                        $total=0;
                                        for($i=0;$i<count($obj['ar_info']);$i++){

                                            echo "<tr><td>".$obj['ar_info'][$i]['customer_name']."</td><td>".$obj['ar_info'][$i]['invoice_number']."</td><td>".$obj['ar_info'][$i]['invoice_date']."</td><td>".$obj['ar_info'][$i]['due_date']."</td><td>".$obj['ar_info'][$i]['payment_status']."</td><td>".$obj['ar_info'][$i]['invoice_amt']."</td></tr>";

                                        }
                                        ?>
                                    </tbody>
                                </table>

                            </div>
                            <!-- /.table-responsive -->  

                        </div>
                        <!-- /.panel-body -->
                    </div>
                    <!-- /.col-lg-12 -->
                </div>

            </div>


        </div>   


    </body>
</html>




   $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

$pdf->SetCreator(PDF_CREATOR);
//$pdf->SetAuthor('Nicola Asuni');
//$pdf->SetTitle('TCPDF Example 049');
//$pdf->SetSubject('TCPDF Tutorial');
//$pdf->SetKeywords('TCPDF, PDF, example, test, guide');

$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 049', PDF_HEADER_STRING);

$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
    require_once(dirname(__FILE__).'/lang/eng.php');
    $pdf->setLanguageArray($l);
}
$pdf->SetFont('helvetica', '', 10);

$pdf->AddPage();
//$html = '<h1>Test TCPDF Methods in HTML</h1>
//<h2 style="color:red;">IMPORTANT:</h2>
//<span style="color:red;">If you are using user-generated content, the tcpdf tag can be unsafe.<br />
//You can disable this tag by setting to false the <b>K_TCPDF_CALLS_IN_HTML</b> constant on TCPDF configuration file.</span>
//<h2>write1DBarcode method in HTML</h2>';
$pdf->writeHTML($html, true, 0, true, 0);

$pdf->lastPage();
ob_end_clean();
$pdf->Output('example_049.pdf', 'I');
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="file.pdf"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize('customer-receipts-report.pdf'));
header('Accept-Ranges: bytes');
readfile('customer-receipts-report.pdf');
ob_end_flush(); 

Please anyone help me on this issue.

Answer

Shehary picture Shehary · Jun 29, 2015

Add this line in the your code

set_time_limit(0);

and this

ini_set('memory_limit', '-1');// its not the proper solution but it will do the trick.

Or

ini_set('memory_limit', '640M'); //Set Proper memory limit in your php file