massive / pdf-bundle
The Massive Bundle for managing pdf documents
Installs: 16 429
Dependents: 3
Suggesters: 0
Security: 0
Stars: 0
Watchers: 24
Forks: 3
Open Issues: 0
Requires
- php: >=5.4
- knplabs/knp-snappy-bundle: ^1.4
- symfony/config: ^3.4 || ^4.0 || ^5.0 || ^6.0
- symfony/dependency-injection: ^3.4 || ^4.0 || ^5.0 || ^6.0
- symfony/framework-bundle: ^3.4 || ^4.0 || ^5.0 || ^6.0
- symfony/http-foundation: ^3.4 || ^4.0 || ^5.0 || ^6.0
- symfony/http-kernel: ^3.4 || ^4.0 || ^5.0 || ^6.0
- twig/twig: ^1.0 || ^2.0 || ^3.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.13
README
Built upon KnpSnappyBundle: https://github.com/KnpLabs/KnpSnappyBundle
Installation
Install the bundle with composer.
composer require massive/pdf-bundle
Add bundle to your symfony kernel.
new Knp\Bundle\SnappyBundle\KnpSnappyBundle(), new Massive\Bundle\PdfBundle\MassivePdfBundle(),
Install wkhtmltopdf
Ubuntu
apt-get install wkhtmltopdf
apt-get install xvfb
echo ‘xvfb-run –server-args=”-screen 0, 1024x768x24″ /usr/bin/wkhtmltopdf $*’ > /usr/bin/wkhtmltopdf.sh
chmod a+x /usr/bin/wkhtmltopdf.sh
ln -s /usr/bin/wkhtmltopdf.sh /usr/local/bin/wkhtmltopdf
wkhtmltopdf http://www.google.com output.pdf
MacOSX
http://wkhtmltopdf.org/downloads.html
Configure Knp Snappy Bundle**
See KnpSnappyBundle for configuration.
Usage
Controller Trait
The controller trait is the easiest way to generate a pdf:
<?php namespace AppBundle\Controller; use Massive\Bundle\PdfBundle\Controller\RenderPdfTrait; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; class YourController extends Controller { use RenderPdfTrait; public function pdfAction(Request $request): Response { return $this->renderPdf( '@pdfs/your.html.twig', [ 'parameter' => 'hello' ], $request->getRequestFormat() ); } }
Register the route with default _format
as pdf
:
<route id="your.pdf" path="/your.{_format}"> <default key="_controller">AppBundle:Your:pdf</default> <default key="_format">pdf</default> </route>
Now you can access the pdf with /your
or use /your.html
to get a html response (good for development).
Generate Pdf
/** @var \Massive\Bundle\PdfBundle\Pdf\PdfFactory $pdfFactory */ $pdfFactory = $this->get('massive_pdf.pdf_factory'); // get the service or inject it in your services configuration $pdf = $pdfFactory->create('pdf.html.twig');
Embedding local assets
The local_asset
avoids doing a http request by using file://
instead of https://
for performance improvement:
<img src="{{ local_asset('/images/image.jpg') }}" alt="Local Asset">
This will only work when $request->getRequestFormat()
will return pdf
and not html
.
If you want to force using file://
set the second parameter to true:
<img src="{{ local_asset('/images/image.jpg', true) }}" alt="Local Asset">