fruitcake / laravel-weasyprint
WeasyPrint for Laravel
Requires
- php: >=8.1
- illuminate/filesystem: ^9|^10|^11
- illuminate/support: ^9|^10|^11
- pontedilana/php-weasyprint: ^1.3
Requires (Dev)
- orchestra/testbench: ^7|^8|^9
- squizlabs/php_codesniffer: ^3.5
This package is auto-updated.
Last update: 2024-09-07 12:51:10 UTC
README
本包是 WeasyPrint 的 ServiceProvider:[https://github.com/pontedilana/php-weasyprint](https://github.com/pontedilana/php-weasyprint)。
本包基于 [https://github.com/barryvdh/laravel-snappy](https://github.com/barryvdh/laravel-snappy) 进行了大量修改,但使用 WeasyPrint 而不是 WKHTMLTOPDF。
WeasyPrint 安装
请参考以下设置:[https://doc.courtbouillon.org/weasyprint/stable/first_steps.html#installation](https://doc.courtbouillon.org/weasyprint/stable/first_steps.html#installation)
测试 WeasyPrint 安装
安装后,您应该可以从命令行/壳中运行 WeasyPrint。
weasyprint https://laravel.net.cn/docs laravel-docs.pdf
包安装
在您的 composer.json 中需要此包并更新 composer。
composer require fruitcake/laravel-weasyprint
配置
您可以发布配置文件
php artisan vendor:publish --provider="Fruitcake\WeasyPrint\WeasyPrintProvider"
用法
您可以创建一个新的 WeasyPrint 实例并加载一个 HTML 字符串、文件或视图名称。您可以将它保存到文件中,或内联(在浏览器中显示)或下载。
使用 App 容器
<?php namespace App\Http\Controllers; use Pontedilana\PhpWeasyPrint\Pdf; class PdfController extends Controller { public function __invoke(Pdf $weasyPrint) { //To file $html = '<h1>Bill</h1><p>You owe me money, dude.</p>'; $weasyPrint->generateFromHtml($html, '/tmp/bill-123.pdf'); $weasyPrint->generate('https://laravel.net.cn/docs/10.x', '/tmp/laravel-docs.pdf'); //Or output: return response( $weasyPrint->getOutputFromHtml($html), 200, array( 'Content-Type' => 'application/pdf', 'Content-Disposition' => 'attachment; filename="file.pdf"' ) ); } }
或者使用 Facade 来访问简单的辅助方法。
内联 PDF
$pdf = \WeasyPrint::loadHTML('<h1>Test</h1>'); return $pdf->inline();
或下载
$pdf = \WeasyPrint::loadView('pdf.invoice', $data); return $pdf->download('invoice.pdf');
您可以链式调用方法
return \WeasyPrint::loadFile('https://laravel.net.cn/docs')->inline('laravel.pdf');
您可以更改方向和纸张大小
\WeasyPrint::loadHTML($html)->setPaper('a4')->setOrientation('landscape')->setOption('margin-bottom', 0)->save('myfile.pdf')
如果您需要字符串格式的输出,您可以使用 output() 函数获取渲染的 PDF,然后您可以自己保存/输出它。
有关更多信息/设置,请参阅 [php-weasyprint](https://github.com/pontedilana/php-weasyprint)。
测试 - PDF 伪造
作为模拟的替代方案,您可以使用 WeasyPrint Facade 的 fake 方法。在执行测试代码后,使用伪造时将进行断言。
<?php namespace Tests\Feature; use Tests\TestCase; use PDF; class ExampleTest extends TestCase { public function testPrintOrderShipping() { PDF::fake(); // Perform order shipping... PDF::assertViewIs('view-pdf-order-shipping'); PDF::assertSee('Name'); } }
其他可用的断言
WeasyPrint::assertViewIs($value); WeasyPrint::assertViewHas($key, $value = null); WeasyPrint::assertViewHasAll(array $bindings); WeasyPrint::assertViewMissing($key); WeasyPrint::assertSee($value); WeasyPrint::assertSeeText($value); WeasyPrint::assertDontSee($value); WeasyPrint::assertDontSeeText($value); PDWeasyPrintF::assertFileNameIs($value);
许可证
此 Laravel 的 WeasyPrint 包装器是开源软件,许可证为 MIT 许可证。[https://open-source.org.cn/licenses/MIT](https://open-source.org.cn/licenses/MIT)