Laravel dompdf error "Image not found or type unknown"

Viewed 35785

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.

6 Answers

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.

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

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

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

Try this, It works fine in Laravel.

public function pdf()
{
    $users = User::get();
    $pdf = PDF::setOptions([
        'isHtml5ParserEnabled' => true,
        'isRemoteEnabled' => true
    ])->loadView('pdf', compact('users'));

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