I’m a new user with dompdf, i’m trying to make pagination and add custom html to specific page number. I see in some posts they have $pdf, PAGE_NUM,PAGE_COUNT… but I dont know how to use them. Here is what I did:
PdfController.php
<?php $html = view('pdf')->render(); $dompdf = new Dompdf(); $dompdf->load_html($html); $canvas = $dompdf->get_canvas(); $cpdf = $canvas->get_cpdf(); $font = $dompdf->getFontMetrics()->get_font("helvetica", "bold"); $firstPageId = $cpdf->getFirstPageId(); $objects = $cpdf->objects; $pages = array_filter($objects, function($v) { return $v['t'] == 'page'; }); $number = 1; foreach($pages as $pageId => $page) { if(($pageId + 1) !== $firstPageId) { $canvas->reopen_object($pageId + 1); $canvas->text(72, 18, "<div>Header: $number</div>", $font, 6, array(0,0,0)); $canvas->close_object(); $number++; } } $dompdf->render(); $dompdf->stream("dompdf_out.pdf", ["Attachment" => false]); exit(0); ?>
And here is html:
<style> body { counter-reset: page; } .footer:not(:first-child) .page-number:before { counter-increment: page; color: red; content: 'Page '; } .footer:not(:first-child) .page-number:after { color: red; content: counter(page); } </style> <script type="text/php"> if ( isset($pdf) ) { $font = Font_Metrics::get_font("helvetica", "bold"); if (PAGE_NUM===2) $pdf->page_text(72, 18, "<div>Header: {PAGE_NUM} of {PAGE_COUNT}</div>", $font, 6, array(0,0,0)); } </script> <div class="footer fixed-section"> <div class="left"> <span class="report-time">08/05/2016 09:43:06</span><br> <span class="promo">Generated by ...</span> </div> <div class="center"> <span class="signature-warning">This is page 0</span> </div> <div class="right"> <span class="page-number"></span> </div> </div> <hr> <div class="footer fixed-section"> <div class="left"> <span class="report-time">08/05/2016 09:43:06</span><br> <span class="promo">Generated by ...</span> </div> <div class="center"> <span class="signature-warning">This is page 1</span> </div> <div class="right"> <span class="page-number"></span> </div> </div> <hr> <div class="footer fixed-section"> <div class="left"> <span class="report-time">08/05/2016 09:43:06</span><br> <span class="promo">Generated by ...</span> </div> <div class="center"> <span class="signature-warning">This is page 2</span> </div> <div class="right"> <span class="page-number"></span> </div> </div> <hr> <div class="footer fixed-section"> <div class="left"> <span class="report-time">08/05/2016 09:43:06</span><br> <span class="promo">Generated by ...</span> </div> <div class="center"> <span class="signature-warning">This is page 3</span> </div> <div class="right"> <span class="page-number"></span> </div> </div> <hr> <div class="footer fixed-section"> <div class="left"> <span class="report-time">08/05/2016 09:43:06</span><br> <span class="promo">Generated by ...</span> </div> <div class="center"> <span class="signature-warning">This is page 4</span> </div> <div class="right"> <span class="page-number"></span> </div> </div> <hr> <div class="footer fixed-section"> <div class="left"> <span class="report-time">08/05/2016 09:43:06</span><br> <span class="promo">Generated by ...</span> </div> <div class="center"> <span class="signature-warning">This is page 5</span> </div> <div class="right"> <span class="page-number"></span> </div> </div> <hr> <div class="footer fixed-section"> <div class="left"> <span class="report-time">08/05/2016 09:43:06</span><br> <span class="promo">Generated by ...</span> </div> <div class="center"> <span class="signature-warning">This is page 6</span> </div> <div class="right"> <span class="page-number"></span> </div> </div> <hr> <div class="footer fixed-section"> <div class="left"> <span class="report-time">08/05/2016 09:43:06</span><br> <span class="promo">Generated by ...</span> </div> <div class="center"> <span class="signature-warning">This is page 7</span> </div> <div class="right"> <span class="page-number"></span> </div> </div> <hr> <div class="footer fixed-section"> <div class="left"> <span class="report-time">08/05/2016 09:43:06</span><br> <span class="promo">Generated by ...</span> </div> <div class="center"> <span class="signature-warning">This is page 8</span> </div> <div class="right"> <span class="page-number"></span> </div> </div> <hr> <div class="footer fixed-section"> <div class="left"> <span class="report-time">08/05/2016 09:43:06</span><br> <span class="promo">Generated by ...</span> </div> <div class="center"> <span class="signature-warning">This is page 9</span> </div> <div class="right"> <span class="page-number"></span> </div> </div> <hr> <div class="footer fixed-section"> <div class="left"> <span class="report-time">08/05/2016 09:43:06</span><br> <span class="promo">Generated by ...</span> </div> <div class="center"> <span class="signature-warning">This is page 10</span> </div> <div class="right"> <span class="page-number"></span> </div> </div> <hr> <div class="footer fixed-section"> <div class="left"> <span class="report-time">08/05/2016 09:43:06</span><br> <span class="promo">Generated by ...</span> </div> <div class="center"> <span class="signature-warning">This is page 11</span> </div> <div class="right"> <span class="page-number"></span> </div> </div> <hr> <div class="footer fixed-section"> <div class="left"> <span class="report-time">08/05/2016 09:43:06</span><br> <span class="promo">Generated by ...</span> </div> <div class="center"> <span class="signature-warning">This is page 12</span> </div> <div class="right"> <span class="page-number"></span> </div> </div> <hr> <div class="footer fixed-section"> <div class="left"> <span class="report-time">08/05/2016 09:43:06</span><br> <span class="promo">Generated by ...</span> </div> <div class="center"> <span class="signature-warning">This is page 13</span> </div> <div class="right"> <span class="page-number"></span> </div> </div> <hr> <div class="footer fixed-section"> <div class="left"> <span class="report-time">08/05/2016 09:43:06</span><br> <span class="promo">Generated by ...</span> </div> <div class="center"> <span class="signature-warning">This is page 14</span> </div> <div class="right"> <span class="page-number"></span> </div> </div>
And my question What is $pdf in script and how to use page_text function to add text inside page dompdf? Or how to do the same in php will better?
Advertisement
Answer
There’s a lot going on here, but I’ll do my best to answer. I’m assuming your using the current release.
First, the Canvas object. This provides direct access to the underlying PDF writer back end. As such you can’t use it to render HTML. Any styling on the text has to be made manually. So writing out HTML into the PDF like you’re attempting to do with the DIV element won’t work. Just write out the text you want.
Second, the $pdf variable. This variable is an eval-local variable you can use within your page scripts and holds a reference to the Canvas object. You don’t need to do anything to access it, just reference it within any Canvas scripts.
In your containing file (e.g. PdfController.php):
$canvas = $dompdf->get_canvas(); $canvas->page_script(<<<EOF if ($PAGE_NUM !== 1) { $font = $fontMetrics->get_font("helvetica", "bold"); $pdf->text(72, 18, "Header: $PAGE_NUM of $PAGE_COUNT", $font, 6, array(0,0,0)); } EOF; );
Note that you don’t need to parse through the PDF objects to find the page you want. Page scripts are run on each page after the HTML has been fully rendered so all you have to do is check the current page.
In your actual HTML document:
<script> if ( !isset($pdf) ) { return; } $pdf->page_script(<<<EOF if ($PAGE_NUM !== 1) { $font = $fontMetrics->get_font("helvetica", "bold"); $pdf->text(72, 18, "Header: $PAGE_NUM of $PAGE_COUNT", $font, 6, array(0,0,0)); } EOF; ); </script>
Note that in order to use HTML-embedded script you have to enable it. This is a security option since HTML-embedded script could be used as a vector to exploit your system if you allow unsanitized user content.
To enable HTML-embedded script:
$dompdf = new Dompdf(['isPhpEnabled' => true]);
Third, about page_script
and page_text
. These Canvas methods are executed in the context of each page of the PDF document after the HTML has been fully rendered. This is different from other methods, such as text
, which are executed only on the active PDF page.
Currently, because of the way Dompdf renders documents the total number of pages ($PAGE_COUNT) only accurately reflects the number of pages after the HTML document has been fully rendered. As such to write out that value to the PDF you have to use either page_text or page_script.
FYI, though you don’t generally mix Canvas script and HTML it is possible to do so in a limited way. Here’s a recent discussion on the topic: https://github.com/dompdf/dompdf/discussions/2475