How can I get the total number of pages in DOMPDF?

2020-07-09 03:01发布

For example Page 1 of 5.

There's an example online of how to get teh Page 1 part but not the of 5 part. This is it:

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

I'm using version 0.6 and $PAGE_NUM and $PAGE_COUNT does not work.

标签: php dompdf
8条回答
叼着烟拽天下
2楼-- · 2020-07-09 03:28

See the attachment named issue121.php on this bug report. Worked for me. As I understand it you can't echo the page num but you can draw it somewhere.

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

查看更多
放我归山
3楼-- · 2020-07-09 03:28

I dont know which kind of content you want to render. I got a bunch of pictures and a maximal Pictures per Site constant.

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

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

Thats how I seperate my Sites and their I also got $pages as a blade variable.

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

With Stylesheet:

.pagenum:before { content: counter(page); }
查看更多
淡お忘
4楼-- · 2020-07-09 03:38

$PAGE_COUNT is the 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, upgrade. Otherwise, you may be out of luck.

查看更多
走好不送
5楼-- · 2020-07-09 03:42

Check you have enabled inline_pdf in dompdf.

Use this code, you can put where you like, it gets the height and width of the document and puts the page/total_pages at the bottom right.

<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

查看更多
戒情不戒烟
6楼-- · 2020-07-09 03:48

Hi this is mi full code... after all the HTML put 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("Y-m-d").'.pdf';
$dompdf->stream($filename);
?>

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

查看更多
Root(大扎)
7楼-- · 2020-07-09 03:49

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

For now, total page count is not supported with the CSS you are using, we are planning to make it work in 0.6 final though.

查看更多
登录 后发表回答