【问题标题】:laravel snappy external link not workslaravel snappy 外部链接不起作用
【发布时间】:2021-02-04 00:18:28
【问题描述】:

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

退出状态代码 '1' 表示出了点问题:stderr: "Loading 页数 (1/6) [> ] 0% [======> ] 10% [================> ] 28% 错误: 无法加载https://picsum.photos/200,网络状态代码为 3 和 http 状态码 0 - 找不到主机 [================================================== ===========] 100% 计数页数 (2/6)
[================================================== ===========] 对象 1 of 1 解析链接 (4/6)
[================================================== ===========] 对象 1 of 1 加载页眉和页脚 (5/6) 打印页 (6/6) [> ] 准备中 [================================================== ===========] 第 1 页 1 完成 由于网络错误退出,代码为 1:HostNotFoundError " 标准输出:"" 命令: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();
    }

【问题讨论】:

    标签: laravel wkhtmltopdf external-links laravel-snappy


    【解决方案1】:

    我在 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'     => [],
        ],
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-07
      • 2017-02-03
      • 2021-08-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多