pontedilana / weasyprint-bundle
Easily create PDF in Symfony by converting Twig/HTML templates.
Installs: 442 872
Dependents: 0
Suggesters: 0
Security: 0
Stars: 32
Watchers: 2
Forks: 5
Open Issues: 2
Type:symfony-bundle
Requires
- php: 7.4.* || 8.0.* || 8.1.* || 8.2.* || 8.3.* || 8.4.*
- pontedilana/php-weasyprint: ^1.0
- symfony/config: ^5.4 || ^6.3 || ^7.0
- symfony/dependency-injection: ^5.4 || ^6.3 || ^7.0
- symfony/http-foundation: ^5.4 || ^6.3 || ^7.0
- symfony/http-kernel: ^5.4 || ^6.3 || ^7.0
Requires (Dev)
- doctrine/annotations: ^1.11 || ^2.0
- friendsofphp/php-cs-fixer: ^3.64
- phpstan/extension-installer: ^1.4
- phpstan/phpstan: ^1.12
- phpstan/phpstan-phpunit: ^1.4
- phpstan/phpstan-symfony: ^1.4
- phpunit/phpunit: ^9.6
- symfony/framework-bundle: ^5.4 || ^6.3 || ^7.0
- symfony/phpunit-bridge: ^5.4 || ^6.3 || ^7.0
- symfony/validator: ^5.4 || ^6.3 || ^7.0
- symfony/yaml: ^5.4 || ^6.3 || ^7.0
README
PhpWeasyPrint is a PHP (7.4+) wrapper for WeasyPrint 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, of which it aims to be a one-to-one substitute
Installation
With composer, require:
composer require pontedilana/weasyprint-bundle
Then enable it in your kernel (a flex recipe is coming soon):
// 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.
# config/packages/weasyprint.yaml weasyprint: pdf: 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
# config/packages/weasyprint.yaml weasyprint: temporary_folder: "%kernel.cache_dir%/weasyprint"
You can also configure the timeout used by the generators with process_timeout
:
# config/packages/weasyprint.yaml weasyprint: process_timeout: 20 # In seconds
Usage
The bundle registers one service:
- the
weasyprint.pdf
service allows you to generate pdf files.
Generate a pdf document from a URL
// @var Pontedilana\PhpWeasyPrint\Pdf $weasyprintPdf->generate('https://www.github.com', '/path/to/the/file.pdf');
Generate a pdf document from a twig view
// @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
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
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 has been developed by Pontedilana.
SnappyBundle has been developed by KnpLabs.