dompdf : image not found or type unknown
Asked Answered
G

12

7

Here is my code, I mostly tried all the methods for displaying image on PDF but still didnt work. Could you please help me for this. I also set DOMPDF_ENABLE_REMOTE to true and still results is same.

require_once("dompdf/autoload.inc.php");
//require('WriteHTML.php');
$html = '<body>
 <div id="watermark"><img src="/var/htdocs/PDF/header.png" height="100%" width="100%"></div>
  <div id="header">
    <h3 style="padding-top:10px;">'.$_POST['name'].'</h3>
  </div>
  <div id="footer">
    <h3 style="padding-top:8px; padding-right:25px;" class="CIJ">ABSOLUTE MARKET INSIGHTS</h3>
  </div>
   <div>
    <br><br><div class="ooo" style="padding-top: 20px;">'.$_POST['comment'].'</div>
  </div>
</body>';

use Dompdf\Dompdf;
$dompdf = new Dompdf();
$canvas = $dompdf->get_canvas();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream($_POST["name"], array("Attachment" => false));
Gudrun answered 3/11, 2016 at 10:9 Comment(2)
if you are still getting the issue in image, then you can debug the issue on this file: vendor/dompdf/dompdf/src/Image/Cache.php you will get the understanding from where the issue is occuring.Insufflate
We had the same issue on an older project. Turns out in 0.8.6 release of DomPDF, the security for loading external resources was enhanced, and broke the loading of images. So you could revert to 0.8.5; obviously not recommended to use a release from 2020.Fachini
P
9

You should be using the full URL instead of a direct path. Especially when it is not a static image: dompdf will open that php script directly, so it won't be executed as if it's a PHP script.

If the full URL doesn't work, you can also show what the result of header.php is. Some good things to keep in mind are to send proper content-type headers and so on.

Peh answered 3/11, 2016 at 10:31 Comment(3)
Actually, it is my mistake, its not "header.php" its "header.png"Gudrun
if you are still getting the issue in image, then you can debug the issue on this file: vendor/dompdf/dompdf/src/Image/Cache.php you will get the understanding from where the issue is occuring.Insufflate
i found my solution here #45691099Loella
B
19

You need to add 'isRemoteEnabled' => true

return PDF::setOptions(['isHtml5ParserEnabled' => true, 'isRemoteEnabled' => true])->loadView('reports.invoiceSell')->stream();
Bolshevik answered 22/3, 2021 at 2:49 Comment(1)
Where I set this option? I am using dompdf using composer.Cay
P
9

You should be using the full URL instead of a direct path. Especially when it is not a static image: dompdf will open that php script directly, so it won't be executed as if it's a PHP script.

If the full URL doesn't work, you can also show what the result of header.php is. Some good things to keep in mind are to send proper content-type headers and so on.

Peh answered 3/11, 2016 at 10:31 Comment(3)
Actually, it is my mistake, its not "header.php" its "header.png"Gudrun
if you are still getting the issue in image, then you can debug the issue on this file: vendor/dompdf/dompdf/src/Image/Cache.php you will get the understanding from where the issue is occuring.Insufflate
i found my solution here #45691099Loella
A
8

Finally I Got Correct Solution to work with any server -> Use Base64 in Img src attribute :)

Alva answered 9/1, 2021 at 3:30 Comment(1)
This is a way better solution than everything else. Super reliable and lets you keep it all "server side." Thank you for sharing this.Shortsighted
E
8

You can verify if the error not is beacause questions of security of the Dompdf's version:

$path = 'NameImage.jpg';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);

this code will "turn your image" to base64 format. This resolved my problems :)

Endowment answered 23/4, 2023 at 14:50 Comment(1)
Great, it solved my problem too. Thank you very much.Crasis
S
4

I tried a few different solutions and want to share, how I solved this.

  1. In the 'vendor' directory find: 'Image not found or type unknown' (For me it was: /vendor/dompdf/dompdf/src/Image/Cache.php)

