Asked  7 Months ago    Answers:  5   Viewed   117 times

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

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?



Update Regarding changes with version of dompdf >= 0.7.0
1. Because the 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 file. As it turns out, DOMPDF_ENABLE_PHP is set to false thus causing the inline php script to be ignored. I simply edited to the following and all is fine and working with the later code in the view.

In dompdf/

    define("DOMPDF_ENABLE_PHP", true);

At Run Time

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

Then, in my html file

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

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

Wednesday, March 31, 2021
answered 7 Months ago

I've tested your code and it works fine for me - sample.pdf file is being downloaded in browser. I've downloaded library from url (not only the encoding branch(

Probably you haven't moved the whole project to selected directory or you haven't downloaded the whole library. I moved the whole downloaded directory content to APIs/dompdf-encoding directory and I have here files and directories lib, include and www.


As you edited you want to use only encoding branch, what you have to do is adding the following code at the beginning of your file:

use DompdfAdapterCPDF;
use DompdfDompdf;
use DompdfException;


The whole working code:

use DompdfAdapterCPDF;
use DompdfDompdf;
use DompdfException;

$cart_body='<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>New Order Placed</title></head><body><p>Test Printing...</p></body></html>';
        $dompdf = new Dompdf();
        $dompdf->load_html($cart_body);//body -> html content which needs to be converted as pdf..
        $dompdf->stream("sample.pdf"); //To popup pdf as download

I have also changed DOMPDF to Dompdf just in case (in Windows both are working)

Wednesday, March 31, 2021
answered 7 Months ago

I had the exact same problem, no images were displaying for me - background images and inline ones. I had to set DOMPDF_ENABLE_REMOTE to true in your config

Just note the security risk if you have this enabled along with DOMPDF_ENABLE_PHP

Saturday, May 29, 2021
answered 5 Months ago

This might be a solution:

  1. convert postscript to pdf using ps2pdf
  2. create a LaTeX file and insert the pages using the pdfpages package (includepdf)
  3. use pagecommand={thispagestyle{plain}} or something from the fancyhdr package in the arguments of includepdf
  4. if postscript output is required, convert the pdflatex output back to postscript via pdf2ps
Tuesday, July 27, 2021
answered 3 Months ago

What do you mean by "difference"? A difference in the text of the PDF or some layout change (e.g. an embedded graphic was resized). The first is easy to detect, the second is almost impossible to get (PDF is an VERY complicated file format, that offers endless file formatting capabilities).

If you want to get the text diff, just run a pdf to text utility on the two PDFs and then use Python's built-in diff library to get the difference of the converted texts.

This question deals with pdf to text conversion in python: Python module for converting PDF to text.

The reliability of this method depends on the PDF Generators you are using. If you use e.g. Adobe Acrobat and some Ghostscript-based PDF-Creator to make two PDFs from the SAME word document, you might still get a diff although the source document was identical.

This is because there are dozens of ways to encode the information of the source document to a PDF and each converter uses a different approach. Often the pdf to text converter can't figure out the correct text flow, especially with complex layouts or tables.

Thursday, September 16, 2021
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :