php - On pdf generate getting Image not found or type unknown barryvdhdompdf error
Get the solution ↓↓↓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
Answer
Solution:
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.
Answer
Solution:
Your class is wrong, to use laravel-dompdf you should callPDF
or\Barryvdh\DomPDF\PDF
. and change/test/example-app/storage/images/profile_1611820816863.jpg
to real absolute path. if your image folder insideapp
folder you should change toapp_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 toapp_path('/storage/images/profile_1611820816863.jpg')
and also setchroot
toapp_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));
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: illegal string offset
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.