27

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
TechCare99
  • 1,763
  • 6
  • 24
  • 33

26 Answers26

58

Try

use Dompdf\Options;
$options = new Options();
$options->set('isRemoteEnabled', true);
$dompdf = new Dompdf($options);
Community
  • 1
  • 1
theTypan
  • 5,471
  • 6
  • 24
  • 29
49

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.

Irfan Ahmed
  • 9,136
  • 8
  • 33
  • 54
15

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.

Rohit Suthar
  • 3,528
  • 1
  • 42
  • 48
daudichya
  • 381
  • 3
  • 5
4

I had the same issue after doing a lot of R&D finally got the solution. 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);
Soubhagya Kumar Barik
  • 1,979
  • 20
  • 26
4

Go to dompdf/vendor/dompdf/dompdf/src/ and open Options.php file.

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

4

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

getcwd().'path/to/image'

It works for me!

Bùi Đức Khánh
  • 3,975
  • 6
  • 27
  • 43
4

For anyone having problem loading images. The above solutions mention using absolute path. But that won't work unless you set chroot option. Any file outside chroot directory won't be loaded. Example setting chroot to read from wordpress' wp content dir:

$options = new Options();    
$options->set( 'chroot', WP_CONTENT_DIR );
$dompdf = new Dompdf( $options );
wyktor
  • 49
  • 4
2

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?)

Stephen Schwink
  • 512
  • 4
  • 7
  • Here user is viewing my site and generating dynamically pdf and for example i am displaying his profile pic in pdf. (profilepic is on cdn server) So may be i can give link like cdnserver.com/user/12.jpg or another way is myserver.com/user_profiles/pic.php?id=12 both the way are not working and user has internet access – TechCare99 Mar 01 '13 at 09:20
2

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

Jaak Kütt
  • 2,566
  • 4
  • 31
  • 39
2

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

Sumit Kumar Gupta
  • 2,132
  • 1
  • 22
  • 21
2

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

Lego
  • 304
  • 2
  • 14
2

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

2

DOMPDF version 1.1.1

I tried all the options above and the only way it worked for me was:

