Skip to content

Commit 8b87e33

Browse files
committed
Initial commit
0 parents  commit 8b87e33

20 files changed

+1282
-0
lines changed

.editorconfig

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
; This file is for unifying the coding style for different editors and IDEs.
2+
; More information at http://editorconfig.org
3+
4+
root = true
5+
6+
[*]
7+
charset = utf-8
8+
indent_size = 4
9+
indent_style = space
10+
end_of_line = lf
11+
insert_final_newline = true
12+
trim_trailing_whitespace = true
13+
14+
[*.md]
15+
trim_trailing_whitespace = false

.gitattributes

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/.github export-ignore
2+
/tests export-ignore
3+
.gitattributes export-ignore
4+
.gitignore export-ignore
5+
phpunit.xml.dist export-ignore

.github/FUNDING.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# These are supported funding model platforms
2+
3+
github: barryvdh
4+
custom: ['https://fruitcake.nl']
5+

.github/stale.yml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Number of days of inactivity before an issue becomes stale
2+
daysUntilStale: 60
3+
# Number of days of inactivity before a stale issue is closed
4+
daysUntilClose: 7
5+
# Issues with these labels will never be considered stale
6+
exemptLabels:
7+
- bug
8+
- enhancement
9+
# Label to use when marking an issue as stale
10+
staleLabel: stale
11+
# Comment to post when marking an issue as stale. Set to `false` to disable
12+
markComment: >
13+
This issue has been automatically marked as stale because it has not had
14+
recent activity. It will be closed if no further activity occurs.
15+
16+
Any issues with PDF rendering itself that are not directly related to this package,
17+
should be reported onhttps://github.com/Kozea/WeasyPrint instead.
18+
When having doubts, please try to reproduce the issue with just WeasyPrint.
19+
20+
If you believe this is an actual issue with the latest version of laravel-weasyprint,
21+
please reply to this issue so we can investigate further.
22+
23+
Thank you for your contribution! Apologies for any delayed response on our side.
24+
# Comment to post when closing a stale issue. Set to `false` to disable
25+
closeComment: false
26+
# Limit to only `issues` or `pulls`
27+
only: issues

.github/workflows/run-tests.yml

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
name: Tests
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
branches:
9+
- "*"
10+
schedule:
11+
- cron: '0 0 * * *'
12+
13+
jobs:
14+
php-tests:
15+
runs-on: ubuntu-20.04
16+
timeout-minutes: 15
17+
env:
18+
COMPOSER_NO_INTERACTION: 1
19+
20+
strategy:
21+
matrix:
22+
php: [8.2, 8.1, 8.0]
23+
laravel: [10.*, 9.*]
24+
dependency-version: [prefer-lowest, prefer-stable]
25+
exclude:
26+
- laravel: 10.*
27+
php: 8.0
28+
29+
name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }}
30+
31+
steps:
32+
- name: Checkout code
33+
uses: actions/checkout@v2
34+
35+
- name: Install wkthtmltopdf
36+
run: |
37+
sudo apt-get install -y xfonts-base xfonts-75dpi
38+
wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.bionic_amd64.deb
39+
sudo dpkg -i wkhtmltox_0.12.6-1.bionic_amd64.deb
40+
41+
- name: Setup PHP
42+
uses: shivammathur/setup-php@v2
43+
with:
44+
php-version: ${{ matrix.php }}
45+
coverage: none
46+
extensions: mbstring
47+
48+
- name: Install dependencies
49+
run: |
50+
composer require "laravel/framework:${{ matrix.laravel }}" --no-update --no-progress
51+
composer update --${{ matrix.dependency-version }} --prefer-dist --no-progress
52+
53+
- name: Execute Unit Tests
54+
run: composer test

.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/vendor
2+
composer.phar
3+
composer.lock
4+
.DS_Store
5+
.phpunit.result.cache
6+
.idea
7+
.phpunit.cache/

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License
2+
3+
Copyright (c) 2023
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

