I am creating a PDF file using DOMPDF. I have a big content to extract in PDF, we need some header in all the pages. So can anyone telme how to add a header and footer in the PDF so that the header will shown in all the pages using DOMPDF.
// your dompdf setup $dompdf = new DOMPDF(); $dompdf->load_html($html); $dompdf->render(); // add the header $canvas = $dompdf->get_canvas(); $font = Font_Metrics::get_font("helvetica", "bold"); // the same call as in my previous example $canvas->page_text(72, 18, "Header: {PAGE_NUM} of {PAGE_COUNT}", $font, 6, array(0 ...
Basic Usage (Convert HTML to PDF) The following example shows how to use Dompdf to convert HTML and generate PDF with minimal configuration. Specify the HTML content in loadHtml() method of Dompdf class. Render HTML as PDF using render() method. Output the generated PDF to Browser using stream() method.
By default it's going to render on 'US Letter', you can change this by using: $dompdf->set_paper(DEFAULT_PDF_PAPER_SIZE, 'portrait'); You can use 'letter', 'legal', 'A4', etc.. With dompdf 0.6 you can also use the CSS @page rule to set your page size/orientation.
Using page-break-inside: auto; basically says to dompdf "do what you would normally do when breaking pages." To force a page break before/after your table you would use page-break-before: always; / page-break-after: always; . To ask dompdf to avoid breaking inside an element you would use page-break-inside: avoid; .
In the 0.6.0 code you will be able to use HTML+CSS to generate headers and footers. There are a few limitations when compared to using inline PHP (e.g. no PAGE_COUNT placeholder yet), so whether or not this is viable depends on your needs.
The following code will produce a two-page document with a header and footer:
<html> <head> <style> @page { margin: 180px 50px; } #header { position: fixed; left: 0px; top: -180px; right: 0px; height: 150px; background-color: orange; text-align: center; } #footer { position: fixed; left: 0px; bottom: -180px; right: 0px; height: 150px; background-color: lightblue; } #footer .page:after { content: counter(page, upper-roman); } </style> <body> <div id="header"> <h1>Widgets Express</h1> </div> <div id="footer"> <p class="page">Page </p> </div> <div id="content"> <p>the first page</p> <p style="page-break-before: always;">the second page</p> </div> </body> </html>
You could also use a combination of the two styles if you needed access to some of the missing functionality. PDF objects and text added using the page_text
method render on top of the HTML content.
There is a FAQ entry on the DOMPDF wiki: Is there a way to add headers and footers or page numbers?.
So you can either add the following "inline PHP"-snippet to your HTML-input (add a similar page_text
-call for your footer):
<script type="text/php"> if ( isset($pdf) ) { $font = Font_Metrics::get_font("helvetica", "bold"); $pdf->page_text(72, 18, "Header: {PAGE_NUM} of {PAGE_COUNT}", $font, 6, array(0,0,0)); } </script>
If you rather want to implement this on your caller-side (meaning in the PHP code directly) you have to call the DOMPDFS's get_canvas()
-method which returns the underlying PDF-Renderer which allows you to call the page_text
method like in the example above. Let me show you what I mean:
// your dompdf setup $dompdf = new DOMPDF(); $dompdf->load_html($html); $dompdf->render(); // add the header $canvas = $dompdf->get_canvas(); $font = Font_Metrics::get_font("helvetica", "bold"); // the same call as in my previous example $canvas->page_text(72, 18, "Header: {PAGE_NUM} of {PAGE_COUNT}", $font, 6, array(0,0,0));
Eventually you have to call page_text
after load_html
(just try it out).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With