Image not readable or empty

4,663 views
Skip to first unread message

phpMagpie

unread,
Mar 6, 2012, 10:40:17 AM3/6/12
to dom...@googlegroups.com
Hi all,

I have started using dompdf and everything was fine until I tried to insert images. 

I have not changed any config settings as I unpacked the lib into my CakePHP application and it produced text only PDFs straight away.  

I could not get the images working on my local WAMP setup or when uploaded to my LAMP server.  

The HTML I am using shows the images fine, so nothing wrong with the URLs.  I am inserting 3 images, one using a standard <img /> tag and the other two using CSS backgrounds.  All images are using absolute urls starting with a leading / the <img /> tag renders a crossed out block with the text 'Image not readable or empty' to CSS images simply do not show.

Unfortunately CakePHP does not make it easy for me to be able to load the /www/setup.php file (tried to alter .htaccess files to allow this, with no joy).  

Can you give me some pointers on how I can troubleshoot the problem?  

HTML output:

DOMPDF output script:

Version: DOMPDF 0.6.0 beta 3

BrianS

unread,
Mar 6, 2012, 11:02:08 PM3/6/12
to dom...@googlegroups.com
On Tuesday, March 6, 2012 10:40:17 AM UTC-5, phpMagpie wrote:
I have started using dompdf and everything was fine until I tried to insert images. 

I have not changed any config settings as I unpacked the lib into my CakePHP application and it produced text only PDFs straight away.  

I could not get the images working on my local WAMP setup or when uploaded to my LAMP server.  

The HTML I am using shows the images fine, so nothing wrong with the URLs.  I am inserting 3 images, one using a standard <img /> tag and the other two using CSS backgrounds.  All images are using absolute urls starting with a leading / the <img /> tag renders a crossed out block with the text 'Image not readable or empty' to CSS images simply do not show.

Never assume that because the HTML views ok in the browser that it will display the same when rendered by DOMPDF. At least not on first run. Text is rendering ok, so your setup is nominally acceptable. However, a number of other configuration concerns come into play once you start adding in images and external stylesheets. More on that below ...
 
Unfortunately CakePHP does not make it easy for me to be able to load the /www/setup.php file (tried to alter .htaccess files to allow this, with no joy).  

You can copy the files in dompdf/www into your webroot (say to a directory called dompdf). Then modify any reference to "../dompdf_config.inc.php" to point to the actual location of your dompdf configuration. So if you've got dompdf in your app/vendors folder you would change that reference to read "../../vendors/dompdf/dompdf_config.inc.php".
 
Can you give me some pointers on how I can troubleshoot the problem?  

HTML output:

DOMPDF output script:

Version: DOMPDF 0.6.0 beta 3

So your problem due to the combination of using absolute paths and loading the document using $dompdf->load_html(). Since dompdf knows nothing about where the document comes from it assumes that you're working from the local file system. As such, an absolute path will point to the root of the hard drive not the root of the web site. There are a few ways to fix your problem:
  1. Prepend $_SERVER['DOCUMENT_ROOT'] to any file reference.
  2. Set DOMPDF_ENABLE_REMOTE to true and prepend 'http://'.$_SERVER['HTTP_HOST'] to any file reference.
  3. Make any file reference relative to the currently executing PHP document

phpMagpie

unread,
Mar 8, 2012, 4:35:16 AM3/8/12
to dom...@googlegroups.com
Brian,

Thanks for the reply, I got this working a cpl of hours after posting my thread .. but as thread was being moderated could not update it.

I eventually ended up using absolute urls including the domain name and switching DOMPDF_ENABLE_REMOTE to true in custom.inc.php where I also specified a DOMPDF_TEMP_DIR.

This made my inline <img /> tags show up but my background images were not.  I saw in other threads a common theme of try reverting to JPG and as my two background images were GIFs I decided to give this a go and it worked.  I then tried inserting a background PNG(32) and that worked too, including the transparency I needed.

So I now have 3 inline images (2 JPG and 1 GIF) and 2 background images (1 JPG and 1 PNG).

Thanks, Paul.
Reply all
Reply to author
Forward
0 new messages