Home > Article > PHP Framework > An article explaining how to export PDF with Laravel-snappy
The following tutorial column from Laravel will introduce to you how to use Laravel-snappy to export PDF. I hope it will be helpful to friends in need!
Preface
There are many articles on how to use Laravel-snappy in the forum, but many of them only focus on installation and basic examples. There is no explanation of usage, and no answers to some questions and doubts, so I will sort it out here and treat it as a record.
Installation
Take ubuntu as an example
1. Execute and install wkhtmltopdf:
composer require h4cc/wkhtmltopdf-amd64 0.12.x composer require h4cc/wkhtmltoimage-amd64 0.12.x
As the name suggests, wkhtmltopdf and wkhtmltopdf are installed respectively. wkhtmltoimage.
2. Copy wkhtmltopdf to the directory of the ubuntu executable command
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. Install laravel-snappy
composer require barryvdh/laravel-snappy
4. Add ServiceProvider to config/app.php In the providers array
Barryvdh\Snappy\ServiceProvider::class
5. Add Facades to the aliases array in config/app.php
'PDF' => Barryvdh\Snappy\Facades\SnappyPdf::class, 'SnappyImage' => Barryvdh\Snappy\Facades\SnappyImage::class,
6. Execute the generated configuration file
php artisan vendor:publish --provider="Barryvdh\Snappy\ServiceProvider"
You can see The default configuration file is 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' => [], ], ];
Note that there is a pit here. The default binary configuration is /usr/local/bin/wkhtmltopdf and /usr/local/bin/wkhtmltoimage. In the first When used for the first time, an error will be reported that /usr/local/bin/wkhtmltopdf does not exist. This is because under the Linux system, the real paths and names of wkhtmltopdf and wkhtmltoimage are: /usr/local/bin/wkhtmltopdf-amd64 and /usr/ local/bin/wkhtmltoimage-amd64.
Therefore, the configuration information needs to be modified to:
'pdf' => [ ... 'binary' => env('WKHTML_PDF_BINARY', '/usr/local/bin/wkhtmltopdf-amd64'), ... ], 'image' => [ ... 'binary' => env('WKHTML_IMG_BINARY', '/usr/local/bin/wkhtmltoimage-amd64'), ... ],
Start using
//使用方法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();
Many blogs have not mentioned it, usage method 1 , the following error will be reported:
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'.
Execution:
sudo apt-get update sudo apt install libssl1.0-dev
The repair is completed and the welcome page is exported.
If you use the save () method to save, it will be saved to the /public folder by default. If the file name is the same, the second save will prompt that the file already exists.
The above is the detailed content of An article explaining how to export PDF with Laravel-snappy. For more information, please follow other related articles on the PHP Chinese website!