add image in to pdf

1,461 views
Skip to first unread message

uoon

unread,
Aug 13, 2009, 3:00:19 AM8/13/09
to dompdf
Hi all,

I use dompdf lib to create pdf from php file and when I add an image
by php code (don't use tag img in html file);
I have an error:

Fatal error: Uncaught exception 'PDFlibException' with message 'Handle
parameter or option of type 'image' has bad value 0' in E:\xampp\htdocs
\dompdf\include\pdflib_adapter.cls.php:662 Stack trace: #0 E:\xampp
\htdocs\dompdf\include\pdflib_adapter.cls.php(662): PDFlib->fit_image
(0, 36, 38, 'boxsize={540 20...') #1 E:\xampp\htdocs\dompdf
\fragment.lib.php(40): PDFLib_Adapter->image('logo.png', 'png', 36,
734, 540, 20) #2 {main} thrown in E:\xampp\htdocs\dompdf\include
\pdflib_adapter.cls.php on line 662


This is my code use to add image:

$html = "<html><body></body><html>";
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$pdf = $dompdf->get_canvas();

$footer = $pdf->open_object();

$pdf->image('logo.png', 'png', 36, 734, 540, 20);

$pdf->close_object();
$pdf->add_object($footer, "all");
$dompdf->stream("hello_world.pdf");

Any one help me resolve problem (my version php is 5.3.0).
Thank you so much.

uoon

uoon

unread,
Aug 14, 2009, 12:04:05 AM8/14/09
to dompdf
Hi all,
I have try change relative link 'logo.png' to full address 'http://
vnjs.net/www/dompdf/logo.png'
and when I upload to server the error doesn't show but it still
doesn't work.
Thank.

BrianS

unread,
Aug 14, 2009, 1:53:11 PM8/14/09
to dompdf
The way you're trying to do things won't work. Though that function is
public, calling it returns an instance of the canvas which is
disconnected from DOMDPF. So when you stream the PDF the header won't
be present in your document.

The $pdf methods were really meant to be called internally to the
object. So what you should be doing is something like this:

$html = "<html><body><script type="text/php">
$pdf = $dompdf->get_canvas();
$footer = $pdf->open_object();
$pdf->image('logo.png', 'png', 36, 734, 540, 20);
$pdf->close_object();
$pdf->add_object($footer, "all");
</script></body><html>";
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("hello_world.pdf");

Also, I believe $pdf->image() will load the image relative to the PHP
script being executed. So you can use 'logo.png' but it will need to
be in the same directory. Otherwise you should specify the absolute
path (or full URL) where the image can be found.

Hope that helps.
-b

uoon

unread,
Aug 15, 2009, 12:00:34 AM8/15/09
to dompdf
Hi,
Thank you for reply,
It still don't work with PNG image.
I try your way and JPG image (relative path) it work.
But the image only add in last page, it not add for every page like
the text at footer (I used the $pdf->page_text).
This is the demo: http://vnjs.net/www/dompdf/fragment.lib.php?first=nguyen&last=minh&color=green.
Can you help me to add image for every page like the text at footer.
Thank you.

uoon

uoon

unread,
Aug 15, 2009, 8:22:33 AM8/15/09
to dompdf
Hi,
I just add image for every page.
Note for some one have problem like me :
- Using the inline php code <script type="text/php">....</script>;
- Add that code before all content, it is the first child of body tag;
(Test with jpg first then test with png late);
Many thank Brian S.
uoon

helmuttis

unread,
Aug 15, 2009, 9:07:00 PM8/15/09
to dompdf
> It still don't work with PNG imag

The cpdf component of dompdf and/or the pdf data format itself do not
support some png features.
In particular excluded are
- apha blending
- certain compression
- certain color oding
On some image processing applications I found no way to switch off
conflicting options, and there are many conflicting png images around.

Therefore with dompdf_0-5-2_alpha1, addPngFromFile loads the image
into a gd object, converts it to a gd object without alpha channel and
with addImagePng converts th gd object back to a png file image. It is
not sure that all conflicting options are catched by this.
Reply all
Reply to author
Forward
0 new messages