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条回答
beautiful°
2楼-- · 2020-07-09 03:49

In php, you can access dompdf variables:

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

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

查看更多
一夜七次
3楼-- · 2020-07-09 03:54

For people coming here using a new version of DomPdf

Since dompdf 0.7.0, the dompdf_config.inc.php file has been removed (and is no longer referenced): all dompdf options should be set at run time.

This means you have to create a new instance of Options class

$domPdfOptions = new Options();

And then you may enable PHP inline using the following line

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

The rest of the codes are correct and will show a 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 pointed out by @london-smith, this also works for DomPDF 0.8.1

查看更多
登录 后发表回答