Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get page number on dompdf PDF when using "view"

Tags:

php

pdf

dompdf

Ok, so I use the following snippet to get "views" of HTML with PHP variables loaded in as $data so that I can do things like fill in tr's of data from a database call or whatever.

function getView ($file, $data=NULL) {
    if (!empty($data)) extract($data);
    ob_start();
    if (is_file($file)) include($file);
    return ob_get_clean();
}

Gets used for something like, $htmlPDF = getView('receipt.php', array( 'orderNumber' => $orderNumber )); Where $orderNumber is then used in the HTML to fill in it's proper places. For instance something like:

<h1>You Order #<?= $orderNumber; ?></h1>


Ok, so point is, that's how I get my HTML. I then load it to my dompdf variable as:
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();

Which all works great. The problem is getting the inline php script to work to get PAge Numbers / Page Headers|Footers. I've followed the directions here best I can, but I can't seem to make the right blend. Thus far, not having any errors, except I get 0 Page Numbers anywhere!

And yes I've looked at pages like Header in PDF page using DOMPDF in PHP and dompdf page number, but still no forward progress, at all! I'm wondering if the inline php scripting has to do with how I'm getting the HTML as a string? Any pointers, ideas, advice?

like image 279
SpYk3HH Avatar asked Nov 14 '13 16:11

SpYk3HH


People also ask

How do I show page numbers in Dompdf?

Easy Method: Using CSS and dompdf Add Page numbers like 1 of 5. you can add pdf page numbers using CSS and specify the Header or Footer tag in the content. Please post your code as text, not as an image.

How do I add a footer in Dompdf?

Just like the header, we will put our footer in a footer tag inside the body tag of the template. Just like header, we will create a space for our footer on every page then use part of the space for our footer. For the body content, put everything in a main tag inside the body tag of the template.

How do you insert a page break in Dompdf?

To force a page break before/after your table you would use page-break-before: always; / page-break-after: always; . To ask dompdf to avoid breaking inside an element you would use page-break-inside: avoid; . Also dompdf debugger for quick debugging of your design.


2 Answers

Update Regarding changes with version of dompdf >= 0.7.0
1. Because the dompdf_config.inc.php file has been removed from this release (and is no longer referenced) all dompdf options should be set at run time.
4. The FontMetrics class is now instantiated instead of static. To simplify migration of embedded scripts from earlier versions of dompdf we provide access to the instantiated FontMetrics class via the $fontMetrics variable. Please update your embedded scripts. For example, FontMetrics::get_font('helvetica') would now be $fontMetrics->getFont('helvetica').
~ Thanks to Dennis Ameling's answer for the updated information.

Found my answer by looking over the dompdf_config.inc.php file. As it turns out, DOMPDF_ENABLE_PHP is set to false thus causing the inline php script to be ignored. I simply edited dompdf_config.custom.inc.php to the following and all is fine and working with the later code in the view.

In dompdf/dompdf_config.custom.inc.php

<?php
    define("DOMPDF_ENABLE_PHP", true);

At Run Time

$dompdf->set_option("isPhpEnabled", true);

Then, in my html file

<body>
    <script type="text/php">
        if ( isset($pdf) ) {
            // OLD 
            // $font = Font_Metrics::get_font("helvetica", "bold");
            // $pdf->page_text(72, 18, "{PAGE_NUM} of {PAGE_COUNT}", $font, 6, array(255,0,0));
            // v.0.7.0 and greater
            $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>
    <div

If you go this route, don't forget to restart Apache

like image 54
SpYk3HH Avatar answered Oct 09 '22 13:10

SpYk3HH


I have a simple code to showing page number in footer of every page of dom pdf

#footer { position: fixed; right: 0px; bottom: 10px; text-align: center;border-top: 1px solid black;}
        #footer .page:after { content: counter(page, decimal); }
 @page { margin: 20px 30px 40px 50px; }

above code is css code

<div id="footer">
    <p class="page">Page </p>
  </div> 

You can change text position according to your requirement

like image 28
Sumit Kumar Gupta Avatar answered Oct 09 '22 14:10

Sumit Kumar Gupta