pmarien / html-to-pdf-bundle
Symfony integration for pmarien/html-to-pdf
Installs: 46
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: >=8.1
- pmarien/html-to-pdf: ^1.0
- symfony/framework-bundle: ^5.0|^6.0|^7.0
- symfony/psr-http-message-bridge: ^2.0|^6.0|^7.0
Requires (Dev)
- roave/security-advisories: dev-latest
- twig/twig: ^3.8
README
Symfony integration for pmarien/html-to-pdf
- Requirements
- How to install?
- How to configure?
- How to use internal assets?
- How to use?
- Controller (Inline PDF)
- Controller (Download PDF)
- Controller (Store PDF file)
- With Twig Templates
- Controller (Inline PDF)
- Controller (Download PDF)
- Controller (Store PDF file)
Requirements
The bundle requires PHP in version 8.1 or higher and Symfony in version 5, 6 or 7.
The html-to-pdf-library requires implementations of psr/http-message
, psr/http-factory
and psr/http-client
.
How to install?
The bundle should be installed via composer: pmarien/html-to-pdf
.
composer req pmarien/html-to-pdf
How to configure?
If you want to configure the bundle you should add the file config/packages/html_to_pdf.yaml
.
Without api key configuration the bundle will act as anonymous client, which leads to a limit of 3 generated PDF files per day.
html_to_pdf: apiKey: 'YOUR_API_KEY' assetScheme: 'http' #optional to use a different asset location assetHost: 'example.com' #optional to use a different asset location
How to use internal assets?
If you want to use assets in your pdf, they have to be public accessible via http for the pdf generator. Sometimes you want to use the assets, but don't want to publish them. For those cases, this bundle comes with an AssetAccessController, which requires a hash for file protection.
The routing have to be enabled via config (config/routes/html_to_pdf.yaml
):
HtmlToPdfBundle: resource: '@HtmlToPdfBundle/Resources/config/routes.xml'
Asset uris can be build manually (route name: html_to_pdf_get_file
, required parameters: filename
and hash
) or via
twig filter:
<img src="{{ '/assets/images/test.png'|pdfAsset }}" alt="Test">
If you build uris manually, the hash can be generated via PMA\HtmlToPdfBundle\Asset\AssetAccessorInterface::getHash
(available as symfony service).
How to use?
If you want to generate pdf files directly from html, you should use PMA\HtmlToPdfBundle\Bridge\FoundationBridge
as
generator. It's available as service in the service container and may be autowired by symfony's dependency injection.
Controller (Inline PDF)
public function showAction(\PMA\HtmlToPdfBundle\Bridge\FoundationBridge $generator):\Symfony\Component\HttpFoundation\Response{ return $generator->inlineResponse( 'test.pdf', '<html><body><p>This is a test!</p></body></html>' ); }
Controller (Download PDF)
public function downloadAction(\PMA\HtmlToPdfBundle\Bridge\FoundationBridge $generator):\Symfony\Component\HttpFoundation\Response{ return $generator->attachmentResponse( 'test.pdf', '<html><body><p>This is a test!</p></body></html>' ); }
Service (Store PDF file)
public function storeFile(\PMA\HtmlToPdfBundle\Bridge\FoundationBridge $generator):void { $generator->createFile( '/your/project/file-storage/test.pdf', '<html><body><p>This is a test!</p></body></html>' ); }
Twig
If you want to generate pdf files from twig templates, you should use PMA\HtmlToPdfBundle\Bridge\TwigBridge
as
generator. It's available as service in the service container and may be autowired by symfony's dependency injection.
It requires Twig to be installed in your project.
Controller (Inline PDF file)
public function showAction(\PMA\HtmlToPdfBundle\Bridge\TwigBridge $generator):\Symfony\Component\HttpFoundation\Response{ return $generator->inlineResponse( 'test.pdf', 'pdf-templates/test.pdf.twig', [ 'test'=>'Test' ] ); }
Controller (Download PDF file)
public function downloadAction(\PMA\HtmlToPdfBundle\Bridge\TwigBridge $generator):\Symfony\Component\HttpFoundation\Response{ return $generator->attachmentResponse( 'test.pdf', 'pdf-templates/test.pdf.twig', [ 'test'=>'Test' ] ); }
Service (Store PDF file)
public function storeFile(\PMA\HtmlToPdfBundle\Bridge\TwigBridge $generator):void { $generator->createFile( '/your/project/file-storage/test.pdf', 'pdf-templates/test.pdf.twig', [ 'test'=>'Test' ] ); }