On pdf generate getting Image not found or type unknown barryvdh\dompdf error

Viewed 803

I am getting below error when generating a PDF in Laravel using barryvdh/laravel-dompdf package of laravel.

Image not found or type unknown

HTML

<html>
<head>
    <style>
        table
        {
            border-color: #000;
            color: #000;
            border-collapse: collapse;
            font-family: sans-serif;
        }   
    </style>
</head>
<body>
    <table style="width: 100%; margin-top: 10px;" cellpadding="3" border="1">
        <tr>
            <td  style="text-align: right; font-weight: bold; font-size: 14px; padding-left: 5px;">User Image</td>
            <td colspan="4" style="height: 50px;">
                <img src="/val/test/storage/images/profile_1611820816863.jpg" height="70">
            </td>
        </tr>
    </table>
</body>
</html>

Level

 $complete_html = '<above HTML>';
 $dompdf = new Dompdf();
                $dompdf->loadHtml($complete_html);
                $page_size = 'A4';
                $dompdf->setPaper($page_size, 'portrait');
                $dompdf->render();

                $file_name = 'test_dom';
                $dompdf->stream($file_name.'.pdf', array("Attachment" => 1));

I have reviewed other post on this topic and set "RemoteEnable" as true in dompdf config file and restarted the server but still getting this issue.

Edit I have changed image path it is a shared folder path, not project or local path. Also, file and folder path both have full permission to read and write(777) is already given

2 Answers

This could be because of a file permission issue, Please check your BL server has all required access to read/write an image file from the shared location.

This could happen only if the server configuration could have been changed.

Your class is wrong, to use laravel-dompdf you should call PDF or \Barryvdh\DomPDF\PDF. and change /test/example-app/storage/images/profile_1611820816863.jpg to real absolute path. if your image folder inside app folder you should change to app_path('storage/images/profile_1611820816863.jpg'). Below is working code using dompdf wrapper:

$pdf     = \Barryvdh\DomPDF\PDF::loadHtml('<html><head>
<style>
        table
        {
            border-color: #000;
            color: #000;
            border-collapse: collapse;
            font-family: sans-serif;
        }   
</style>
</head>
<body>
    <table style="width: 100%; margin-top: 10px;" cellpadding="3" border="1">
        <tr>
            <td  style="text-align: right; font-weight: bold; font-size: 14px; padding-left: 5px;">User Image</td>
            <td colspan="4" style="height: 50px;">
                <img src="'.app_path('storage/images/profile_1611820816863.jpg').'" height="70>
            </td>
        </tr>
    </table>
</body></html>');
$pdf->stream('test_pdf.pdf', array("Attachment" => 1));

if you want still use original dompdf without wrapper, you need to change image url to app_path('/storage/images/profile_1611820816863.jpg') and also set chroot to app_path()

below is my tested working code withoud wrapper:

         $complete_html = '<html><head>
<style>
        table
        {
            border-color: #000;
            color: #000;
            border-collapse: collapse;
            font-family: sans-serif;
        }   
</style>
</head>
<body>
    <table style="width: 100%; margin-top: 10px;" cellpadding="3" border="1">
        <tr>
            <td  style="text-align: right; font-weight: bold; font-size: 14px; padding-left: 5px;">User Image</td>
            <td colspan="4" style="height: 50px;">
                <img src="'.app_path('/test/example-app/storage/images/profile_1611820816863.jpg').'" height="70>
            </td>
        </tr>
    </table>
</body></html>';
 $dompdf = new \Dompdf\Dompdf();
 $options = $dompdf->getOptions();
$options->setChroot(app_path());
$options->setIsRemoteEnabled(true);
$dompdf->setOptions($options);

     $dompdf->loadHtml($complete_html);

     $page_size = 'A4';
     $dompdf->setPaper($page_size, 'portrait');
     $dompdf->render();

     $file_name = 'test_dom';
     $dompdf->stream($file_name.'.pdf', array("Attachment" => 1));
Related