DomPDF: Image not readable or empty

88,485

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

Thanks to this: lost in code

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" >
Share:
88,485
Admin
Author by

Admin

Updated on March 12, 2021

Comments

  • Admin
    Admin about 3 years

    For some reason, DomPDF won't render an image included in the html that is being parsed:

    PDF Image missing

    However, the image is rendered on the page when it is returned as html:

    HTML Image exists

    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 ZF2

    Are there any other things that I should be checking for?

  • Admin
    Admin over 9 years
    The 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
    Chad Caldwell almost 8 years
    you rule. I was about to rearrange a lot of stuff!!
  • Marc
    Marc almost 8 years
    After all these try I gave... this is the solution!
  • Keval Shah
    Keval Shah about 7 years
    Same with . None of the solutions worked. At the last tried base64 thanks to this comment and it worked.
  • thirtydot
    thirtydot almost 7 years
    For newer versions the way you specify this setting has apparently changed: stackoverflow.com/a/45362099/405015
  • Loren
    Loren over 6 years
    When 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
    Olotin Temitope over 5 years
    Converting the image to base64 works for me $imageUrl = (string) Image::make(public_path($path)) ->fit(80, 80) ->encode('data-url');
  • Nilesh Pansuriya
    Nilesh Pansuriya almost 4 years
    Adding absolute path worked for me instead relative path.
  • Omer
    Omer about 3 years
    Thanks, Mate. I was trying to get an image from S3 to display in the PDF. Worked like a charm for me 😁
  • ynsmtkl
    ynsmtkl almost 3 years
    Worked for me on Laravel, Thanks
  • vanarie
    vanarie almost 3 years
    This worked for me with remote images. Thank you for sharing!