Laravel dompdf 错误“找不到图像或类型未知”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45690599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel dompdf error "Image not found or type unknown"
提问by Ahsan
I am getting error "Image not found or type unknown" after downloading PDF in Laravel 5.4 using dompdf package. Here is the method
使用 dompdf 包在 Laravel 5.4 中下载 PDF 后,出现错误“找不到图像或类型未知”。这是方法
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.
请建议我如何修复它。
回答by Alexej
回答by Tulpa
I've solved it:
我已经解决了:
1- Call the DomPDF library:
1- 调用 DomPDF 库:
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:
2- 生成 HTML 文件,我使用一个返回简单 HTML 表的函数来完成:
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:
3- 现在,您必须初始化 DomPDF 对象,注意我设置的所有选项:
$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:
注意:在我的函数中,在 HTML 表中,我使用完整路径调用图像,如下所示:
<img src="'.$_SERVER['DOCUMENT_ROOT'].'/mi/DiasAdministrativos/logo.png">
That's all, I hope this will be usefull, that was for me.
就是这样,我希望这对我有用。
回答by Jovi
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');
}