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?
If you're using DOMPDF >= 0.7.0, the dompdf_config.inc.php file has been removed and adding a page number now requires a slightly different approach:
To enable PHP code to be executed by DOMPDF, use:
Also, FontMetrics should now be called by using
$fontMetrics
instead ofFont_Metrics
, so the code mentioned by @user1231342435346354 changes slightly:Found my answer by looking over the
dompdf_config.inc.php
file. As it turns out,DOMPDF_ENABLE_PHP
is set tofalse
thus causing the inline php script to be ignored. I simply editeddompdf_config.custom.inc.php
to the following and all is fine and working with the later code in theview
.In dompdf/dompdf_config.custom.inc.php
At Run Time
Then, in my html file
If you go this route, don't forget to restart
Apache
First you must enable executing inline php scripts in
dompdf_config.inc.php
file. (changedefine("DOMPDF_ENABLE_PHP", true);
this line to true)This code sets Page indicator in right corner of the header...
Be sure that script tag is within body tag... otherwise it doesn't work!