laravel-snappy基于wkhtmltopdf工具,可以比较完美的将web页面转成pdf格式。
顺便把laravel及laravel-snappy安装过程记录下
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/ composer create-project laravel/laravel example-app composer require barryvdh/laravel-snappy php artisan vendor:publish --provider="Barryvdh\Snappy\ServiceProvider"
config/app.php 'aliases'项加上
'PDF' => Barryvdh\Snappy\Facades\SnappyPdf::class, 'SnappyImage' => Barryvdh\Snappy\Facades\SnappyImage::class,
使用
$pdf = PDF::loadView('pdf.invoice', $data); $pdf->setPaper('a4')->setOption('encoding', 'utf-8')->setOption('margin-left', 0)->setOption('margin-right', 0)
->setOption('javascript-delay', 10000)->setOption('no-stop-slow-scripts', true); $pdf->save($path) //return $pdf->download('invoice.pdf');
用于调整生成pdf格式的css属性
.keep-together { page-break-inside: avoid; } .break-before { page-break-before: always; } .break-after { page-break-after: always; }
使用laravel的command定时产出pdf
php artisan make:command pdf
<?php namespace App\Console\Commands; use Illuminate\Console\Command; use App\Services\PdfService; class Pdf extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'pdf {name}'; /** * The console command description. * * @var string */ protected $description = 'pdf'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function handle() { $name = $this->argument('name'); $service = new PdfService(); $service->pdf($name); $this->info('The command was successful!'); } }
调试
方法1 use Illuminate\Support\Facades\Artisan; Route::post('/pdf/{name}', function ($name) { $exitCode = Artisan::call('pdf', [ 'name' => $name ]); }); 方法2 php artisan pdf 'aa'
标签:laravel,name,setOption,snappy,composer,pdf From: https://www.cnblogs.com/caroline2016/p/17391607.html