Laravel pdf生成显示(snappy库)返回ERR_INVALID_RESPONSE


Laravel pdf generation display (snappy library) returning ERR_INVALID_RESPONSE

我在尝试snappy库的示例时遇到了一些麻烦。当我试图使用getOutput函数显示时,浏览器正在返回ERR_INVALID_RESPONSE。我尝试了解决方案张贴在这里,但它不适合我。

下面是我的函数代码:

    // Display the resulting pdf in the browser
    // by setting the Content-type header to pdf
    $snappy = new Pdf('/usr/local/bin/wkhtmltopdf');
    header('Content-Type: application/pdf',true,200);
    header('Content-Disposition: attachment; filename="file.pdf"');
    echo $snappy->getOutput('http://www.github.com');
非常感谢你的帮助/建议/建议。提前感谢!

尝试先将pdf保存到本地存储,然后返回响应。

它看起来像这样:

$snappy = new Pdf('/usr/local/bin/wkhtmltopdf');
$snappy->save('path/to/your/storage');
$response = Response::make(Storage::get('path/to/your/storage'), 200);
$response->header("Content-Type", 'application/pdf');
$response->header("Content-Disposition", 'attachment; filename=file.pdf');
return $response;

既然你使用的是Laravel,我建议你使用Laravel包装器

Snappy PDF/Image Wrapper for Laravel 5 and Lumen 5.1

我猜wkhtmltopdf的二进制文件缺失。你可以通过在你的Laravel项目所在的文件夹中执行以下命令来安装它:

$ composer require h4cc/wkhtmltopdf-i386 0.12.x

或64位系统:

$ composer require h4cc/wkhtmltopdf-amd64 0.12.x

那么你需要修改函数中的这一行:

$snappy = new Pdf('/usr/local/bin/wkhtmltopdf');

$snappy = new Pdf(base_path('vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64'));

source: Wkhtmltopdf二进制安装作为composer依赖