首頁 > php框架 > Laravel > 主體

一文講解Laravel-snappy如何導出PDF

藏色散人
發布: 2021-09-01 08:53:23
轉載
2972 人瀏覽過

以下由Laravel教學專欄為大家介紹如何使用Laravel-snappy匯出PDF,希望對需要的朋友有幫助!

前言

論壇裡有很多關於如何使用Laravel-snappy 的文章,但是很多都停留在安裝、基本範例,對於實際的應用既沒有說明用法,也沒有解答一些問題和疑惑,因此在此整理一下,權當做個記錄吧。

安裝

以ubuntu 為例

1.執行安裝wkhtmltopdf:

composer require h4cc/wkhtmltopdf-amd64 0.12.x
composer require h4cc/wkhtmltoimage-amd64 0.12.x
登入後複製

顧名思義,分別安裝的是wkhtmltopdf 和wkhtmltoimage。

2. 複製wkhtmltopdf 到ubuntu 執行指令的目錄中

sudo cp vendor/h4cc/wkhtmltoimage-amd64/bin/wkhtmltoimage-amd64 /usr/local/bin/
sudo cp vendor/h4cc/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。

一文講解Laravel-snappy如何導出PDF

因此,需要把設定資訊修改為:

'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 = &#39;<html><head><meta charset="utf-8"></head><h1>订单id</h1><h2>12346546</h2></html>&#39;;
$pdf = \PDF::loadHTML($html);
return $pdf->inline();
登入後複製

很多部落格裡沒有提到,使用方法1中,會報這樣的錯誤:

The exit status code &#39;1&#39; 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 &#39;/tmp/knp_snappy612c3edcdfc855.21787864.html&#39; &#39;/tmp/knp_snappy612c3edcdfce49.80482557.pdf&#39;.
登入後複製

執行:

sudo apt-get update
sudo apt install libssl1.0-dev
登入後複製

修復完成,匯出welcome 頁面。

如果使用 save () 方法儲存,預設儲存到 /public 資料夾下,如果檔案名稱相同的情況下,第二次儲存會提示檔案已經存在。

以上是一文講解Laravel-snappy如何導出PDF的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
pdf
來源:learnku.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板