tjgazel /
为 laravel 5.* 扩展的 FPDF 库
v1.1.2
2021-04-20 12:44 UTC
Requires
- php: >=5.4.0
This package is auto-updated.
Last update: 2024-09-20 19:50:36 UTC
README
安装
1) Composer
composer require tjgazel/laravel-fpdf
2) 可选: Laravel 5.4 及以下版本
将 TJGazel\Toastr\ToastrServiceProvider::class
添加到 config/app.php
文件中的 providers
数组。
将 'Toastr' => TJGazel\Toastr\Facades\Toastr::class
添加到 config/app.php
文件中的 aliases
数组。
// config/app.php
'providers' => [
// ...
TJGazel\LaraFpdf\LaraFpdfServiceProvider::class,
],
'aliases' => [
// ...
'LaraFpdf' => TGazel\LaraFpdf\Facades\LaraFpdf::class,
],
用法
(方法1)门面
use App\Http\Controllers\Controller; use TGazel\LaraFpdf\Facades\LaraFpdf; class MyController extends Controller { public function index() { LaraFpdf::AddPage(); LaraFpdf::SetFont('Courier', 'B', 18); LaraFpdf::Cell(50, 25, 'Hello World!'); LaraFpdf::Output('I', "MyPdf.pdf", true); exit; } }
(方法2)扩展类
创建你的PDF类
namespace App\Pdf; use TJGazel\LaraFpdf\LaraFpdf; class MyPdf extends LaraFpdf { private $data public function __construct($data) { $this->data = $data; parent::__construct('P', 'mm', 'A4'); $this->SetA4(); $this->SetTitle('My pdf title', true); $this->SetAuthor('TJGazel', true); $this->AddPage('L'); $this->Body(); } public function Header() { // fixed all pages } public function Body() { $this->SetFont('Arial', 'B', '24'); $this->Cell(50, 25, $this->data->title); $this->SetFont('Arial', '', '14'); $this->Cell(50, 25, $this->data->content); } public function Footer() { // fixed all pages } }
在你的控制器中
use App\Http\Controllers\Controller; use App\Pdf\MyPdf; class MyController extends Controller { public function index() { $data = MyModel::all(); $myPdf = new MyPdf($data); $myPdf->Output('I', "MyPdf.pdf", true); exit; } }