-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
1,253 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.github/ export-ignore | ||
tests/ export-ignore | ||
.gitattributes export-ignore | ||
.gitignore export-ignore | ||
.php-cs-fixer-dist.php export-ignore | ||
phpunit.xml.dist export-ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
name: "CI Tests" | ||
|
||
on: | ||
pull_request: | ||
push: | ||
|
||
jobs: | ||
php-74-symfony-44: | ||
name: PHP 7.4 / Symfony 4.4 | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: "Checkout" | ||
uses: "actions/checkout@v2" | ||
with: | ||
fetch-depth: 2 | ||
|
||
- name: "Install PHP" | ||
uses: "shivammathur/setup-php@v2" | ||
with: | ||
php-version: "7.4" | ||
|
||
- name: "Cache composer packages" | ||
uses: "actions/cache@v2" | ||
with: | ||
path: "~/.composer/cache" | ||
key: "php-7.4-composer-locked-${{ hashFiles('composer.lock') }}" | ||
restore-keys: "php-7.4-composer-locked-" | ||
|
||
- name: "Install Symfony 4.4" | ||
run: "composer require symfony/symfony:4.4.* --no-update" | ||
|
||
- name: "Install dependencies with composer" | ||
run: "composer update --no-interaction" | ||
|
||
- name: "Run PHPUnit Tests" | ||
run: "composer test" | ||
|
||
php-74-symfony53: | ||
name: PHP 7.4 / Symfony 5.3 | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: "Checkout" | ||
uses: "actions/checkout@v2" | ||
with: | ||
fetch-depth: 2 | ||
|
||
- name: "Install PHP 7.4" | ||
uses: "shivammathur/setup-php@v2" | ||
with: | ||
php-version: "7.4" | ||
|
||
- name: "Cache composer packages" | ||
uses: "actions/cache@v2" | ||
with: | ||
path: "~/.composer/cache" | ||
key: "php-7.4-composer-locked-${{ hashFiles('composer.lock') }}" | ||
restore-keys: "php-7.4-composer-locked-" | ||
|
||
- name: "Install Symfony 5.3" | ||
run: "composer require symfony/symfony:5.3.* --no-update" | ||
|
||
- name: "Install dependencies with composer" | ||
run: "composer update --no-interaction" | ||
|
||
- name: "Run PHPUnit Tests" | ||
run: "composer test" | ||
|
||
php-80-symfony44: | ||
name: PHP 8.0 / Symfony 4.4 | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: "Checkout" | ||
uses: "actions/checkout@v2" | ||
with: | ||
fetch-depth: 2 | ||
|
||
- name: "Install PHP 8" | ||
uses: "shivammathur/setup-php@v2" | ||
with: | ||
php-version: "8" | ||
|
||
- name: "Cache composer packages" | ||
uses: "actions/cache@v2" | ||
with: | ||
path: "~/.composer/cache" | ||
key: "php-8.0-composer-locked-${{ hashFiles('composer.lock') }}" | ||
restore-keys: "php-8.0-composer-locked-" | ||
|
||
- name: "Install Symfony 4.4" | ||
run: "composer require symfony/symfony:4.4.* --no-update" | ||
|
||
- name: "Install dependencies with composer" | ||
run: "composer update --no-interaction" | ||
|
||
- name: "Run PHPUnit Tests" | ||
run: "composer test" | ||
php-80-symfony53: | ||
name: PHP 8.0 / Symfony 5.3 | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: "Checkout" | ||
uses: "actions/checkout@v2" | ||
with: | ||
fetch-depth: 2 | ||
|
||
- name: "Install PHP 8" | ||
uses: "shivammathur/setup-php@v2" | ||
with: | ||
php-version: "8" | ||
|
||
- name: "Cache composer packages" | ||
uses: "actions/cache@v2" | ||
with: | ||
path: "~/.composer/cache" | ||
key: "php-8.0-composer-locked-${{ hashFiles('composer.lock') }}" | ||
restore-keys: "php-8.0-composer-locked-" | ||
|
||
- name: "Install Symfony 5.3" | ||
run: "composer require symfony/symfony:5.3.* --no-update" | ||
|
||
- name: "Install dependencies with composer" | ||
run: "composer update --no-interaction" | ||
|
||
- name: "Run PHPUnit Tests" | ||
run: "composer test" | ||
|
||
php8: | ||
name: PHP 8 / Symfony 5.4@dev | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: "Checkout" | ||
uses: "actions/checkout@v2" | ||
with: | ||
fetch-depth: 2 | ||
|
||
- name: "Install PHP 8" | ||
uses: "shivammathur/setup-php@v2" | ||
with: | ||
php-version: "8" | ||
|
||
- name: "Cache composer packages" | ||
uses: "actions/cache@v2" | ||
with: | ||
path: "~/.composer/cache" | ||
key: "php-8-composer-locked-${{ hashFiles('composer.lock') }}" | ||
restore-keys: "php-8-composer-locked-" | ||
|
||
- name: "Install Symfony 5.4" | ||
run: "composer require symfony/symfony:5.4.*@dev --no-update" | ||
|
||
- name: "Install dependencies with composer" | ||
run: "composer update --no-interaction" | ||
|
||
- name: "Run PHPUnit Tests" | ||
run: "composer test" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/vendor | ||
/composer.lock | ||
/.php-cs-fixer.cache | ||
.phpunit.result.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
$finder = (new PhpCsFixer\Finder()) | ||
->in(__DIR__ . '/src') | ||
->in(__DIR__ . '/tests') | ||
; | ||
|
||
return (new PhpCsFixer\Config()) | ||
->setRiskyAllowed(true) | ||
->setRules([ | ||
'@Symfony' => true, | ||
'@Symfony:risky' => true, | ||
'array_syntax' => ['syntax' => 'short'], | ||
'concat_space' => ['spacing' => 'one'], | ||
'cast_spaces' => ['space' => 'none'], | ||
'native_function_invocation' => false, | ||
'no_superfluous_phpdoc_tags' => ['allow_mixed' => true], | ||
]) | ||
->setFinder($finder) | ||
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Changelog | ||
All notable changes to this project will be documented in this file. | ||
|
||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), | ||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). | ||
|
||
## [Unreleased] | ||
|
||
## [1.0.0] - 2021-07-17 | ||
### Added | ||
- First public release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
# WeasyPrintBundle | ||
[PhpWeasyPrint](https://github.com/pontedilana/php-weasyprint) is a PHP (7.4+) wrapper for [WeasyPrint](https://weasyprint.org/) PDF generator. | ||
It allows you to generate PDF files from HTML string or URL. | ||
|
||
The WeasyPrintBundle provides a simple integration for your Symfony project. | ||
|
||
This bundle is massively inspired by [KnpLabs/KnpSnappyBundle](https://github.com/KnpLabs/KnpSnappyBundle), of which it aims to be a one-to-one substitute | ||
|
||
## Installation | ||
|
||
With [composer](https://getcomposer.org), require: | ||
|
||
`composer require pontedilana/weasyprint-bundle` | ||
|
||
Then enable it in your kernel (a flex recipe is coming soon): | ||
|
||
```php | ||
// config/bundles.php | ||
<?php | ||
|
||
return [ | ||
//... | ||
Pontedilana\WeasyprintBundle\WeasyprintBundle::class => ['all' => true], | ||
//... | ||
]; | ||
``` | ||
|
||
## Configuration | ||
If you need to change the binaries, change the instance options or even disable one or both services, you can do it through the configuration. | ||
|
||
```yaml | ||
# config/packages/weasyprint.yaml | ||
weasyprint: | ||
pdf: | ||
enabled: true | ||
binary: /usr/local/bin/weasyprint | ||
options: [] | ||
image: | ||
enabled: true | ||
binary: /usr/local/bin/weasyprint | ||
options: [] | ||
``` | ||
If you want to change temporary folder which is ```sys_get_temp_dir()``` by default, you can use | ||
|
||
```yaml | ||
# config/packages/weasyprint.yaml | ||
weasyprint: | ||
temporary_folder: "%kernel.cache_dir%/weasyprint" | ||
``` | ||
|
||
You can also configure the timeout used by the generators with `process_timeout`: | ||
|
||
```yaml | ||
# config/packages/weasyprint.yaml | ||
weasyprint: | ||
process_timeout: 20 # In seconds | ||
``` | ||
|
||
## Usage | ||
|
||
The bundle registers two services: | ||
|
||
- the `weasyprint.pdf` service allows you to generate pdf files. | ||
- the `weasyprint.image` service allows you to generate images (works only with WeasyPrint 52.5 or lower); | ||
|
||
### Generate a pdf document from a URL | ||
|
||
```php | ||
// @var Pontedilana\PhpWeasyPrint\Pdf | ||
$weasyprintPdf->generate('https://www.github.com', '/path/to/the/file.pdf'); | ||
``` | ||
|
||
### Generate a pdf document from a twig view | ||
|
||
```php | ||
// @var Pontedilana\PhpWeasyPrint\Pdf | ||
$weasyprintPdf->generateFromHtml( | ||
$this->renderView( | ||
'frontend/product/pdf.html.twig', | ||
[ | ||
'some' => $vars, | ||
] | ||
), | ||
'/path/to/the/file.pdf' | ||
); | ||
``` | ||
|
||
### Render a pdf document as response from a controller | ||
|
||
```php | ||
use Pontedilana\WeasyprintBundle\WeasyPrint\Response\PdfResponse; | ||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | ||
class SomeController extends AbstractController | ||
{ | ||
public function pdfAction(Pontedilana\PhpWeasyPrint\Pdf $weasyprintPdf) | ||
{ | ||
$html = $this->renderView( | ||
'frontend/product/pdf.html.twig', | ||
[ | ||
'some' => $vars, | ||
] | ||
); | ||
return new PdfResponse( | ||
$weasyprintPdf->getOutputFromHtml($html), | ||
'file.pdf' | ||
); | ||
} | ||
} | ||
``` | ||
|
||
### Render a pdf document with a relative url inside like css files or images | ||
|
||
```php | ||
use Pontedilana\WeasyprintBundle\WeasyPrint\Response\PdfResponse; | ||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | ||
class SomeController extends AbstractController | ||
{ | ||
public function pdfAction(Pontedilana\PhpWeasyPrint\Pdf $weasyprintPdf) | ||
{ | ||
$pageUrl = $this->generateUrl('homepage', [], true); // use absolute path! | ||
return new PdfResponse( | ||
$weasyprintPdf->getOutput($pageUrl), | ||
'file.pdf' | ||
); | ||
} | ||
} | ||
``` | ||
|
||
## Credits | ||
|
||
WeasyPrintBundle and [PhpWeasyPrint](https://github.com/pontedilana/php-weasyprint) has been developed by [Pontedilana](https://www.pontedilana.it/). | ||
SnappyBundle has been developed by [KnpLabs](https://knplabs.com). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
{ | ||
"name": "pontedilana/weasyprint-bundle", | ||
"type": "symfony-bundle", | ||
"description": "Easily create PDF in Symfony by converting Twig/HTML templates.", | ||
"keywords": [ | ||
"weasyprint", | ||
"pdf", | ||
"bundle", | ||
"symfony-bundle" | ||
], | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Pontedilana Dev Team", | ||
"homepage": "https://www.pontedilana.it" | ||
} | ||
], | ||
"require": { | ||
"php": "^7.4 || ^8.0", | ||
"pontedilana/php-weasyprint": "^0.9 || ^1.0", | ||
"symfony/framework-bundle": "^4.4 || ^5.3" | ||
}, | ||
"require-dev": { | ||
"doctrine/annotations": "^1.11", | ||
"friendsofphp/php-cs-fixer": "^3.0", | ||
"phpstan/phpstan": "^0.12", | ||
"phpstan/phpstan-phpunit": "^0.12", | ||
"symfony/asset": "^4.4 || ^5.3", | ||
"symfony/finder": "^4.4 || ^5.3", | ||
"symfony/phpunit-bridge": "^4.4 || ^5.3", | ||
"symfony/security-csrf": "^4.4 || ^5.3", | ||
"symfony/templating": "^4.4 || ^5.3", | ||
"symfony/validator": "^4.4 || ^5.3", | ||
"symfony/yaml": "^4.4 || ^5.3" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Pontedilana\\WeasyprintBundle\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Pontedilana\\WeasyprintBundle\\Tests\\": "tests/" | ||
} | ||
}, | ||
"scripts": { | ||
"check-cs": "vendor/bin/php-cs-fixer fix --diff --dry-run --verbose", | ||
"fix-cs": "vendor/bin/php-cs-fixer fix --verbose", | ||
"static-analysis": "vendor/bin/phpstan analyse --ansi", | ||
"test": "vendor/bin/simple-phpunit" | ||
} | ||
} |
Oops, something went wrong.