Laravel dompdf error "Image not found or type unknown"
Asked Answered
G

11

21

I am getting error "Image not found or type unknown" after downloading PDF in Laravel 5.4 using dompdf package. Here is the method

public function pdf()
    {
        $users = User::get();
        $pdf = PDF::loadView('pdf', compact('users'));

        return $pdf->download('Users.pdf');
    }

My view file

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>PDF</title>
</head>
<body> 
    <div class="container">
        <div class="row">
            @foreach ($users as $user)
                <img src="public/storage/images/{{ $user->profile_pic }}" alt="" style="width: 150px; height: 150px;">
            @endforeach
        </div>
    </div>
</body>
</html>

If I try with static image name (like following), it works

<img src="public/storage/images/image_1.jpg" alt="" style="width: 150px; height: 150px;">

But does not work with dynamic name.

Please suggest how can I can fix it.

Gallnut answered 15/8, 2017 at 9:58 Comment(3)
Possible duplicate of dompdf and img tag, image wont showArtilleryman
Hi, its not the same issue as the one you mentioned. That post was about a fixed image which I can make work (see my post details). This was about dynamic image url. Secondly the solution there did not work for me. I tried it before posting here. In fact, I tried other solutions provided in stackoverflow and dompdf github page. Nothing worked @ArtillerymanGallnut
Is this question about barryvdh/laravel-dompdf? If yes, why not specify it?Raised
H
35

According to this question you have to use the full server path try:

<img src="{{ public_path("storage/images/".$user->profile_pic) }}" alt="" style="width: 150px; height: 150px;">

Assuming the image is stored in your public directory.

Hildick answered 15/8, 2017 at 18:7 Comment(0)
C
16

in my case. it work on windows but don't work on ubuntu 20.

i change to

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

and it work fine

Cosper answered 30/5, 2022 at 15:17 Comment(2)
this is worked for me . I used laravel 8Riannon
the only code that worked for meElison
C
12
public function pdf()
{
    $users = User::get();
    $pdf = PDF::loadView('pdf', compact('users'));
    $pdf->getDomPDF()->setHttpContext(
        stream_context_create([
            'ssl' => [
                'allow_self_signed'=> TRUE,
                'verify_peer' => FALSE,
                'verify_peer_name' => FALSE,
            ]
        ])
    );

    return $pdf->download('Users.pdf');
}
Centre answered 9/8, 2019 at 6:5 Comment(1)
This works for the remote imagesPowder
O
9

I've solved it:

1- Call the DomPDF library:

require_once ($_SERVER['DOCUMENT_ROOT'] . "/mi/LiquidacionesSueldo/include/dompdf/autoload.inc.php");
    ob_start();

2- Generate the HTML file, I do it with a function that returns a simply HTML table:

get_dias_goce_sueldo($_POST['run'],$_POST['dvr'],$_POST['cantidad_dias'],$_POST['desde'],$_POST['hasta'],$_POST['am_pm']);

3- Now, you must initialize the DomPDF object, note all the options that I set:

    $dompdf=new Dompdf\Dompdf();
    $dompdf->set_option('isHtml5ParserEnabled', true);
    $dompdf->set_option('isRemoteEnabled', true);   
    $dompdf->loadHtml(ob_get_clean());
    $dompdf->set_paper('letter', 'portrait');
    $dompdf->render();
    $dompdf->stream("Solicitud_de_permiso_con_goce_de_sueldo.pdf", array('Attachment'=>1));

NOTE: in my function, in the HTML table, I call the image with the full path, this way:

<img src="'.$_SERVER['DOCUMENT_ROOT'].'/mi/DiasAdministrativos/logo.png">

That's all, I hope this will be usefull, that was for me.

Operate answered 17/4, 2019 at 14:38 Comment(1)
$dompdf->set_option('isHtml5ParserEnabled', true); $dompdf->set_option('isRemoteEnabled', true);I just add this 2 option and images are loaded in my pdf. --- thank youCoulter
W
4

If you are using asset(image/location) library to get your image, try replace with public_path('image/location')

Whiplash answered 7/7, 2022 at 23:43 Comment(0)
H
2

Adding the option is remote enabled to true works for me. After this image should be an HTTP URL.

$dompdf->set_option('isRemoteEnabled', true);

The issue came when I moved my code from php5.6 to php7.3. Older code is still working on the php5.6 server where I was passing src as an absolute path.

Hyposensitize answered 11/10, 2022 at 5:34 Comment(0)
W
2

Use This Way

 <img src="data:image/png;base64,{{ base64_encode(file_get_contents(public_path($Customer->customer_image))) }}" class="image img-thumbnail" height="100px" width="100px" />
Weitzel answered 22/10, 2022 at 11:52 Comment(0)
C
1

It turned out to be OPTIONS issue. Try to setup these options:

$PDFOptions = ['enable_remote' => true, 'chroot' => public_path('storage/resource-booking')];

PDF::setOptions($PDFOptions)->loadView();
Carmagnole answered 28/9, 2022 at 6:45 Comment(0)
G
1
 <img src="data:image/png;base64,{{ base64_encode(file_get_contents(public_path($Customer->customer_image))) }}" class="image img-thumbnail" height="100px" width="100px" />

This worked but I also had to install the GD library.

sudo apt-get install php8.x-gd

Also, restart your apache service and Laravel server.

Gibbet answered 7/11, 2022 at 8:20 Comment(0)
D
0

Make sure your page is not password protected, for example when you are working on a staging environment.

The class needs public access to your page for this option to work:

$dompdf->set_option('isRemoteEnabled', true);
Disjointed answered 30/1, 2023 at 12:25 Comment(0)
M
0

In my case, I had to set "chroot" to my working directory instead of default dompdf directory

$pdf = new Dompdf(['chroot' => __DIR__]);
Magnetic answered 28/4, 2023 at 9:39 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.