Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 994
Ahsan Avatar asked Aug 15 '17 09:08

Ahsan


2 Answers

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.

like image 160
Alexej Avatar answered Sep 16 '22 23:09

Alexej


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.

like image 36
Tulpa Avatar answered Sep 17 '22 23:09

Tulpa