My code is only adding the footer to all the pages but not the header. I am doing it inside a Laravel project.
// ReportController.php
$mpdf = new Mpdf();
$pagecount = $mpdf->SetSourceFile($request->file('document')->getPathname());
for ($i = 1; $i <= $pagecount; $i++) {
$mpdf->AddPage();
$importedPage = $mpdf->ImportPage($i);
$mpdf->UseTemplate($importedPage);
$mpdf->SetHTMLHeader('Your header content here.');
$mpdf->SetHTMLFooter('Your footer content');
}
$newPdfPath = storage_path('app/public/new_pdf_with_header_footer.pdf');
$mpdf->Output($newPdfPath, \Mpdf\Output\Destination::FILE);
I even tried seeting the Header and Footer before the for loop.
$mpdf->SetHTMLHeader('Your header content here.');
$mpdf->SetHTMLFooter('Your footer content');
for ($i = 1; $i <= $pagecount; $i++) {
$mpdf->AddPage();
// ...
}
Still only the footer is being added.
It looks like the imported page is added on top of the header. To get the content in between the header and footer, we need to adjust the useTemplate() co-ordinates and lengths.
Eg:
This way, the imported page will put the content from imported page inside the co-ordinates with specified length and width. This way the header and footer will be displayed.
Another approach it to add header and footer like normal and use the position:absolute and z-index css. This way the headers and footers overwrite the content at the top and the bottom. Although we can control how for it should be displayed with width and height. Eg:
Not to forget, to add the header for the first page, we need to call setHTMLHeader() before looping the pages.