Lajur tutorial Laravel akan memperkenalkan kepada anda cara menggunakan Laravel-snappy untuk mengeksport PDF, saya harap ia dapat membantu rakan yang memerlukan.
Prakata
Terdapat banyak artikel tentang cara menggunakan Laravel-snappy dalam forum, tetapi kebanyakannya hanya menumpukan pada pemasangan dan contoh asas, dan tidak praktikal untuk aplikasi sebenar Tiada penjelasan tentang penggunaan, dan tiada jawapan kepada beberapa soalan dan keraguan, jadi saya akan menyelesaikannya di sini dan menganggapnya sebagai rekod.
Pemasangan
Ambil ubuntu sebagai contoh
1. Jalankan pemasangan wkhtmltopdf:
composer require h4cc/wkhtmltopdf-amd64 0.12.x composer require h4cc/wkhtmltoimage-amd64 0.12.x
Seperti namanya. , pemasangan ialah wkhtmltopdf dan wkhtmltoimage.
2. Salin wkhtmltopdf ke direktori perintah boleh laku 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 Pasang laravel-snappy
composer require barryvdh/laravel-snappy
4 🎜>
Barryvdh\Snappy\ServiceProvider::class
'PDF' => Barryvdh\Snappy\Facades\SnappyPdf::class, 'SnappyImage' => Barryvdh\Snappy\Facades\SnappyImage::class,
php artisan vendor:publish --provider="Barryvdh\Snappy\ServiceProvider"
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' => [], ], ];
'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();
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
Atas ialah kandungan terperinci Artikel yang menerangkan cara mengeksport PDF dengan Laravel-snappy. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!