seshadry
seshadry

Reputation: 26

Loop in creating mutliple PDF in DOMPDF

I want to loop my PDF content in this, currently I'm using DOMPDF for creating PDF. While looping the PDF content, I did not any pdf content. If I remove the loop, I'm getting the pdf. Actually, I want a PDF which contains 5 pages.

Can anyone help me? Thanks in Advance.

<?php
include('config/config.php');
require_once('dompdf_config.inc.php');
error_reporting(0);
for ($i = 0; $i <= 5; $i++) {
    //here is my content to loop
    $pdf_content = '<body>
                <table width="450" border="0" cellpadding="5" class="table_data">
                  <tr>
                    <td class="left_col">3. Chassis No</td>
                    <td><b>: ' . $chass_no . '    </b></td>
                  </tr>
                  <tr>
                    <td class="left_col">4. Engine No.  </td>
                    <td>    <b> : ' . $engine_id . '        </b></td>
                  </tr>
                 </table>
                </body>
                ';
    //create pdf function
    function createPDF($pdf_userid, $pdf_content, $pdf_For, $filename)
    {
        $path   = 'UsersActivityReports/';
        $dompdf = new DOMPDF();
        $dompdf->load_html($pdf_content);
        $dompdf->render();
        $output = $dompdf->output();
        file_put_contents($path . $filename, $output);
        file_get_contents('output1.php');

        return $filename;
    }

    $name      = date("d-m-y") . rand() . '.pdf';
    $reportPDF = createPDF(12, $pdf_content, 'activity_Report', $name);
}
//loop ends here
?>
<iframe class="prv_pdf" src="/UsersActivityReports/<?php echo $name ?>#zoom=50" height="300px" width="440px">

Upvotes: 0

Views: 2080

Answers (1)

BrianS
BrianS

Reputation: 13914

First see this question. Declaring a function inside a loop will result in an error on the second time the code is encountered. So to start move that outside the loop.

Second, you're generating a new PDF on each loop and saving that PDF to the file file each time. So you will only ever get a one-page PDF. What you probably want to do is generate the HTML content on each loop and then generate the PDF when you're done.

Try something like this (simplified based on what's actually in the questions):

require_once('dompdf_config.inc.php');

function createPDF($pdf_content, $filename) {
    $dompdf = new DOMPDF();
    $dompdf->load_html($pdf_content);
    $dompdf->render();
    $output = $dompdf->output();
    file_put_contents($filename, $output);
}

$pdf_content = '
  <html>
    <head>
      <style>
        .table_data { page-break-before: always; }
        .table_data:first-child { page-break-before: never; }
      </style>
    </head>
    <body>
';
for ($i = 0; $i <= 5; $i++) {
    $pdf_content = '
      <table width="450" border="0" cellpadding="5" class="table_data">
        <tr>
          <td class="left_col">3. Chassis No</td>
          <td>: <b>' . $chass_no . '</b></td>
        </tr>
        <tr>
          <td class="left_col">4. Engine No.  </td>
          <td>: <b>' . $engine_id . '</b></td>
        </tr>
      </table>
    ';
}
$html_content = '</body></html>';

$name = 'UsersActivityReports/' . date("d-m-y") . rand() . '.pdf';
createPDF($pdf_content, $name);
?>

<iframe class="prv_pdf" src="<?php echo $name ?>#zoom=50" height="300px" width="440px">

Upvotes: 1

Related Questions