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!
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
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?
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;
Thanks again, Brian. You're the man.