user3653203 user3653203 - 21 days ago 11
PHP Question

MPDF : independent informations in multiple pages

I actually use MPDF in order to show my HTML code on a PDF.
The problem that I have a lot of payroll employees , and I want to make each payroll on a page.
This code works well.

$html = $this->view->partial('fiche/telechargerfiche.phtml',
array('fichep' => $recuperationFiche));

$mpdf=new mPDF();
foreach ($recuperationFiche as $fiche) {

$mpdf->SetDisplayMode('fullpage');
$mpdf->WriteHTML($html);
$mpdf->SetHTMLFooter("<div style='text-align: center'><img src='pieddepage.jpg' /></div>") ;
$mpdf->Output();
exit;


But the problem is that my payrolls is shown successively in the same page.
Now I want to make each payroll on an independant page .
I have to use a foreach , but I don't know where is the error , because it's shown to me the same result :

$html = $this->view->partial('fiche/telechargerfiche.phtml',
array('fichep' => $recuperationFiche));

$mpdf=new mPDF();
foreach ($recuperationFiche as $fiche) {

$mpdf->SetDisplayMode('fullpage');
$mpdf->WriteHTML($html);
$mpdf->SetHTMLFooter("<div style='text-align: center'><img src='pieddepage.jpg' /></div>") ;
$mpdf->Output();
exit;
}

Answer

You need to :

  • Change your partial file to generate the HTML for only one payroll (fiche).
  • Update your code

Example

$mpdf = new mPDF(); 
$mpdf->SetDisplayMode('fullpage'); 

foreach ($recuperationFiche as $i => $fiche) {

    if($i) { //If not the first payroll then add a new page
        $mpdf->AddPage();
    }
    $out = $this->view->partial(
        'fiche/telechargerfiche.phtml', 
        array('fichep' => $fiche)
    );
    $mpdf->WriteHTML($html);

}

$mpdf->SetHTMLFooter( "" ); 
$mpdf->Output(); 
exit;

Hope it helps