Errors when creating inline PHP for a header/footer.

138 views
Skip to first unread message

Rob

unread,
Sep 26, 2011, 9:11:29 AM9/26/11
to dompdf
I have a single PHP file, which generates a string full of HTML,
which I pump into DOMPDF using


$dompdf->load_html($htmltext);
$dompdf->render();
$dompdf->stream("my.pdf", array("Attachment"=>0));

I've decided to put a footer and header on this PDF, and just to
experiment, I've copied the section on the WIKI (AS IS) about inline
PHP code into my PHP file.. i.e......

$htmltext .= <<<EOFINLINEPHP
<script type="text/php">

if ( isset($pdf) ) {
// Open the object: all drawing commands will
// go to the object instead of the current page
$footer = $pdf->open_object();

$w = $pdf->get_width();
$h = $pdf->get_height();

// Draw a line along the bottom
$y = $h - 2 * $text_height - 24;
$pdf->line(16, $y, $w - 16, $y, $color, 1);

// Add an initals box
$font = Font_Metrics::get_font("helvetica", "bold");
$text = "Initials:";
$width = Font_Metrics::get_text_width($text, $font, $size);
$pdf->text($w - 16 - $width - 38, $y, $text, $font, $size, $color);
$pdf->rectangle($w - 16 - 36, $y - 2, 36, $text_height + 4,
array(0.5,0.5,0.5), 0.5);

// Add a logo
$img_w = 2 * 72; // 2 inches, in points
$img_h = 1 * 72; // 1 inch, in points -- change these as required
$pdf->image("../images/mylogo.jpg", "jpg", ($w - $img_w) / 2.0, $y -
$img_h, $img_w, $img_h);

// Close the object (stop capture)
$pdf->close_object();

// Add the object to every page. You can
// also specify "odd" or "even"
$pdf->add_object($footer, "all");
}

</script>
EOFINLINEPHP;

I intended to see ti working, then edit it to make it show the
infomation I require.

The problem I have is when run, the PHP generates and error for every
line in the inline code that mentions the class $pdf. i.e.

Notice: Undefined variable: pdf in printquestionnaire.php on line 164

where line 164 is

$footer = $pdf->open_object();

I have set the correct variable in dompdf_config.custom.inc.php I
think....

define("DOMPDF_ENABLE_PHP", true);


BrianS

unread,
Sep 26, 2011, 6:10:30 PM9/26/11
to dom...@googlegroups.com
You need to update your code to use nowdoc instead of heredoc. Because you are using heredoc PHP tries to evaluate any variables in the string. As it says in the manual:

Nowdocs are to single-quoted strings what heredocs are to double-quoted strings.

If you're not using PHP 5.3.x you can still use heredoc, you'll just have to escape the $.


On Monday, September 26, 2011 9:11:29 AM UTC-4, Rob wrote:
I have a single PHP file, which generates a string full of  HTML,
which I pump into DOMPDF using


$dompdf->load_html($htmltext);
$dompdf->render();
$dompdf->stream("my.pdf", array("Attachment"=>0));

I've decided to put a footer and header on this PDF, and just to
experiment, I've copied the section on the WIKI (AS IS) about inline
PHP code into my PHP file..  i.e......

...

Rob

unread,
Sep 27, 2011, 4:33:53 AM9/27/11
to dompdf
On Sep 26, 11:10 pm, BrianS <eclecticg...@gmail.com> wrote:
> You need to update your code to use nowdoc instead of heredoc.

Thanks very much.
Reply all
Reply to author
Forward
0 new messages