Laravel中使用laravel-snappy导出PDF

分类: 编程
日期: 2022-04-20
浏览: 652

环境安装

安装wkhtmltopdf

下载地址:wkhtmltopdf

CentOS8为例

sudo dnf install wkhtmltox-0.12.6-1.centos8.x86_64.rpm
安装laravel-snappy

GitHub地址: laravel-snappy

composer require barryvdh/laravel-snappy

将以下代码添加到config/app.phpaliases中,可以编写更少的代码

'PDF' => Barryvdh\Snappy\Facades\SnappyPdf::class,
'SnappyImage' => Barryvdh\Snappy\Facades\SnappyImage::class,

发布配置文件

php artisan vendor:publish --provider="Barryvdh\Snappy\ServiceProvider"

配置文件中定义pdf的代码如下,binary用来定义wkhtmltopdf可执行文件的路径

'pdf' => [
    'enabled' => true,
    'binary'  => env('WKHTML_PDF_BINARY', '/usr/local/bin/wkhtmltopdf'),
    'timeout' => false,
    'options' => [],
    'env'     => [],
],

使用示例

使用App容器
$snappy = App::make('snappy.pdf');
//To file
$html = '<h1>Bill</h1><p>You owe me money, dude.</p>';
$snappy->generateFromHtml($html, '/tmp/bill-123.pdf');
$snappy->generate('http://www.github.com', '/tmp/github.pdf');
//Or output:
return new Response(
    $snappy->getOutputFromHtml($html),
    200,
    array(
        'Content-Type'          => 'application/pdf',
        'Content-Disposition'   => 'attachment; filename="file.pdf"'
    )
);
使用wrapper
$pdf = App::make('snappy.pdf.wrapper');
$pdf->loadHTML('<h1>Test</h1>');
return $pdf->inline();
使用facade(推荐)
$pdf = PDF::loadView('pdf.invoice', $data);
return $pdf->download('invoice.pdf');
链式调用方法
PDF::loadHTML($html)->setPaper('a4')->setOrientation('landscape')->setOption('margin-bottom', 0)->save('myfile.pdf')

更多设置可以参考 wkhtmltopdf 手册

中文字体问题

下载simsun.ttc字体放到/usr/share/fonts目录下,或者在该路径下新增文件夹放置。simsun.ttc是宋体,下载其它支持中文的字体也可。

ProtocolUnknownError

报类似以下的错误

The exit status code '1' says something went wrong: stderr: "Loading pages (1/6) [> ] 0% [======> ] 10% Warning: Blocked access to file [========> ] 14% Error: Failed to load about:blank, with network status code 301 and http status code 0 - Protocol "about" is unknown [=========================> ] 42% [===========================> ] 46% [============================> ] 48% [=======================================> ] 66% [=================================================> ] 82% [============================================================] 100% Counting pages (2/6) [============================================================] Object 1 of 1 Resolving links (4/6) [============================================================] Object 1 of 1 Loading headers and footers (5/6) Printing pages (6/6) [> ] Preparing [==============================> ] Page 1 of 2 [============================================================] Page 2 of 2 Done Exit with code 1 due to network error: ProtocolUnknownError " stdout: "" command: /usr/local/bin/wkhtmltopdf --lowquality --page-size 'a4' '/tmp/knp_snappy6260c4b1895344.43757367.html' '/tmp/knp_snappy6260c4b1895c39.67784048.pdf'.
解决方法

wkhtmltopdf命令加上--enable-local-file-access

setOption('enable-local-file-access', true)

示例

$pdf = PDF::loadView('pdf.invoice', $data);
return $pdf->setPaper('a4')->setOption('enable-local-file-access', true)->download('myfile.pdf');
版权声明
作者:不二
来源:不二博客
文章版权归作者所有,未经允许请勿转载。
共有 0 条评论
赶快发表评论吧~
发表评论