19
votes

On my server pdf generated via dompdf was not displaying images. Because they are remote url images.(local images are working fine) then I come to know that it needs some settings to render remote images.

allow_url_fopen = true => i can not as server control is not in my hand.(and no one will suggest to do this due to security reasons)
read/write access to the DOMPDF_TEMP_DIR (already have this)
DOMPDF_ENABLE_REMOTE = true (already have this)

So to sure the issue of allow_url_fopen, I set false in my localhost which is now giving the same output as server.

So, now the issue is now I want to display remote images in PDF with allow_url_fopen = false

  • I tried almost 5-10 unique ways to do this.
  • I tried to display image in php file (via setting headers) and then displaying the php link in pdf
  • I tried to display image via absolute path to php too but nothing worked.
  • I tried via getting image via curl in a function and then displaying it in a php file ...but no luck.

Is there anyone can suggest me how can I display image in pdf please. The error I am always getting is ...

Image not found
http://localhost/dompdf/image.php

and

Image not found
http://localhost/dompdf/image.jpg
18
did you find any solution? i am having the same problem, images are appearing fine on localhost, but not on live server, path is fine, image is already on the server. - Irfan Ahmed
no. still not found it. - TechCare99

18 Answers

42
votes

I had the same problem, dompdf image not found on live server

I found its solution, you just need to double check the image path,

Considering your live server image path

<img src="http://www.example.com/public/images/thumb.png">

You just need to change it to,

<img src="public/images/thumb.png">

Note: Make sure that, all settings are same as you have already made.

I hope this will help you.

43
votes

Try

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

There are two things to take care of.

  1. If using image from same server then use full directory path e.g. /var/www/html/project_folder/images/logo.jpg

  2. List item Use JPEG image instead of png or other types.

2
votes

I had the same issue after doing a lot of R&D finally got the solutions.First you need to get the options of dompdf then set isRemoteEnabled with true.

    $options = $dompdf->getOptions(); 
    $options->set(array('isRemoteEnabled' => true));
    $dompdf->setOptions($options);
2
votes

I had the same issue with images, and my solution was:

getcwd().'path/to/image'

It works for me!

1
votes

Can you reach those URLs in your web browser on the machine that you're using to open the PDF? If not, the PDF reader won't be able to either.

I suspect that the "localhost" domain means that those URLs are only visible from the web server that generated the PDF. You need to output a url like http://example.com/dompdf/image.jpg

(To step around this issue, bear in mind that there are good reasons not to use remote images. The document will look bad if the viewer is not connected to the internet, for example. Is it possible to just embed the images directly in the document?)

1
votes

I think you could add this

private function change_url_image($data, $url) {    
    $str = $url; //for example "http://localhost/yoursite/";
    $str2 = str_replace($str, "", $data);
    return $str2;
}

to change url for image

1
votes

I am giving you an example with function

  $html = $this->output->get_output();
        $this->load->library('pdf');
        $this->dompdf->loadHtml($html);
        $this->dompdf->set_option('isRemoteEnabled', true);
        $this->dompdf->setPaper('A4', 'portrait');
        $this->dompdf->render();
        $this->dompdf->stream("studentidcard.pdf", array("Attachment"=>0));

Set isremoteenabled true to show remote images

1
votes

Hi my solution to this problem was adding the enabling the allow_url_fopen in my php.ini file

1
votes

Go to library/dompdf/scr/option

change private $isRemoteEnabled = false; to private $isRemoteEnabled = true;

1
votes

This is because, DOMPDF Can't access the image on the server, please check if you have any protection added to access your webpage.

0
votes

dompdf doesn't currently have a mechanism for distinguishing between a local and remote domain, so any URL that starts http://... is treated as remote. Plus, any image that uses a PHP-based intermediary (like pic.php) can't use a local path because the PHP will not be parsed unless you go through a web server.

It's a difficult prospect, but your pages are generated dynamically. So I see two options:

  1. downloading remote images and linking to them on the local file system
  2. downloading remote images and using a data URI.

Since you've already worked out getting the image using curl you should be able to implement either one of these.

0
votes

Same problem i was facing while i was also set the "DOMPDF_ENABLE_REMOTE=>true" in "dompdf/dompdf_config.inc" but not worked.

One thing worked for me change the src for images/css from absolute to relative and things done.in this case i have all the css/images on my server.

0
votes

Just in case anyone has the same issues as me:

  • I changed src="https://" to src="http://" and the images loaded fine.

  • I also added all CSS inline within a <style> instead of loading via <link>

  • and make sure the CSS <style> is in the <head> not the <body>

0
votes

I used this line of code

<img src="{{ url('/frontened/images/jeevan-green-logo.png') }}" >

It's working fine. Thanks.

0
votes
$data = {{any html content with src & href links}}

Sometimes it may happen based on the unsecure url like https with invalid ssl certificate. so the renderer of dompdf cannot be accessed the url for fetching the content. check your content has any unsecure url. If you're using that url with process as unsafe option in browser, please use the following method. It replace the url to abspath

$url = "https://example.com";
$abspath="/home/public_html/example" || getcwd();
$data =  preg_replace('#(src=\"[\s\r\n]{0,})('.$url.')#','$1'.$abspath, $data);
$data =  preg_replace('#(href=\"[\s\r\n]{0,})('.$url.')#','$1'.$abspath, $data);
0
votes

Try use full directory path with .jpg image

I realized that jpg images appear when you host your files remotely.

-3
votes

Go to the dompdf_config.inc.php file and set the variable DOMPDF_ENABLE_REMOTE to TRUE ...