The two days of the project need to convert HTML pages to PDF file for easy printing, I searched the internet a lot of information. Have tried laravel-dompdf and laravel-snappy two kinds of expansion pack, personal feeling laravel-snappy more useful.
First, using the Laravel-dompdf expansion pack
1. Install Expansion pack
We install it through composer.
require barryvdh/laravel-dompdf
2. Add serviceprovider to the providers array in config/app.php
Barryvdh\dompdf\serviceprovider::class,
3. Add facade to the aliases array in config/app.php
' PDF ' = Barryvdh\dompdf\facade::class,
4. Use
You can create an dompdf instance and load an HTML string, file, or view, and then use the stream () method to display the download in the browser, save () method to a file, or download () method.
$pdf = App::make(‘dompdf.wrapper‘);
$pdf->loadHTML(‘<h1>Test</h1>‘);
return $pdf->stream();
You can also use the façade (facade), using the first PDF before use;
$pdf = PDF::loadView(‘pdf.invoice‘, $data);
return $pdf->download(‘invoice.pdf‘);
You can also chain-operate
return PDF::loadFile(public_path().‘/myfile.html‘)->save(‘/path-to/my_stored_file.pdf‘)->stream(‘download.pdf‘);
You can change the direction (landscape the direction to landscape, generally use vertical, pay attention when using) and paper size, and hide or show errors (by default, display errors when debugging is turned on)
Pdf::loadhtml ($html)->setpaper (' A4 ', ' Landscape ')->setwarnings (false)->save (' Myfile.pdf ')
For some other basic usage and configuration please refer to documentation https://github.com/barryvdh/laravel-dompdf
5, solve the problem of Chinese garbled
Download a Chinese font that supports Unicode encoding. Example: Msyh.ttf (Microsoft Ya-Black)
It is recommended to create the Fonts folder under Storage, put the font in the Fonts folder, or you will get an error.
Introducing fonts in CSS
<style>
@font-face {
font-family: ‘msyh‘;
font-style: normal;
font-weight: normal;
src: url({{ storage_path(‘your_path/msyh.ttf‘) }}) format(‘truetype‘);
}
body {
font-family: msyh, DejaVu Sans,sans-serif;
}
</style>
Note: The pro-test is only Unicode-encoded in Chinese to display properly, which is why I think this PDF extension is not very useful.
Second, the use of Laravel-snappy expansion package
1. Install dependent software
Laravel-snappy expansion pack requires WKHTMLTOPDFF support, so install Wkhtmltopdf first
Method One: Download the Wkhtmltopdf installation package
Wkhtmltopdfhttp://www.softpedia.com/get/office-tools/pdf/wkhtmltopdf.shtml
Windows can download the installation directly, and note that installing to the place you know, this path is useful later.
My installation is under the G:WK directory
Wkhtmltoimage and wkhtmltopdf two dependent software in bin directory
When the Linux download is complete, move the file to/usr/local/bin
Method Two: Install by composer
Take the Linux system as an example
32-bit:
$ composer require h4cc / wkhtmltopdf-i386 0.12.x
$ composer require h4cc / wkhtmltoimage-i386 0.12.x,
64-bit:
$ composer require h4cc/wkhtmltopdf-amd64 0.12.x
$ composer require h4cc/wkhtmltoimage-amd64 0.12.x
(unname -a command to view system digits)
Cp vendor/h4cc/wkhtmltoimage-amd64/bin/wkhtmltoimage-amd64 /usr/local/bin/
Cp vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64 /usr/local/bin/
And make it executable:
Chmod +x /usr/local/bin/wkhtmltoimage-amd64
Chmod +x /usr/local/bin/wkhtmltopdf-amd64
The use of this third-party plugin under Linux requires several library support
apt-get install libXrender*
apt-get install libfontconfig*
1. Installing the Laravel-snappy expansion pack
Composer require barryvdh/laravel-snappy
2. Adding serviceprovider to the providers array in config/app.php
Barryvdh\snappy\serviceprovider::class,
3. Add facade to the aliases array in config/app.php
‘PDF‘ => Barryvdh\Snappy\Facades\SnappyPdf::class,
‘SnappyImage‘ => Barryvdh\Snappy\Facades\SnappyImage::class,
4. Generate Configuration file
PHP artisan vendor:publish--provider= "Barryvdh\snappy\serviceprovider"
This command generates a configuration file in the config/snappy.php
The specific configuration is as follows:
<?php
Return array(
‘pdf’ => array(
‘enabled‘ => true,
// ‘binary‘ => ‘/usr/local/bin/wkhtmltopdf’, //dependent software path, linux defaults in this directory
‘binary‘ => ‘G:\wk\wkhtmltopdf\bin\wkhtmltopdf’,
‘timeout‘ => false,
‘options‘ => array(),
‘env‘ => array(),
),
‘image’ => array(
‘enabled‘ => true,
// ‘binary‘ => ‘/usr/local/bin/wkhtmltoimage‘,
‘binary‘ => ‘G:\wk\wkhtmltopdf\bin\wkhtmltoimage’,
‘timeout‘ => false,
‘options‘ => array(),
‘env‘ => array(),
),
);
5. Use (similar to using dompdf)
First introduce
use PDF;
use SnappyImage;
Generate PDF files
You can use the façade (facade) to load an HTML string, file, or view, and then use the stream () method to display the download in the browser, save () method to a file, or download () method.
$pdf = PDF::loadView(‘pdf.invoice‘, $data);
return $pdf->download(‘invoice.pdf‘)
You can also chain-operate
return Pdf::loadfile (Public_path (). ' /myfile.html ')->save ('/path-to/my_stored_file.pdf ')->stream (' download.pdf ');
You can change the direction (landscape the direction to landscape, generally use vertical, pay attention when using) and paper size, and hide or show errors (by default, display errors when debugging is turned on)
Pdf::loadhtml ($html)->setpaper (' A4 ', ' Landscape ')->setwarnings (false)->save (' Myfile.pdf ')
Create a picture
$pdf = SnappyImage::loadView(‘pdf.invoice‘, $data);
return $pdf->download(‘invoice.image‘);
6, solve the English garbled problem (Linux)
You can add an example: Arial or other Chinese fonts to the /usr/share/fonts/ .
Very happy to be able to share my learning experience to everyone (? >?<?)
PDF expansion Pack--laravel-dompdf and laravel-snappy used in Laravel