Image in Header

1,346 views
Skip to first unread message

Dennis Meyer

unread,
Oct 13, 2009, 5:22:21 AM10/13/09
to dompdf
Hey Dudes,
at first youre class rocks! I really like it, i tried to code it by
myself and i failed ;-) You did it great.
Now the question.
I just want to add a image into my header, i read the faq and tried
the example, i was a little bit confused about the missing } at the
end, whatev.
This is my HTML template:

<html>
<body>
<p>Hello World</p>
<script type="text/php">
if ( isset($pdf) ) {

$header = $pdf->open_object();
$pdf->image("pix/bb_iqment02.JPG", "jpg", 1, 10, 570, 130);
$pdf->close_object();
$pdf->add_object($header, "all");
}
</script>
</body>
</html>

And this is the call:

require_once("dompdf/dompdf_config.inc.php");
$html = file_get_contents('modules/offer/pdftemplate.html');

$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->set_paper('a4', 'portrait'); // letter, landscape
$dompdf->render();
$dompdf->stream("sample.pdf");

And this is my error ;-)

Fatal error: Uncaught exception 'PDFlibException' with message
'Function must not be called in 'template' scope' in /is/htdocs/
wp1046861_Z1H6V86WEE/pm/modules/offer/dompdf/include/
pdflib_adapter.cls.php:658 Stack trace: #0 /is/htdocs/
wp1046861_Z1H6V86WEE/pm/modules/offer/dompdf/include/
pdflib_adapter.cls.php(658): PDFlib->load_image('jpeg', 'pix/
bb_iqment02...', '') #1 /is/htdocs/wp1046861_Z1H6V86WEE/pm/modules/
offer/dompdf/include/php_evaluator.cls.php(66) : eval()'d code(6):
PDFLib_Adapter->image('pix/bb_iqment02...', 'jpg', 1, 10, 570, 130)
#2 /is/htdocs/wp1046861_Z1H6V86WEE/pm/modules/offer/dompdf/include/
php_evaluator.cls.php(66): eval() #3 /is/htdocs/wp1046861_Z1H6V86WEE/
pm/modules/offer/dompdf/include/php_evaluator.cls.php(70):
PHP_Evaluator->evaluate('???if ( isset($...') #4 /is/htdocs/
wp1046861_Z1H6V86WEE/pm/modules/offer/dompdf/include/renderer.cls.php
(180): PHP_Evaluator->render(Object(Null_Frame_Decorator)) #5 /is/
htdocs/wp1046861_Z1H6V86WEE/pm/modules/offer/dompdf/include/
renderer.cls.php(120): Renderer->_ren in /is/htdocs/
wp1046861_Z1H6V86WEE/pm/modules/offer/dompdf/include/
pdflib_adapter.cls.php on line 658


Hope you can help me.
DM

BrianS

unread,
Oct 29, 2009, 2:57:05 PM10/29/09
to dompdf
On Oct 13, 5:22 am, Dennis Meyer <snooop...@googlemail.com> wrote:
> I just want to add a image into my header, i read the faq and tried
> the example, i was a little bit confused about the missing } at the
> end, whatev.
> This is my HTML template:
>
> <html>
> <body>
>         <p>Hello World</p>
>         <script type="text/php">
>                 if ( isset($pdf) ) {
>
>                         $header = $pdf->open_object();
>                         $pdf->image("pix/bb_iqment02.JPG", "jpg", 1, 10, 570, 130);
>                         $pdf->close_object();
>                         $pdf->add_object($header, "all");
>                 }
>         </script>
> </body>
> </html>

Your HTML looks fine, but you'll probably need to use a full local
filesystem path in the $pdf->image() function. I believe, though I
would need to double-check, that this image looks relative to the
executing PHP file. However, this is not the source of your error.

> And this is the call:
>
> require_once("dompdf/dompdf_config.inc.php");
> $html = file_get_contents('modules/offer/pdftemplate.html');
>
> $dompdf = new DOMPDF();
> $dompdf->load_html($html);
> $dompdf->set_paper('a4', 'portrait'); // letter, landscape
> $dompdf->render();
> $dompdf->stream("sample.pdf");

Looks fine.

> And this is my error ;-)
>
> Fatal error: Uncaught exception 'PDFlibException' with message
> 'Function must not be called in 'template' scope' in /is/htdocs/
> wp1046861_Z1H6V86WEE/pm/modules/offer/dompdf/include/
> pdflib_adapter.cls.php:658 Stack trace: #0 /is/htdocs/
> wp1046861_Z1H6V86WEE/pm/modules/offer/dompdf/include/
> pdflib_adapter.cls.php(658): PDFlib->load_image('jpeg', 'pix/
> bb_iqment02...', '') #1 /is/htdocs/wp1046861_Z1H6V86WEE/pm/modules/
> offer/dompdf/include/php_evaluator.cls.php(66) : eval()'d code(6):
> PDFLib_Adapter->image('pix/bb_iqment02...', 'jpg', 1, 10, 570, 130)
> #2 /is/htdocs/wp1046861_Z1H6V86WEE/pm/modules/offer/dompdf/include/
> php_evaluator.cls.php(66): eval() #3 /is/htdocs/wp1046861_Z1H6V86WEE/
> pm/modules/offer/dompdf/include/php_evaluator.cls.php(70):
> PHP_Evaluator->evaluate('???if ( isset($...') #4 /is/htdocs/
> wp1046861_Z1H6V86WEE/pm/modules/offer/dompdf/include/renderer.cls.php
> (180): PHP_Evaluator->render(Object(Null_Frame_Decorator)) #5 /is/
> htdocs/wp1046861_Z1H6V86WEE/pm/modules/offer/dompdf/include/
> renderer.cls.php(120): Renderer->_ren in /is/htdocs/
> wp1046861_Z1H6V86WEE/pm/modules/offer/dompdf/include/
> pdflib_adapter.cls.php on line 658

What version of PHP, DOMPDF, and PDFLib are you using? I have not been
able to reproduce this error as yet. I'll try and help you figure out
the source of the error, but in the meantime you can work around the
problem by using the built-in CPDF adapter. Set DOMPDF_PDF_BACKEND to
"CPDF" to do so.
-b

BrianS

unread,
Oct 29, 2009, 4:13:19 PM10/29/09
to dompdf
I have been able to reproduce the error you are seeing. Apparently
there is a requirement that an image be loaded outside the scope of an
object. Unfortunately, the way DOMPDF handles things right now is not
quite correct with relation to PDFLib. We'll try to address this issue
for the next release.

To work around the problem you have to add the image before creating
the object, then add the image again. The first time you add the image
do it so that it doesn't actually display anything in the document.
Something like the following should work:

<html>
<body>
<p>Hello World</p>
<script type="text/php">
if ( isset($pdf) ) {
$pdf->image("pix/bb_iqment02.JPG", "jpg", 0,
0, 0, 0);
$header = $pdf->open_object();
$pdf->image("pix/bb_iqment02.JPG", "jpg", 1,
10, 570, 130);
$pdf->close_object();
$pdf->add_object($header, "all");
}
</script>
</body>
</html>

Not ideal, but it's the best work-around I can offer for now.
-b
Reply all
Reply to author
Forward
0 new messages