Skip to content

Latest commit

 

History

History
151 lines (105 loc) · 4.28 KB

readme.md

File metadata and controls

151 lines (105 loc) · 4.28 KB

WeasyPrint PDF Wrapper for Laravel

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 orientation and paper size

\WeasyPrint::loadHTML($html)->setPaper('a4')->setOrientation('landscape')->setOption('margin-bottom', 0)->save('myfile.pdf')

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