How to get the total number of pages in DOMPDF?

For example, Page 1 of 5 .

Here's an online example on how to get part of Page 1 , but not part of 5 . It's him:

 .pagenum:before { content: "Page " counter(page); } 

I use versions 0.6 and $PAGE_NUM and $PAGE_COUNT does not work.

+6
php dompdf
source share
8 answers

Embedded PHP is disabled by default for security reasons, you need to enable it yourself in dompdf_config.custom.inc.php. See here .

As long as the total number of pages is not supported with the CSS that you use, we plan to make it work in the final 0.6.

+8
source share

For people coming here using the new version of DomPdf

Since dompdf 0.7.0 , the dompdf_config.inc.php file dompdf_config.inc.php been deleted (and there is no longer link): all dompdf parameters must be set at runtime.

This means that you need to create a new instance of the Options class

 $domPdfOptions = new Options(); 

And then you can enable inline PHP using the following line

 $domPdfOptions->set("isPhpEnabled", true); 

Other codes are correct and will show page number and page count

 <script type="text/php"> if (isset($pdf)) { $x = 72; $y = 18; $text = "{PAGE_NUM} of {PAGE_COUNT}"; $font = $fontMetrics->get_font("helvetica", "bold"); $size = 6; $color = array(255,0,0); $word_space = 0.0; // default $char_space = 0.0; // default $angle = 0.0; // default $pdf->page_text($x, $y, $text, $font, $size, $color, $word_space, $char_space, $angle); } </script> 

Update As @ london-smith noted, this also works for DomPDF 0.8.1

+6
source share

Make sure you include inline_pdf in dompdf.

Use this code, you can put it wherever you want, it gets the height and width of the document and puts the / total _pages page in the lower right corner.

 <script type = "text/php"> if ( isset($pdf) ) { $pdf->page_script(' if ($PAGE_COUNT > 1) { $font = Font_Metrics::get_font("Arial, Helvetica, sans-serif", "normal"); $size = 12; $pageText = $PAGE_NUM . "/" . $PAGE_COUNT; $y = $pdf->get_height() - 24; $x = $pdf->get_width() - 15 - Font_Metrics::get_text_width($pageText, $font, $size); $pdf->text($x, $y, $pageText, $font, $size); } '); } 

Seen at the end of this page

+4
source share

$PAGE_COUNT - total number of pages.

Example

 <script type="text/php"> if (isset($pdf) ) { echo $PAGE_COUNT; } </script> 

Documentation

Update

If you are using an old version where this is not supported, please upgrade. Otherwise, you may be out of luck.

0
source share

In php, you can access dompdf variables:

 $PAGE_NUM the current page number $PAGE_COUNT the total number of pages in the document 

more details http://code.google.com/p/dompdf/wiki/Usage

0
source share

See the attachment named issue121.php in this error report. Worked for me. As far as I understand, you cannot repeat the page number, but you can draw it somewhere.

http://code.google.com/p/dompdf/issues/detail?id=121

0
source share

Hi, this is mi full code ... after HTML puts this.

 <? require_once("dompdf/dompdf_config.inc.php"); $dompdf = new DOMPDF(); $dompdf->load_html(ob_get_clean()); $dompdf->render(); $canvas = $dompdf->get_canvas(); $font = Font_Metrics::get_font("helvetica", "bold"); $canvas->page_text(512, 10, "Página: {PAGE_NUM} de {PAGE_COUNT}",$font, 8, array(0,0,0)); $filename = "yourNameConvention".date("Ymd").'.pdf'; $dompdf->stream($filename); ?> 

Test with a simple file, then put all the relevant code with requests, etc. etc.

0
source share

I do not know what content you want to display. I got a bunch of pictures and the maximum picture on the site.

So the function gives me the site number (total / max), and then I got everything I need to start creating pdf.

 @for ($i = 1;$i <= $pages; $i++) @if ($i !== 1) <div style="page-break-before: always;"></div> @endif #Code for each Site @end 

This is how I share my sites, and they also have $ pages as a variable blade.

 <div class="info_footer"> Page <span class="pagenum"></span> / {{$pages}} </div> 

With style sheet:

 .pagenum:before { content: counter(page); } 
0
source share

All Articles