问题描述
我正在使用laravel snappy fot将html转换为pdf,但是外部链接不起作用会引发错误。有img标签,而src是lorem picsum。我正在使用Windows计算机,它是本地主机而不是生产服务器。也许Windows是问题,我不确定。但是请阅读很多答案,并使用所有建议,例如public_path(“ image_path”),asset(“ image-path”),但没有一个起作用。
退出状态代码“ 1”表示出了点问题:stderr:“正在加载 页数(1/6)[>] 0%[======>] 10%[===============>] 28%错误: 无法加载网络状态代码为3的https://picsum.photos/200 和http状态码0-找不到主机 [================================================= ===========] 100% 页数(2/6)
[================================================= ===========]对象 1 of 1解析链接(4/6)
[================================================= ===========]对象 1/1加载页眉和页脚(5/6)打印页面(6/6)[>] 准备中 [================================================= ===========]第1页 由于网络错误,退出代码为1的退出次数:HostNotFoundError “ stdout:”“命令:C:\ wamp64 \ bin \ wkhtmltopdf \ bin \ wkhtmltopdf --lowquality --page-height“ 1000px” --page-width“ 992px”“ C:\ Users \ hpp_2 \ AppData \ Local \ Temp \ knp_snappy5f8fcd256bf775.59730728.html” “ C:\ Users \ hpp_2 \ AppData \ Local \ Temp \ knp_snappy5f8fcd256d5fa6.11170764.pdf”。
我认为不需要添加完整的html内容,因为它只是img标签而已
<img src="https://picsum.photos/200">
这是PHP:
public function downloadCv(Request $request)
{
$width = 992;
$height = 1000;
$html = file_get_contents('storage/cv-templates/01.html');
// $image = public_path().'\images\bg-25.png';
// dd($image);
// $html = str_replace('<!-- %img% -->','<img src="file://'.$image.'" width="200" height="100">',$html);
// $html = str_replace('fontSrc',storage_path().'/fonts/Montserrat-Light.ttf',$html);
// return PDF::loadHtml($html)->setoptions(["isRemoteEnabled" => true])->setPaper([0,$width,$height])->setWarnings(true)->stream();
// return PDF::loadView('cv')->setPaper([0,$height])->setWarnings(true)->stream();
// return $html;
// return view('cv');
return SnappyPdf::loadHtml($html)
->setoption('page-width',$width.'px')
->setoption('page-height',$height.'px')
->setoption('disable-external-links',false)
->setWarnings(true)
->inline();
}
解决方法
我在Windows x64机器上有同样的问题。是的,您必须使用public_path方法。
wkhtmltopdf在0.12.6版本中默认禁用本地文件访问。
对于使用 laravel-snappy 的用户,在config \ snappy.php中添加“ 启用本地文件访问”选项:
'pdf' => [
'enabled' => true,'binary' => env('WKHTML_PDF_BINARY','/usr/local/bin/wkhtmltopdf'),'timeout' => false,'options' => [
'enable-local-file-access' => true,'orientation' => 'landscape','encoding' => 'UTF-8'
],'env' => [],],'image' => [
'enabled' => true,'binary' => env('WKHTML_IMG_BINARY','/usr/local/bin/wkhtmltoimage'),