Add image

1,595 views
Skip to first unread message

pigfoxeria

unread,
Jan 28, 2010, 1:40:16 PM1/28/10
to dompdf
Does anyone have complete working code of dompdf-0.5.1 adding an png
to the pdf? Running PHP Version 5.2.6.

Paul Waring

unread,
Jan 28, 2010, 3:23:59 PM1/28/10
to dom...@googlegroups.com
pigfoxeria wrote:
> Does anyone have complete working code of dompdf-0.5.1 adding an png
> to the pdf? Running PHP Version 5.2.6.

How are you linking to the PNG file? I have successfully created PDFs
with images in, but you might need to use the full path to the image, e.g.

<img src="/path/to/image.png" width="100" height="100" />

and not:

<img src="image.png" width="100" height="100" />

The same applies to other external files, such as stylesheets.

The PHP code shouldn't be any different than what you use to generate
any other PDF file using dompdf - the snippet I use is:

$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->set_paper('a4', 'landscape');
$dompdf->render();
file_put_contents($filename, $dompdf->output());

Where $html contains the HTML (e.g. extracted from a file) and $filename
is where you want to save the PDF.

Paul

--
Paul Waring
http://www.pwaring.com

pigfoxeria

unread,
Jan 28, 2010, 3:44:51 PM1/28/10
to dompdf
This is my code:
$html="<img src="http://www.pigfox.com/imgs/test.png"/><br/>";
$html.="Some string";
$html.= '<br /><br />';

$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
dompdf->stream("dompdf_out.pdf");
exit(0);

Commenting out the img line works but including it I get a 500 error.

Paul Waring

unread,
Jan 28, 2010, 3:46:20 PM1/28/10
to dom...@googlegroups.com
pigfoxeria wrote:
> This is my code:
> $html="<img src="http://www.pigfox.com/imgs/test.png"/><br/>";
> $html.="Some string";
> $html.= '<br /><br />';
> $dompdf = new DOMPDF();
> $dompdf->load_html($html);
> $dompdf->render();
> dompdf->stream("dompdf_out.pdf");
> exit(0);
>
> Commenting out the img line works but including it I get a 500 error.

Have you tried another image, preferably using a local filename? That
would help narrow the problem down.

pigfoxeria

unread,
Jan 28, 2010, 3:56:48 PM1/28/10
to dompdf
Hello Paul,
I'm using url rewriting in that folder where the script lives so
that's the reason I'm using absolute urls.
I also tried <img src="/var/www/vhosts/pigfox.com/httpdocs/imgs/
test.png"/> which also gave me a 500 error.

pigfoxeria

unread,
Jan 29, 2010, 5:37:30 PM1/29/10
to dompdf
I tried a local image:
$s="<img src="ucla.png"/>";
$html.= $s.'<br /><br />';

$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$filename='test.pdf';
$dompdf->stream($filename);
exit(0);
Still generates a 500 error. The image renders well in any browser.
Do you know if url rewriting has anything to do with this problem?

Sam Doyle

unread,
Feb 1, 2010, 7:08:06 AM2/1/10
to dompdf
You have a PHP error.

$s="<img src="ucla.png"/>";

should be

$s = "<img src=\"ucla.png\" />";

or

$s = "<img src='ucla.png' />";

Paul Waring

unread,
Feb 1, 2010, 9:43:57 AM2/1/10
to dom...@googlegroups.com
Or even:

$s = '<img src="ucla.png" />';

Probably the best option is to use single quotes as then you don't have
to worry about escaping a lot of things.

pigfoxeria

unread,
Feb 1, 2010, 3:43:36 PM2/1/10
to dompdf
Got it to run, php and img in the same directory.
Apparently url rewriting caused the problem.
Reply all
Reply to author
Forward
0 new messages