Page Numbers when HTML comes from another Class

13 views
Skip to first unread message

Sage

unread,
Mar 5, 2010, 5:53:39 PM3/5/10
to dompdf
Hi guys,
Can't seem to get the Page Numbers thing working =(
I've set the "DOMPDF_PDF_BACKEND" to "CPDF" in the dompdf config file,
and followed instructions on:
http://www.digitaljunkies.ca/dompdf/faq.php#footers

I'm thinking the way I output my html may be the issue...
I've got a PHP Class and function where it builds the output HTML and
returns the html back to getpdf.php
So the var $pdf is not getting through into the class function...
correct?

---
class myClass{
function get_PdfHtml(){

if ( isset($pdf) ) {
$font = Font_Metrics::get_font("verdana",
"bold");
$pdf->page_text(72, 18, "Header: {PAGE_NUM} of
{PAGE_COUNT}", $font, 6, array(0,0,0));
}

$sReturnHtml = "lorem ipsum blah blah......."; //
several pages worth of html here
return $sReturnHtml;
}
}

---
---
getpdf.php:

header("Content-type: application/pdf");
header("Content-disposition: newpdf.pdf");
header("Content-disposition: Attachment;
filename=newpdf.pdf");

$myClass = new myClass();

// create pdf
$dompdf = new DOMPDF();
$dompdf->load_html($myClass->get_PdfHtml());
$dompdf->set_paper('letter','landscape');
$dompdf->render();
echo $dompdf->output();
---

Thanks again for your help guys!

BrianS

unread,
Mar 5, 2010, 6:35:30 PM3/5/10
to dompdf

You have a bit of a misunderstanding about how to go about this. When
you do the following:

$dompdf->load_html($myClass->get_PdfHtml());

DOMPDF is not physically loading your class. PHP will first evaulate
the get_PdfHtml() method. The return value from that method is then
passed to DOMPDF. Your class, at no time in this process, knows
anything about the internal DOMPDF objects. What you need to do is
pass the page numbering code as part of your HTML, like so:

class myClass{
function get_PdfHtml(){
$sReturnHtml = "
<script type="text/php">


if ( isset($pdf) ) {
$font = Font_Metrics::get_font("verdana","bold");
$pdf->page_text(72, 18, "Header: {PAGE_NUM} of {PAGE_COUNT}",
$font, 6, array(0,0,0));
}

</script>
lorem ipsum blah blah.......";
return $sReturnHtml;
}
}

Also, be sure to set DOMPDF_ENABLE_PHP to true.

Also, instead of setting the headers yourself, you can let DOMPDF take
care of it by using $dompdf->stream() instead of $dompdf->output();

Hope that helps,
-b

Sage

unread,
Mar 8, 2010, 2:17:06 PM3/8/10
to dompdf
Thanks for your help once again bro.
I figured that was the issue, and had actually tried the way you
advised before, but wasn't working either.
The DOMPDF_ENABLE_PHP is set true.

So turns out the first problem was that the php code inside
<script...> tag wasn't being parsed when tested with a simple echo.
Putting inside <? ?> displayed the echo inside the pdf.

Second problem was that the vars ($pdf, $font, etc.) were being
evaluated in-line rather than being sent as a string.
Escaping them with a backslash like \$pdf seemed to fix that issue.

Then I tried using this code to test if $pdf was set inside dompdf:
$vartest is just making sure that vars are being set.

$sReturnHtml.= "<?\n";
$sReturnHtml.= " \$vartest = true;\n";
$sReturnHtml.= " if (isset(\$pdf)) {\n";
$sReturnHtml.= " echo '--- pdf var IS set ---';\n";
$sReturnHtml.= " \$font =
Font_Metrics::get_font('verdana','bold');\n";
$sReturnHtml.= " \$pdf->page_text(72, 18, 'Header:
{PAGE_NUM} of {PAGE_COUNT}', \$font, 6, array(0,0,0));\n";
$sReturnHtml.= " }\n";
$sReturnHtml.= " elseif(\$vartest) {\n";
$sReturnHtml.= " echo '--- pdf var NOT set ---';\n";
$sReturnHtml.= " }\n";
$sReturnHtml.= "?>\n";

This returned --- pdf var NOT set --- in the generated pdf.
Any ideas?

BrianS

unread,
Mar 9, 2010, 2:14:00 PM3/9/10
to dompdf
Normal PHP code is run prior to any document parsing. At this point
the $pdf object won't be set. Inline script (PHP code between <script>
tags) is run later on and as part of the DOMPDF object. It's during
parsing of inline script that the $pdf object becomes available.

I think DOMPDF is treating the inline script as part of the document
header. Header scripts are currently ignored by DOMPDF. Try returning
a full HTML document instead of just a snippet.

class myClass{
function get_PdfHtml(){
$sReturnHtml = "

<html><body>


<script type="text/php">
if ( isset($pdf) ) {
$font = Font_Metrics::get_font("verdana","bold");
$pdf->page_text(72, 18, "Header: {PAGE_NUM} of {PAGE_COUNT}",
$font, 6, array(0,0,0));
}
</script>
lorem ipsum blah blah.......

</body></html>";
return $sReturnHtml;

Sage

unread,
Mar 11, 2010, 3:14:17 PM3/11/10
to dompdf
Nice. The combination of html, body, and script, tags worked.
I still had to escape the vars though.

Thanks again, Brian. You're the man.

Reply all
Reply to author
Forward
0 new messages