0

我正在使用 laravel snappy fot 将 html 转换为 pdf,但外部链接不起作用会引发错误。有 img 标签和 src 是 lorem picsum。我正在使用 Windows 机器,它是本地主机而不是生产。也许Windows是问题,我不确定。但是阅读了很多答案并使用了所有建议,例如 public_path("image_path")、asset("image-path") 但没有一个有效。

退出状态代码 '1' 表示出了点问题:stderr: "Loading pages (1/6) [> ] 0% [======> ] 10% [=========== =====> ] 28% 错误:无法加载https://picsum.photos/200,网络状态代码 3 和 http 状态代码 0 - 找不到主机 [========== ==================================================] 100% 计数页数 (2/6)
[======================================== ====================] 对象 1 的 1 解析链接 (4/6)
[================================================== ===========] Object 1 of 1 加载页眉和页脚 (5/6) 打印页面 (6/6) [>] 准备 [============= =================================================] 第 1 页1 完成 由于网络错误退出代码 1:HostNotFoundError "stdout:"" command: 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,0, $width, $height])->setWarnings(true)->stream();
//        return PDF::loadView('cv')->setPaper([0,0,$width, $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();
    }
4

1 回答 1

0

我在 Windows x64 机器上遇到了同样的问题。是的,您必须使用 public_path 方法。

wkhtmltopdf 在 0.12.6 版本中默认禁用本地文件访问。

对于使用laravel-snappy 的用户,请在 config\snappy.php 中添加“ enable-local-file-access ”选项:

'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'),
        'timeout' => false,
        'options' => [
            'enable-local-file-access' => true,
            'orientation'   => 'landscape',
            'encoding'      => 'UTF-8'
        ],
        'env'     => [],
    ],
于 2020-12-08T04:08:18.373 回答