Download the PHP package orkestra/pdf-bundle without Composer
On this page you can find all versions of the php package orkestra/pdf-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download orkestra/pdf-bundle
More information about orkestra/pdf-bundle
Files in orkestra/pdf-bundle
Package pdf-bundle
Short Description Provides integration between Symfony and TCPDF
License MIT
Informations about the package pdf-bundle
OrkestraPdfBundle
Abstracts different PDF generation libraries. Currently supports TCPDF, Zend PDF and wkhtmltopdf.
This bundle is under active development and is prone to backwards compatibility breaks.
Installation
Using composer, in your project's root directory:
Update AppKernel.
In addition, you should add TCPDF or Zend PDF to your project, as necessary.
If planning to use wkhtmltopdf, ensure it is installed on your system.
Configuration
No configuration is necessary, unless you want to change default settings.
Configuration defaults reference:
Usage
This bundle's aim is to provide a uniform API for generating PDFs using different generation tools.
Currently supported: TCPDF, Zend PDF, and wkhtmltopdf.
Generators
The basis of this bundle revolves around services called "Generators". Generators know how to take
some input, such as entities and other data, and return a PDF ready to be used. Generators encapsulate
library-specific logic, returning a subtype of Orkestra\Bundle\PdfBundle\Pdf\PdfInterface
which can
then be used to send the generated PDF to the browser, save it to the filesystem, etc.
All Generators should implement PdfGeneratorInterface
. Additionally, a base class called
AbstractPdfGenerator
is included. This document details how to extend AbstractPdfGenerator
Creating an Invoice Generator
The two main methods to implement are: doGenerate
and setDefaultParameters
.
Implementing the doGenerate
method
This method actually performs the PDF generation. It takes two parameters, $parameters
and
$options
.
$parameters
is an array of data to be sent to the templating engine.$options
is an array of options to pass to the underlying PDF library.
Implementing the setDefaultParameters
method
This method configures an OptionsResolver
and allows specification of required and available
parameters that the Generator supports.
NOTE: There's also a setDefaultOptions
method available. Parameters are intended to be passed
to the templating engine, things like entities and collections. Options are intended to allow
configuration of the generator at runtime.
Example implementation: InvoiceGenerator
In this example, we use the WkPdf adapter and the built in templating engine to render a PDF.
Useful methods
AbstractPdfGenerator->createPdf($type, $options)
wraps the PDF factory registry. As the first
parameter, pass the type of PDF and options to configure it. Available PDF types: tcpdf
, wkpdf
,
and zendpdf
.
AbstractPdfGenerator->render($template, array $parameters = array())
uses the application's templating
engine to render templates for use in generating the content for the PDF.
Registering your generator
In services.yml:
AbstractPdfGenerator
by default, takes a PDF factory registry and the templating service. You may
need to add more dependencies, depending on your implementation.
Using your service
From within one of your controllers:
Strategies
Using TCPDF to render template fragments
TCPDF can be difficult to use, especially when rendering HTML. Usually, the easiest way is to
position different smaller sections of the page, making multiple writeHTML
or writeHTMLCell
calls on the TCPDF object.
Here's an example of the same Invoice Generator from before.
See TCPDF documentation for more details.
Using custom subclasses with TCPDF
TCPDF offers the ability to extend the TCPDF class directly to implement, for example, page headers and footers.
The default TcPdfFactory
lets you specify a custom subclass by specifying the 'className'
option when creating a PDF. For example:
Notice that $builder
in the example above is an instance of My\Bundle\CustomTcpdf
.