這篇文章主要介紹了Laravel-snappy怎么導(dǎo)出PDF,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
龍港ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:028-86922220(備注:SSL證書合作)期待與您的合作!
安裝
以 ubuntu 為例
1. 執(zhí)行安裝 wkhtmltopdf:
composer require h5cc/wkhtmltopdf-amd64 0.12.x composer require h5cc/wkhtmltoimage-amd64 0.12.x
顧名思義,分別安裝的是 wkhtmltopdf 和 wkhtmltoimage。
2. 復(fù)制 wkhtmltopdf 到 ubuntu 可執(zhí)行命令的目錄中
sudo cp vendor/h5cc/wkhtmltoimage-amd64/bin/wkhtmltoimage-amd64 /usr/local/bin/ sudo cp vendor/h5cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64 /usr/local/bin/ //并使其可執(zhí)行: 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 數(shù)組中
Barryvdh\Snappy\ServiceProvider::class
5. 將 Facades 添加到 config/app.php 中的 aliases 數(shù)組中
'PDF' => Barryvdh\Snappy\Facades\SnappyPdf::class, 'SnappyImage' => Barryvdh\Snappy\Facades\SnappyImage::class,
6. 執(zhí)行生成配置文件
php artisan vendor:publish --provider="Barryvdh\Snappy\ServiceProvider"
可以看到默認(rèn)的配置文件為 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' => [], ], ];
注意,這里有個(gè)坑,默認(rèn) binary 配置為 /usr/local/bin/wkhtmltopdf 和 /usr/local/bin/wkhtmltoimage,在第一次使用的時(shí)候,會(huì)報(bào)錯(cuò) /usr/local/bin/wkhtmltopdf不存在,這是因?yàn)樵?linux 系統(tǒng)下,wkhtmltopdf 和 wkhtmltoimage 的真實(shí)路徑和名稱為:/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 = '<html><head><meta charset="utf-8"></head><h2>訂單id</h2><h3>12346546</h3></html>'; $pdf = \PDF::loadHTML($html); return $pdf->inline();
很多博客里沒有提到,使用方法 1 中,會(huì)報(bào)這樣的錯(cuò):
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'.
執(zhí)行:
sudo apt-get update sudo apt install libssl1.0-dev
修復(fù)完成,導(dǎo)出 welcome 頁面。
如果使用 save () 方法保存,默認(rèn)保存到 /public 文件夾下,并且如果文件名相同的情況下,第二次保存會(huì)提示文件已經(jīng)存在。
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Laravel-snappy怎么導(dǎo)出PDF”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!
本文標(biāo)題:Laravel-snappy怎么導(dǎo)出PDF
瀏覽地址:http://jinyejixie.com/article40/peodeo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、網(wǎng)站制作、網(wǎng)站維護(hù)、網(wǎng)站設(shè)計(jì)、微信公眾號(hào)、服務(wù)器托管
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)