Laravel dompdf error “Image not found or type unknown”

柔情痞子 提交于 2020-02-03 08:23:42

问题


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.


回答1:


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.




回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/45690599/laravel-dompdf-error-image-not-found-or-type-unknown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!