Add footer text with variable outside the <script type="text/php">

516 views
Skip to first unread message

Timo002

unread,
Oct 20, 2009, 11:23:11 AM10/20/09
to dompdf
Hello,

Maybe my title is a bit strange so I will try to tell my problem here.

I'm adding a footer to the PDF page with the following code:
[CODE]
<?php
ob_start();

$this_is_my_footer_tekst = "Hello this is my footer";
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://
www.w3.org/TR/html4/loose.dtd">
<html>
<head>
</head>
<body>
<script type="text/php">
if ( isset($pdf) ) {

$font = Font_Metrics::get_font("verdana");;
$size = 10;
$color = array(0,0,0);
$text_height = Font_Metrics::get_font_height($font, $size);

// 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
$text = $this_is_my_footer_tekst;
$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);

// 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>
<!-- here is my html page that will be converter to pdf -->
</body>
</html>
<?php
$html = ob_get_contents();
ob_clean();

$pdfnaam = "Factuur.pdf";
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream($pdfnaam);
?>

[/CODE]

The problem is, is that my footer text is made outside the <script
type="text/php"></script> tags. By this the variable
$this_is_my_footer_tekst is not accessible in the script.

Does anyone know how to solve this?

Kind regards,

BrianS

unread,
Nov 3, 2009, 2:40:49 PM11/3/09
to dompdf
I can see two possible ways to solve this problem.

1) Regular PHP tags are executed before the document is rendered
(including inline PHP). So generate the header variable dynamically
inside your inline PHP like this:

<script type="text/php">
if ( isset($pdf) ) {

<?php
$this_is_my_footer_tekst = "Hello this is my footer";
echo '$this_is_my_footer_tekst = "' , $this_is_my_footer_tekst ,
'";'
?>

...
</script>

2) Make the variable $this_is_my_footer_tekst global so you can access
it via the inline script:

<?php
$GLOBALS['this_is_my_footer_tekst'] = "Hello this is my footer";
?>
...
<script type="text/php">
...
$text = $GLOBALS['this_is_my_footer_tekst'];
...
</script>
Reply all
Reply to author
Forward
0 new messages