For the Absolute Paths (example: https://website/image.jpg) I had to:

#1 - Use these options:

$options = new Options();
$options->set('isRemoteEnabled', TRUE);
$options->set('tempDir', '/tmp'); //folder name and location can be changed
$dompdf = new Dompdf($options);

#2 - create this tmp folder at same level of the php code where lines above are added.

For the Relative Paths (example: /contents/test_image.jpg) I had to:

#1 - Use these options:

$options = new Options();
$options->set('tempDir', '/tmp');
$options->set('chroot', __DIR__);
$dompdf = new Dompdf($options);

#2 - create this tmp folder at same level of the php code where lines above are added (like the absolute path)

At the end, in order to work with absolute and relative paths my code was like this:

<?php

use Dompdf\Dompdf;
use Dompdf\Options;

require __DIR__ . "/vendor/autoload.php";

$options = new Options();
$options->set('isRemoteEnabled', TRUE);
$options->set('tempDir', '/tmp');
$options->set('chroot', __DIR__);

$dompdf = new Dompdf($options);

ob_start();
require __DIR__ . "/contents/my_html.php";
$dompdf->loadHtml(ob_get_clean());


$dompdf->setPaper("A4", "portrait");
$dompdf->render();
$dompdf->stream("file.pdf", ['Attachment' => false]);

and my project structure is like this:

pdftest/
  |__contents/
  |  |__my_html.php
  |  |__jpg_test.jpg
  |__tmp/
  |__vendor/* //composer stuff
  |__index.php

and my html looks index.php this

<html>
<header>
    <style>
        body { display:flex; flex-direction:column; align-items: center; justify-content: center; }
        table { border: 1px solid #a1a1a1; }
        table th { border-bottom: 1px solid #a1a1a1; }
    </style>
</header>
<body>
    <table>
        <tr>
            <th>column 1</th>
            <th>column 2</th>
            <th>column 3</th>
            <th>column 4</th>
        </tr>
        <tr>
            <td>Data 1</td>
            <td>
                <img width="40" src="contents/jpg_test.jpg" alt=""/>
                <img width="40" src="https://cdn.pixabay.com/photo/2021/10/14/15/11/cathedral-6709412_960_720.jpg" alt=""/>
            </td>
            <td>Data 3</td>
            <td>Data 4</td>
        </tr>
    </table>
</body>
</html>
2

Try this solution to solve your issue. image url is sample. i am using Laravel "barryvdh/laravel-dompdf": "^2.0", package for generate PDF.

 <img src="data:image/png;base64,{{ base64_encode(file_get_contents( "https://example.com/image/domo.png" )) }}"> 

enter image description here

pankaj
  • 1
  • 17
  • 36
1

I use symfony and two steps solved my problem:

  1. enable option isRemoteEnabled on domPdf options
  2. put absolute url on twig

Here my answer more detailed : my answer more detailed on stackoverflow

I hope it helps

yavuz16dev
  • 119
  • 6
0

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.

BrianS
  • 13,284
  • 15
  • 62
  • 125
0

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

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>

vendo
  • 13
  • 1
  • 4
0

I used this line of code

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

It's working fine. Thanks.

Y. Joy Ch. Singha
  • 3,056
  • 24
  • 26
0
$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);
Karthikeyan Ganesan
  • 1,901
  • 20
  • 23
0

Try use full directory path with .jpg image

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

user3315848
  • 81
  • 1
  • 3
  • 13
0

$pdf=\PDF::loadView('page url',compact('data')); Laravel Dome PDF

    $pdf->getDomPDF()->setHttpContext(
        stream_context_create([
            'ssl' => [
                'allow_self_signed'=> TRUE,
                'verify_peer' => FALSE,
                'verify_peer_name' => FALSE,
            ]
        ])
    );
Romit
  • 1
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 26 '21 at 09:41
0

actually problem is on remote server dompdf not showing .png but .jpg works, i think simple solution is ,

$extension = substr('1.png', - 4);
    if($extension == '.png' || $extension == '.PNG')
    {
        //convert image png to jpg, because dompdf png not working on Remote Server.
        $image = imagecreatefrompng('1.png');
        imagejpeg($image, '1.png', 100);
        $profileimage = '1.jpg';
        
    }

after pdf generated you can unlink above temporary jpg file.

MAYUR SANCHETI
  • 420
  • 3
  • 10
0

I faced the same image issue & I did R&D 3 days about this finally I got a proper solution to this dompdf issue

first of all, you need to enable the remote flag in your dompdf library using the below example

$dompdf = new Dompdf();
$dompdf->set_options(['isHtml5ParserEnabled' => true, 'isRemoteEnabled' => true]);
$html = file_get_contents($html_file_path);
$dompdf->load_html($html);
$dompdf->set_paper($paper, $orientation);
$dompdf->render();
$dompdf->stream($filename . '.pdf', array('Attachment' => 1));

& make sure you have installed some php extensions as mentioned below dompdf require

> extension=mbstring
> extension=gd2
> extension=curl 
> extension=fileinfo

follow the above things if you face still any issues then ask me I will be helping out to solve your problem Happy Coding

Adeel Ahmed Baloch
  • 672
  • 2
  • 7
  • 15
0

In my case with symfony, i needed to do this :

$dompdf = new Dompdf(array('enable_remote' => true));
    $dompdf->setProtocol('https://');
    $dompdf->setBaseHost($this->requestStack->getCurrentRequest()->getHttpHost());
    $context = stream_context_create([
        'ssl' => [
            'verify_peer' => FALSE,
            'verify_peer_name' => FALSE,
            'allow_self_signed'=> TRUE
        ]
    ]);
    $dompdf->setHttpContext($context);
    $dompdf->loadHtml($htmlContents);
    $dompdf->render();
    $output =  $dompdf->output(array("compress" => 1));
    return $output;

For my image in my twig :

<div><img src="/theme/images/my-image.png"></div>
0

In my case it was I was trying to load a local image using an HTTPS URL but using an untrusted self-signed certificate. I had to add this to make it work:

  $dompdf->setHttpContext(
      stream_context_create([
          'ssl' => [
              'allow_self_signed'=> TRUE,
              'verify_peer' => FALSE,
              'verify_peer_name' => FALSE,
          ]
      ])
    );
Leonardo Nomdedeu
  • 814
  • 1
  • 8
  • 11