定制 fruitcake/laravel-weasyprint 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

fruitcake/laravel-weasyprint

最新稳定版本:v0.3.1

Composer 安装命令:

composer require fruitcake/laravel-weasyprint

包简介

WeasyPrint for Laravel

README 文档

README

Tests Packagist License Latest Stable Version Total Downloads Fruitcake

This package is a ServiceProvider for WeasyPrint: https://github.com/pontedilana/php-weasyprint.

This package is based heavily on https://github.com/barryvdh/laravel-snappy but uses WeasyPrint instead of WKHTMLTOPDF

WeasyPrint Installation

Follow the setup here: https://doc.courtbouillon.org/weasyprint/stable/first_steps.html#installation

Testing the WeasyPrint installation

After installing, you should be able to run WeasyPrint from the command line / shell.

weasyprint https://laravel.com/docs laravel-docs.pdf

Package Installation

Require this package in your composer.json and update composer.

composer require fruitcake/laravel-weasyprint

Configuration

You can publish the config file:

php artisan vendor:publish --provider="Fruitcake\WeasyPrint\WeasyPrintProvider"

Usage

You can create a new WeasyPrint instance and load an HTML string, file or view name. You can save it to a file, or inline (show in browser) or download.

Using the App container:

<?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.com/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"' ) ); } }

Or use the Facade to access easy helper methods.

Inline a PDF:

$pdf = \WeasyPrint::loadHTML('<h1>Test</h1>'); return $pdf->inline();

Or download:

$pdf = \WeasyPrint::loadView('pdf.invoice', $data); return $pdf->download('invoice.pdf');

You can chain the methods:

return \WeasyPrint::loadFile('https://laravel.com/docs')->inline('laravel.pdf');

You can change the footer, orientation and paper size using the @page in you stylesheets

@page { size: A4; margin: 1cm; @bottom-right{ content: "Page " counter(page) " of " counter(pages); } }
@page { size: A6 landscape; margin: 1cm; }

If you need the output as a string, you can get the rendered PDF with the output() function, so you can save/output it yourself.

See the php-weasyprint for more information/settings.

Testing - PDF fake

As an alternative to mocking, you may use the WeasyPrint facade's fake method. When using fakes, assertions are made after the code under test is executed:

<?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'); } }

Other available assertions:

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);

License

This WeasyPrint Wrapper for Laravel is open-sourced software licensed under the MIT license

统计信息

  • 总下载量: 37.96k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 20
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 20
  • Watchers: 3
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固