public static $error_message = "Image not found or type unknown";

  1. After that you will find one place in the same file, where this error message is used.

  2. It's possible you need before modification file change permissions for that file, to do that you can use:

sudo chmod -R 777 ./vendor

  1. Modify the file by adding inside catch('} catch (ImageException $e) {') this:

$message = $e->getMessage();

dump($e->getMessage()); die;

  1. Try to build PDF. Now you should see a more detailed report(Don't forget to change those changes back...).

  2. I got an error about chroot problems. I tried to change chroot for the image, but it's not helping, because there is a different chroot for me in the server and in localhost, so I modified it like this:

     $options = new Options();
     $options->setChroot('');
    
     $dompdf = new Dompdf();
     $dompdf->setOptions($options);
     $dompdf->loadHtml($html, 'UTF-8');
     $dompdf->render();
    
Seventieth answered 14/2, 2023 at 21:9 Comment(2)
Thanks! We can also use 'chroot' => '' into the Options param like this: new Options([ 'chroot' => '' ]);Antemortem
I had more success with $options->setChroot('/'); And I provided full path for each file ex: /var/www/html/myapp/public/image/logo.jpgProviso
O
3

Another alternative that works in version 1.0.2 is to make a change to the file.

dompdf/src/Image/Cache.php

Set the variable ($chrootValid) to true

$chrootValid = true;

*In this situation you cannot have the full path

Obituary answered 24/12, 2021 at 17:36 Comment(0)
C
1

On mac, below error will be generated, but in Dompdf this error message will not be displayed.

"Unable to create temporary image in /var/folders/cc/nxb7r62n4vb0dds455ss6pw0000gn/T "

$options = new Options();
$options->setTempDir('temp'); // temp folder with write permission

$dompdf = new Dompdf();
$dompdf->setOptions($options);
Cleromancy answered 19/9, 2020 at 10:36 Comment(1)
Thank you very much. You make my day. I tried too much way on my MAC, finally it's working.Gorges
R
1

You should first go to your image source. There, just add the server link, your localhost link, or whatever you are using.

For example, suppose you are using a localhost, then just add it before:

<img 
    src="https://i.sstatic.net/ADiZy.jpg?s=256&g=1" 
    height="100px" 
    width="100px"
>
</div>

Then, in the controller or in the library where we added $dompdf->load_html($html);, just add:

$dompdf->set_option('isRemoteEnabled', true);
Radke answered 30/1, 2024 at 14:47 Comment(0)
N
0

Have you tried full path/url:
<img src="http://domain.com/var/htdocs/PDF/header.png" height="100%" width="100%">

Or with a more variable solution:

$baseurl = "http://www.domain.com";
echo '<img src="' . $baseurl . '/var/htdocs/PDF/header.php" height="100%" width="100%">';
Nibelung answered 3/11, 2016 at 10:18 Comment(3)
Not Working. :(Gudrun
And domain.com/var/htdocs/PDF/header.png is returning an image? Is the image public reachable, because I use in development ip blocking and then the images can't be retrieved by DomPDF .Nibelung
its my typing mistake, its a "header.png" not ".php" sorry for inconvenienceGudrun
J
0

I tried every other solution above - but the only thing that worked for me was keeping the images inside the dompdf folder and then referencing them from that folder.

Jenks answered 11/12, 2021 at 14:18 Comment(0)
M
0

Finally I got correct solution to work with any server -> Use Base64 in Img src attribute, like this:

<img src="{{$variable['signature']}}" />
Modify answered 12/1, 2022 at 15:44 Comment(1)
the code example: <img src="{{$variable['signature']}}" />Modify
D
0

I faced this issue for a long time working on Mac and could not solve it with any of the solutions I read here. I finally got it working right by hardcoding into Dompdf’s source, so I’ll leave my solution here in case anyone is still stuck:

  1. Go to dompdf/src/Options.php file.
  2. Around line 317 you should find the expression $this->setTempDir(sys_get_temp_dir());
  3. Replace it for $this->setTempDir(dirname(__FILE__)); and test.

Hope this helps someone!

Degraw answered 8/6, 2024 at 5:35 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.