Dompdf remote image is not displaying in pdf
Asked Answered
S

28

31

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
Scroop answered 1/3, 2013 at 8:4 Comment(2)
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.Teleplay
no. still not found it.Scroop
T
52

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.

Teleplay answered 19/4, 2014 at 10:13 Comment(3)
+1 was searching on issue from half n hour and this one really helped.Townsend
Getting issue if the image name has space.Wilhite
Thanks. This helped me. BTW I had to remove public. "<img src="images/thumb.png"> " Hope this will help others.Hallah
F
67

Try

use Dompdf\Options;
$options = new Options();
$options->set('isRemoteEnabled', true);
$dompdf = new Dompdf($options);
Flub answered 26/10, 2016 at 8:9 Comment(9)
I had to use the Options class as well, like so: use Dompdf\Options;Fibster
In my case, $dompdf = new DOMPDF(); $dompdf->set_option('isRemoteEnabled', TRUE); was sufficient (i.e. no need for an extra class initialisation)Camire
set_option() is deprecatedAffusion
i know this is late now, but i tried it and it is not working for me on Yii2Growing
@aexl ...we really doesnt need any other lines. thumbs up for you. ThanksNovelist
This worked for me. Note - if you haven't imported the namespace with the use operator, you can write $options = new Dompdf\Options();Misfortune
Options class is undefinedMozart
Thankyou, $options->set('isRemoteEnabled', true); that worked smooth :)Sc
@Flub Worked on the local server, but not on the production server why?Grunion
T
52

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.

Teleplay answered 19/4, 2014 at 10:13 Comment(3)
+1 was searching on issue from half n hour and this one really helped.Townsend
Getting issue if the image name has space.Wilhite
Thanks. This helped me. BTW I had to remove public. "<img src="images/thumb.png"> " Hope this will help others.Hallah
D
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.

Dihedron answered 20/10, 2014 at 7:6 Comment(0)
S
6

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 );
Simulacrum answered 20/10, 2021 at 13:29 Comment(1)
This solution is what worked for me!! Thank you!Haberman
W
6

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

Wriggler answered 26/1, 2023 at 8:34 Comment(1)
This solution also works especially if you have a private image file outside the public web folder.Mendacity
R
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);
Runoff answered 6/10, 2019 at 7:59 Comment(0)
A
4

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

getcwd().'path/to/image'

It works for me!

Alterative answered 28/7, 2020 at 7:40 Comment(0)
M
3

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

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

Megadeath answered 21/4, 2020 at 23:2 Comment(1)
This option worked for me for a CI ProjectSpavined
M
3

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>
Merit answered 12/1, 2022 at 22:17 Comment(0)
T
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?)

Through answered 1/3, 2013 at 8:13 Comment(1)
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 accessScroop
H
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

Holladay answered 29/12, 2013 at 21:40 Comment(0)
P
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

Placebo answered 1/9, 2019 at 10:17 Comment(1)
set_option is deprecatedMozart
N
2

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

Noleta answered 17/11, 2019 at 22:53 Comment(0)
S
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.

Saunder answered 18/3, 2021 at 10:24 Comment(0)
R
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

Raffin answered 31/3, 2022 at 23:13 Comment(0)
C
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.

Carborundum answered 1/3, 2013 at 14:34 Comment(0)
T
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.

Tseng answered 28/1, 2015 at 8:10 Comment(0)
S
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>

Swacked answered 9/5, 2018 at 1:52 Comment(0)
A
0

I used this line of code

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

It's working fine. Thanks.

Axiology answered 9/3, 2019 at 8:42 Comment(0)
R
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);
Ramification answered 2/1, 2020 at 7:10 Comment(0)
H
0

Try use full directory path with .jpg image

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

Hadria answered 25/10, 2020 at 13:11 Comment(0)
M
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,
            ]
        ])
    );
Mortality answered 26/11, 2021 at 9:28 Comment(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.Sw
M
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.

Maite answered 1/4, 2022 at 12:32 Comment(0)
E
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

Ejaculation answered 10/4, 2023 at 6:49 Comment(0)
M
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>
Mervinmerwin answered 21/6, 2023 at 8:13 Comment(0)
A
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,
          ]
      ])
    );
Agnosticism answered 29/6, 2023 at 11:46 Comment(0)
I
0

I use this type URL, its working. I faced this issue{Image not found or type unknown}

<div class="left-content">
    <img src="../public/images/logo.jpg" alt="Logo" style="max-width: 30%; height: auto;">
</div>
Ipswich answered 29/4, 2024 at 4:35 Comment(1)
<div class="left-content"> <img src="../public/images/logo.jpg" alt="Logo" style="max-width: 30%; height: auto;"> </div>Ipswich
V
0

Last but not least, check image format. The library barryvdh/laravel-dompdf does not support .webp image format yet, and it does not return any warning about it either.

Vulnerable answered 5/6, 2024 at 0:19 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.