DomPDF: Image not readable or empty
Solution 1
Following helped me like charm, at least localy, and even with
def("DOMPDF_ENABLE_REMOTE", false);
The solution is to change the image SRC to the absolute path on the server, like this:
<img src="/var/www/domain/images/myimage.jpg" />
All of the following worked for me:
<img src="<?php echo $_SERVER["DOCUMENT_ROOT"].'/placeholder.jpg';?>"/>
<img src="<?php echo $_SERVER["DOCUMENT_ROOT"].'\placeholder.jpg';?>"/>
<img src="<?php echo $_SERVER["DOCUMENT_ROOT"].'./placeholder.jpg';?>"/>
$_SERVER["DOCUMENT_ROOT"] is C:/wamp/www/ZendSkeletonApplication/public
Solution 2
As there was another answer that suggests enabling the remote option in module.config.php
and I can't yet add comments, I thought it would be best to answer that this file does not exist in newer versions of DomPDF.
If you need to include remotely stored images in a newer version you have to pass it as an option to the constructor:
$dompdf = new Dompdf(array('enable_remote' => true));
This fixed the issue I had.
Solution 3
Ok I had the same problem with image using :
<img id="logo" src="/images/flags/fr.png" width="50" alt="Logo">
But if I add a . before /images, without changing anything in dompdf_config.custom.inc, it works
<img id="logo" src="./images/flags/fr.png" width="50" alt="Logo">
Hope it helps
Solution 4
Now (May 2018) the correct way is :
$options = new Options();
$options->set('isRemoteEnabled',true);
$dompdf = new Dompdf( $options );
Solution 5
You can use base64 encoded image
<img src="{{'data:image/png;base64,' . base64_encode(file_get_contents(@$image))}}" alt="image" >
Admin
Updated on March 12, 2021Comments
-
Admin about 3 years
For some reason, DomPDF won't render an image included in the html that is being parsed:
However, the image is rendered on the page when it is returned as html:
I've looked at these issues and have make sure that DOMPDF_ENABLE_REMOTE is set to true and verified file permissions:
dompdf image not real image not readable or empty
Image error in DOMPDF for ZF2Are there any other things that I should be checking for?
-
Admin over 9 yearsThe reason that worked is because originally you are looking for the images directory in the root. The "./" just means "look in this directory". You could actually remove the "./" and it will still work.
-
Chad Caldwell almost 8 yearsyou rule. I was about to rearrange a lot of stuff!!
-
Marc almost 8 yearsAfter all these try I gave... this is the solution!
-
Keval Shah about 7 yearsSame with . None of the solutions worked. At the last tried base64 thanks to this comment and it worked.
-
thirtydot almost 7 yearsFor newer versions the way you specify this setting has apparently changed: stackoverflow.com/a/45362099/405015
-
Loren over 6 yearsWhen providing sample code, please provide it as an example to fix the problem for the asker. (IE Sample code with a full path to an image rather than a stylesheet since this question is about an image.)
-
Olotin Temitope over 5 yearsConverting the image to base64 works for me
$imageUrl = (string) Image::make(public_path($path)) ->fit(80, 80) ->encode('data-url');
-
Nilesh Pansuriya almost 4 yearsAdding absolute path worked for me instead relative path.
-
Omer about 3 yearsThanks, Mate. I was trying to get an image from S3 to display in the PDF. Worked like a charm for me 😁
-
ynsmtkl almost 3 yearsWorked for me on Laravel, Thanks
-
vanarie almost 3 yearsThis worked for me with remote images. Thank you for sharing!