composer.json

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"name": "fruitcake/laravel-weasyprint",
3+
"description": "WeasyPrint for Laravel",
4+
"keywords": ["laravel", "weasyprint", "pdf"],
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Fruitcake",
9+
"homepage": "https://fruitcake.nl"
10+
},
11+
{
12+
"name": "Barry vd. Heuvel",
13+
"email": "barry@fruitcake.nl"
14+
}
15+
],
16+
"require": {
17+
"php": ">=8.1",
18+
"illuminate/support": "^9|^10",
19+
"illuminate/filesystem": "^9|^10",
20+
"pontedilana/php-weasyprint": "^1.2"
21+
},
22+
"require-dev": {
23+
"orchestra/testbench": "^7|^8",
24+
"squizlabs/php_codesniffer": "^3.5"
25+
},
26+
"autoload": {
27+
"psr-4": {
28+
"Fruitcake\\WeasyPrint\\": "src/"
29+
}
30+
},
31+
"autoload-dev": {
32+
"psr-4": {
33+
"Fruitcake\\WeasyPrint\\Tests\\": "tests/"
34+
}
35+
},
36+
"extra": {
37+
"branch-alias": {
38+
"dev-master": "0.1-dev"
39+
},
40+
"laravel": {
41+
"providers": [
42+
"Fruitcake\\WeasyPrint\\WeasyPrintProvider"
43+
],
44+
"aliases": {
45+
"WeasyPrint": "Fruitcake\\WeasyPrint\\WeasyPrint"
46+
}
47+
}
48+
},
49+
"scripts": {
50+
"actions": "composer test && composer check-style",
51+
"test": "phpunit",
52+
"check-style": "phpcs -p --standard=PSR12 --exclude=Generic.Files.LineLength --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src tests",
53+
"fix-style": "phpcbf -p --standard=PSR12 --exclude=Generic.Files.LineLength --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src tests"
54+
},
55+
"minimum-stability": "dev",
56+
"prefer-stable": true
57+
}

config/.gitkeep

Whitespace-only changes.

config/weasyprint.php

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
return [
4+
5+
/*
6+
|--------------------------------------------------------------------------
7+
| WeasyPrint Configuration
8+
|--------------------------------------------------------------------------
9+
|
10+
| This option contains settings for PDF generation.
11+
|
12+
| Enabled:
13+
|
14+
| Whether to load PDF / Image generation.
15+
|
16+
| Binary:
17+
|
18+
| The file path of the WeasyPrint executable.
19+
|
20+
| Timout:
21+
|
22+
| The amount of time to wait (in seconds) before PDF / Image generation is stopped.
23+
| Setting this to false disables the timeout (unlimited processing time).
24+
|
25+
| Options:
26+
|
27+
| The WeasyPrint command options.
28+
|
29+
| Env:
30+
|
31+
| The environment variables to set while running the weasyprint process.
32+
|
33+
*/
34+
35+
'pdf' => [
36+
'binary' => env('WEASYPRINT_BINARY', '/usr/local/bin/weasyprint'),
37+
'timeout' => false,
38+
'options' => [],
39+
'env' => [],
40+
],
41+
];

phpunit.xml.dist

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnError="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
3+
<coverage/>
4+
<testsuites>
5+
<testsuite name="Test Suite">
6+
<directory>./tests/</directory>
7+
</testsuite>
8+
</testsuites>
9+
<source>
10+
<include>
11+
<directory>./src</directory>
12+
</include>
13+
</source>
14+
</phpunit>

readme.md

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
## WeasyPrint PDF Wrapper for Laravel
2+
[![Tests](https://github.com/fruitcake/laravel-weasyprint/workflows/Tests/badge.svg)](https://github.com/fruitcake/laravel-weasyprint/actions)
3+
[![Packagist License](https://poser.pugx.orgfruitcake/laravel-weasyprint/license.png)](http://choosealicense.com/licenses/mit/)
4+
[![Latest Stable Version](https://poser.pugx.org/fruitcake/laravel-weasyprint/version.png)](https://packagist.org/packages/fruitcake/laravel-weasyprint)
5+
[![Total Downloads](https://poser.pugx.org/fruitcake/laravel-weasyprint/d/total.png)](https://packagist.org/packages/fruitcake/laravel-weasyprint)
6+
[![Fruitcake](https://img.shields.io/badge/Powered%20By-Fruitcake-b2bc35.svg)](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

Comments
 (0)