laravel安装PDF扩展laravel-snappy
首先得下载这2个文件,不然没法用,这个配置文件是在config/snappy.php中
下载
composer require barryvdh/laravel-snappy
发布
php artisan vendor:publish --provider="Barryvdh\Snappy\ServiceProvider"
官网:https://github.com/barryvdh/laravel-snappy 需要安装字体不然是乱码;
sudo apt-get install fonts-wqy-microhei
模板输出用这个:
<?php
namespace App\Http\Controllers;
use Encore\Admin\Controllers\AdminController;
use Illuminate\Http\Request;
use Validator;
use \DB;
use PDF;
use App;
use App\Models\Xjrw;
use App\Services\SensitiveWords;
class PdfController extends Controller{
public function index(Request $request){
$person = ["name" => "John", "age" => 30, "city" => "New York"];
// 加载 Blade 模板
$html = view('pdf.pdf1', compact('person'))->render();
// 生成 PDF
$pdf = PDF::loadHTML($html);
// 在浏览器中打开 PDF
return $pdf->inline('task_details.pdf');
}
}
在windwos中需要下载安,安装后,https://wkhtmltopdf.org/downloads.html 安装的目录不能带空格,配置文件如下
<?php
return [
'pdf' => [
'enabled' => true,
//'binary' => env('WKHTML_PDF_BINARY', '/usr/local/bin/wkhtmltopdf-amd64'), // linux 配置路径
'binary' => env('WKHTML_PDF_BINARY', 'D:\wk\wkhtmltopdf\bin\wkhtmltopdf.exe'), // windows 配置路径
'timeout' => false,
'options' => [],
'env' => [],
],
'image' => [
'enabled' => true,
//'binary' => env('WKHTML_IMG_BINARY', '/usr/local/bin/wkhtmltoimage-amd64'), // linux 配置路径
'binary' => env('WKHTML_IMG_BINARY', 'D:\wk\wkhtmltopdf\bin\wkhtmltoimage.exe'), // windows 配置路径
'timeout' => false,
'options' => [],
'env' => [],
],
];
最近访问时间:2025-08-30 20:57:14