|
| 1 | +## WeasyPrint PDF Wrapper for Laravel |
| 2 | +[](https://github.com/fruitcake/laravel-weasyprint/actions) |
| 3 | +[](http://choosealicense.com/licenses/mit/) |
| 4 | +[](https://packagist.org/packages/fruitcake/laravel-weasyprint) |
| 5 | +[](https://packagist.org/packages/fruitcake/laravel-weasyprint) |
| 6 | +[](https://fruitcake.nl/) |
| 7 | + |
| 8 | +This package is a ServiceProvider for WeasyPrint: [https://github.com/pontedilana/php-weasyprint](https://github.com/pontedilana/php-weasyprint). |
| 9 | + |
| 10 | +This package is based heavily on https://github.com/barryvdh/laravel-snappy but uses WeasyPrint instead of WKHTMLTOPDF |
| 11 | + |
| 12 | +### WeasyPrint Installation |
| 13 | + |
| 14 | +Follow the setup here: https://doc.courtbouillon.org/weasyprint/stable/first_steps.html#installation |
| 15 | + |
| 16 | +### Testing the WeasyPrint installation |
| 17 | + |
| 18 | +After installing, you should be able to run WeasyPrint from the command line / shell. |
| 19 | + |
| 20 | +```shell |
| 21 | +weasyprint https://laravel.com/docs laravel-docs.pdf |
| 22 | +``` |
| 23 | + |
| 24 | +### Package Installation |
| 25 | + |
| 26 | +Require this package in your composer.json and update composer. |
| 27 | + |
| 28 | +```bash |
| 29 | +composer require fruitcake/laravel-weasyprint |
| 30 | +``` |
| 31 | + |
| 32 | +### Configuration |
| 33 | + |
| 34 | +You can publish the config file: |
| 35 | + |
| 36 | +```bash |
| 37 | +php artisan vendor:publish --provider="Fruitcake\WeasyPrint\ServiceProvider" |
| 38 | +``` |
| 39 | + |
| 40 | +### Usage |
| 41 | + |
| 42 | +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. |
| 43 | + |
| 44 | +Using the App container: |
| 45 | + |
| 46 | +```php |
| 47 | +$weasy = App::make(Fruitcake\WeasyPrint\WeasyPrint::class); |
| 48 | +//To file |
| 49 | +$html = '<h1>Bill</h1><p>You owe me money, dude.</p>'; |
| 50 | +$weasy->generateFromHtml($html, '/tmp/bill-123.pdf'); |
| 51 | +$weasy->generate('http://www.github.com', '/tmp/github.pdf'); |
| 52 | +//Or output: |
| 53 | +return new Response( |
| 54 | + $weasy->getOutputFromHtml($html), |
| 55 | + 200, |
| 56 | + array( |
| 57 | + 'Content-Type' => 'application/pdf', |
| 58 | + 'Content-Disposition' => 'attachment; filename="file.pdf"' |
| 59 | + ) |
| 60 | +); |
| 61 | +``` |
| 62 | + |
| 63 | +Using the wrapper: |
| 64 | + |
| 65 | +```php |
| 66 | +$pdf = App::make('weasyprint.pdf.wrapper'); |
| 67 | +$pdf->loadHTML('<h1>Test</h1>'); |
| 68 | +return $pdf->inline(); |
| 69 | +``` |
| 70 | + |
| 71 | +Or use the facade: |
| 72 | + |
| 73 | +```php |
| 74 | +$pdf = WeasyPrint::loadView('pdf.invoice', $data); |
| 75 | +return $pdf->download('invoice.pdf'); |
| 76 | +``` |
| 77 | + |
| 78 | +You can chain the methods: |
| 79 | + |
| 80 | +```php |
| 81 | +return WeasyPrint::loadFile('http://www.github.com')->inline('github.pdf'); |
| 82 | +``` |
| 83 | + |
| 84 | +You can change the orientation and paper size |
| 85 | + |
| 86 | +```php |
| 87 | +WeasyPrint::loadHTML($html)->setPaper('a4')->setOrientation('landscape')->setOption('margin-bottom', 0)->save('myfile.pdf') |
| 88 | +``` |
| 89 | + |
| 90 | +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. |
| 91 | + |
| 92 | +See the [php-weasyprint](https://github.com/pontedilana/php-weasyprint) for more information/settings. |
| 93 | + |
| 94 | +### Testing - PDF fake |
| 95 | + |
| 96 | +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: |
| 97 | + |
| 98 | +```php |
| 99 | +<?php |
| 100 | + |
| 101 | +namespace Tests\Feature; |
| 102 | + |
| 103 | +use Tests\TestCase; |
| 104 | +use PDF; |
| 105 | + |
| 106 | +class ExampleTest extends TestCase |
| 107 | +{ |
| 108 | + public function testPrintOrderShipping() |
| 109 | + { |
| 110 | + PDF::fake(); |
| 111 | + |
| 112 | + // Perform order shipping... |
| 113 | + |
| 114 | + PDF::assertViewIs('view-pdf-order-shipping'); |
| 115 | + PDF::assertSee('Name'); |
| 116 | + } |
| 117 | +} |
| 118 | +``` |
| 119 | + |
| 120 | +#### Other available assertions: |
| 121 | + |
| 122 | +```php |
| 123 | +WeasyPrint::assertViewIs($value); |
| 124 | +WeasyPrint::assertViewHas($key, $value = null); |
| 125 | +WeasyPrint::assertViewHasAll(array $bindings); |
| 126 | +WeasyPrint::assertViewMissing($key); |
| 127 | +WeasyPrint::assertSee($value); |
| 128 | +WeasyPrint::assertSeeText($value); |
| 129 | +WeasyPrint::assertDontSee($value); |
| 130 | +WeasyPrint::assertDontSeeText($value); |
| 131 | +PDWeasyPrintF::assertFileNameIs($value); |
| 132 | +``` |
| 133 | + |
| 134 | +### License |
| 135 | + |
| 136 | +This WeasyPrint Wrapper for Laravel is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT) |
0 commit comments