fruitcake/laravel-weasyprint

WeasyPrint for Laravel

Maintainers

👁 barryvdh

Package info

github.com/fruitcake/laravel-weasyprint

pkg:composer/fruitcake/laravel-weasyprint

Fund package maintenance!

barryvdh

fruitcake.nl

Statistics

Installs: 44 343

Dependents: 0

Suggesters: 0

Stars: 21

Open Issues: 0

v0.3.3 2026-06-17 17:42 UTC

Requires

Requires (Dev)

Suggests

None

Provides

None

Conflicts

None

Replaces

None

MIT 5185b37ff3c2659d2c5643ac29bcf722a8651eb0

  • Fruitcake
  • Barry vd. Heuvel <barry.woop@fruitcake.nl>

pdflaravelWeasyPrint


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