开发者

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

开发者 https://www.devze.com 2023-02-21 07:45 出处:网络
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:

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.


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


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.


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


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


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..


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); }


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


$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.

0

精彩评论

暂无评论...
验证码 换一张
取 消