Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DomPDF: Image not readable or empty

People also ask

How do I get an image in Dompdf?

$dompdf->getOptions()->setChroot("C:\\laragon\\www\\your-local-website\\public"); and then you can insert any <img> with src from within (can be nested) that /public folder into HTML.

Can we use bootstrap in Dompdf?

dompdf's CSS support is still a little lacking for something as complex as Bootstrap. You can get a basic design working, but more complicated designs are not likely to produce the desired results. If you provide a sample of the HTML we can try to tweak the CSS to get better results.

What is Dompdf library?

Dompdf is an HTML to PDF converter. At its heart, dompdf is (mostly) a CSS 2.1 compliant HTML layout and rendering engine written in PHP. It is a style-driven renderer: it will download and read external stylesheets, inline style tags, and the style attributes of individual HTML elements.


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


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.


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


Now (May 2018) the correct way is :

$options = new Options();
$options->set('isRemoteEnabled',true);      
$dompdf = new Dompdf( $options );

You can use base64 encoded image

<img src="{{'data:image/png;base64,' . base64_encode(file_get_contents(@$image))}}" alt="image" >

I solve this problem by using external CSS's full path. This one worked on my linux ubuntu server :

<link href="{{ public_path('css/style.css') }}" />

<img src="{{ public_path('images/image.jpg') }}" />

and work on image.