Ahsan
Ahsan

Reputation: 1359

Laravel dompdf error "Image not found or type unknown"

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.

Upvotes: 22

Views: 65109

Answers (11)

christian nyembo
christian nyembo

Reputation: 135

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

$pdf = new Dompdf(['chroot' => __DIR__]);

Upvotes: 0

hamil.Dev
hamil.Dev

Reputation: 398

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

Upvotes: 18

GeorgeP
GeorgeP

Reputation: 857

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

Upvotes: 0

Ntuuro
Ntuuro

Reputation: 29

 <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.

Upvotes: 1

IslamYearul
IslamYearul

Reputation: 328

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" />

Upvotes: 2

deepkesh choubey
deepkesh choubey

Reputation: 41

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.

Upvotes: 3

Umar Niazi
Umar Niazi

Reputation: 534

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();

Upvotes: 1

Kamania
Kamania

Reputation: 107

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

Upvotes: 4

Jovi
Jovi

Reputation: 137

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');
}

Upvotes: 12

Tulpa
Tulpa

Reputation: 91

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.

Upvotes: 9

Alexej
Alexej

Reputation: 640

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.

Upvotes: 35

Related Questions