使用Laravel-snappy匯出PDF

她來聽我的演唱會發表於2021-08-30

前言

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

附:

參考文章:
Laravel 使用 laravel-snappy 包 實現 HTML 轉 PDF 和 PNG
laravel使用snappy生成pdf問題及過程(本地環境)
QSslSocket: cannot resolve CRYPTO_num_locks的解決方法
barryvdhlaravel-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 = '<html><head><meta charset="utf-8"></head><h1>訂單id</h1><h2>12346546</h2></html>';
$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'.

可以參考這篇文章:
QSslSocket: cannot resolve CRYPTO_num_locks的解決方法
執行:

sudo apt-get update
sudo apt install libssl1.0-dev

修復完成,匯出welcome頁面。

如果使用save()方法儲存,預設儲存到/public資料夾下,並且如果檔名相同的情況下,第二次儲存會提示檔案已經存在。更多用法可以參考這篇文章:

laravel使用snappy生成pdf問題及過程(本地環境)

自定義頁面匯出資料

簡單的用法可以直接使用loadHTML(),但是如果比較複雜的頁面,並且需要美化頁面的情況下,如果還繼續使用loadHTML()就顯得很有問題,可以想象一下,成片的h5程式碼在php程式碼中,就顯得很繁雜,不夠優雅,就算要維護也相當困難。因此如果是這種需求的話,應該使用loadView()
控制器:

$logs = [
            ["name" => "李大", "city" => "上海"],
            ["name" => "阿鵬", "city" => "北京"]
        ];
        //請注意這裡的compact,如果不使用compact,需要另外寫js進行處理
        $pdf = PDF::loadView('admin.user_data', compact('logs'));
        return $pdf->download('billing_data.pdf');

admin.user_data.blade.php:

<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <title>資料匯出</title>
  <!-- 最新版本的 Bootstrap 核心 CSS 檔案 -->
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">

  <!-- 最新的 Bootstrap 核心 JavaScript 檔案 -->
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" crossorigin="anonymous"></script>
</head>
<body>
 <table class="table">
 <caption>基本的表格佈局</caption>
 <thead> <tr> <th>名稱</th>
 <th>城市</th>
 </tr> </thead> <tbody>  @foreach($logs as $log)
  <tr>
 <td>{{  $log["name"] }}</td>
 <td>{{  $log["city"] }}</td>
 </tr>
  @endforeach
  </tbody>
 </table></body>
</html>

匯出效果:

使用Laravel-snappy匯出PDF

相應的美化工作可以使用css控制。

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章