重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章主要介绍了Laravel-snappy怎么导出PDF,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
龙港ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:028-86922220(备注:SSL证书合作)期待与您的合作!
安装
以 ubuntu 为例
1. 执行安装 wkhtmltopdf:
composer require h5cc/wkhtmltopdf-amd64 0.12.x composer require h5cc/wkhtmltoimage-amd64 0.12.x
顾名思义,分别安装的是 wkhtmltopdf 和 wkhtmltoimage。
2. 复制 wkhtmltopdf 到 ubuntu 可执行命令的目录中
sudo cp vendor/h5cc/wkhtmltoimage-amd64/bin/wkhtmltoimage-amd64 /usr/local/bin/ sudo cp vendor/h5cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64 /usr/local/bin/ //并使其可执行: sudo chmod +x /usr/local/bin/wkhtmltoimage-amd64 sudo chmod +x /usr/local/bin/wkhtmltopdf-amd64
3. 安装 laravel-snappy
composer require barryvdh/laravel-snappy
4. 将 ServiceProvider 添加到 config/app.php 中的 providers 数组中
Barryvdh\Snappy\ServiceProvider::class
5. 将 Facades 添加到 config/app.php 中的 aliases 数组中
'PDF' => Barryvdh\Snappy\Facades\SnappyPdf::class, 'SnappyImage' => Barryvdh\Snappy\Facades\SnappyImage::class,
6. 执行生成配置文件
php artisan vendor:publish --provider="Barryvdh\Snappy\ServiceProvider"
可以看到默认的配置文件为 config/snappy.php:
return [ 'pdf' => [ 'enabled' => true, 'binary' => env('WKHTML_PDF_BINARY', '/usr/local/bin/wkhtmltopdf'), 'timeout' => false, 'options' => [], 'env' => [], ], 'image' => [ 'enabled' => true, 'binary' => env('WKHTML_IMG_BINARY', '/usr/local/bin/wkhtmltoimage'), 'timeout' => false, 'options' => [], 'env' => [], ], ];
注意,这里有个坑,默认 binary 配置为 /usr/local/bin/wkhtmltopdf 和 /usr/local/bin/wkhtmltoimage,在第一次使用的时候,会报错 /usr/local/bin/wkhtmltopdf不存在,这是因为在 linux 系统下,wkhtmltopdf 和 wkhtmltoimage 的真实路径和名称为:/usr/local/bin/wkhtmltopdf-amd64 和 /usr/local/bin/wkhtmltoimage-amd64。
因此,需要把配置信息修改为:
'pdf' => [ ... 'binary' => env('WKHTML_PDF_BINARY', '/usr/local/bin/wkhtmltopdf-amd64'), ... ], 'image' => [ ... 'binary' => env('WKHTML_IMG_BINARY', '/usr/local/bin/wkhtmltoimage-amd64'), ... ],
开始使用
//使用方法1 $pdf = \PDF::loadView('welcome', $data); return $pdf->download('welcome.pdf'); //使用方法2 $html = '订单id
12346546
'; $pdf = \PDF::loadHTML($html); return $pdf->inline();
很多博客里没有提到,使用方法 1 中,会报这样的错:
The exit status code '1' says something went wrong: stderr: "Loading pages (1/6) [> ] 0% [======> ] 10% QSslSocket: cannot resolve CRYPTO_num_locks QSslSocket: cannot resolve CRYPTO_set_id_callback QSslSocket: cannot resolve CRYPTO_set_locking_callback QSslSocket: cannot resolve sk_free QSslSocket: cannot resolve sk_num QSslSocket: cannot resolve sk_pop_free QSslSocket: cannot resolve sk_value QSslSocket: cannot resolve SSL_library_init QSslSocket: cannot resolve SSL_load_error_strings QSslSocket: cannot resolve SSLv3_client_method QSslSocket: cannot resolve SSLv23_client_method QSslSocket: cannot resolve SSLv3_server_method QSslSocket: cannot resolve SSLv23_server_method QSslSocket: cannot resolve X509_STORE_CTX_get_chain QSslSocket: cannot resolve OPENSSL_add_all_algorithms_noconf QSslSocket: cannot resolve OPENSSL_add_all_algorithms_conf QSslSocket: cannot resolve SSLeay QSslSocket: cannot call unresolved function CRYPTO_num_locks QSslSocket: cannot call unresolved function CRYPTO_set_id_callback QSslSocket: cannot call unresolved function CRYPTO_set_locking_callback QSslSocket: cannot call unresolved function SSL_library_init QSslSocket: cannot call unresolved function SSLv23_client_method QSslSocket: cannot call unresolved function sk_num [==================> ] 31% QSslSocket: cannot call unresolved function SSLv23_client_method QSslSocket: cannot call unresolved function SSL_library_init [============================================================] 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 1 Done Exit with code 1 due to network error: UnknownNetworkError QSslSocket: cannot call unresolved function CRYPTO_num_locks QSslSocket: cannot call unresolved function CRYPTO_set_id_callback QSslSocket: cannot call unresolved function CRYPTO_set_locking_callback " stdout: "" command: /usr/local/bin/wkhtmltopdf-amd64 --lowquality '/tmp/knp_snappy612c3edcdfc855.21787864.html' '/tmp/knp_snappy612c3edcdfce49.80482557.pdf'.
执行:
sudo apt-get update sudo apt install libssl1.0-dev
修复完成,导出 welcome 页面。
如果使用 save () 方法保存,默认保存到 /public 文件夹下,并且如果文件名相同的情况下,第二次保存会提示文件已经存在。
感谢你能够认真阅读完这篇文章,希望小编分享的“Laravel-snappy怎么导出PDF”这篇文章对大家有帮助,同时也希望大家多多支持创新互联,关注创新互联行业资讯频道,更多相关知识等着你来学习!