Have you ever wanted to deliver your SilverStripe website's content in a slow and bloated format? If you have, this module fulfils your needs!
- Use Composer:
composer require betterbrief/silverstripe-pdf
- Install the correct version of
wkhtmltopdf
...
- The version of the binary you use is dependent on your architecture. To find out the architecture your server is running, type
arch
in your terminal window. With the development environment, we use amd64.
- Once you have got the module from composer, you will need to get the wkhtmltopdf binary and then configure PDFExtension to point to it. The composer suggestions give you your options.
- Add
PDFExtension
to the objects you wish to render to PDF.
PDFExtension:
wkhtmltopdf_binary: '/vagrant/www/vendor/bin/wkhtmltopdf-amd64'
MyDataObject:
extensions:
- PDFExtension
- Author your template file called
MyDataObject_pdf.ss
in a suitable directory within your templates folder. Note that you can override this name if you wish to do so. - If this is a standalone file, you will need to include the base tag so assets are rendered correctly. In your template:
<% base_tag %>
- Congratulations, you can now call
MyDataObject->generatePDF()
and get your PDF data.- If you wish to send the output to the browser, you'll need to set your response body to
generatePDF()
's response, useSS_HTTPResponse->addHeader('Content-Type', 'application/pdf')
. - To force a download,
SS_HTTPResponse->addHeader('Content-Disposition', 'attachment')
- If you wish to send the output to the browser, you'll need to set your response body to
You can configure generation on a call-by-call basis by modifying the PDFExtension->generatePDF()
parameters.
- Use the
$userOptions
parameter to pass in Snappy / wkhtmltopdf options. Here's the full list. - Use
$variables
to pass in your standard SSViewer template variables. Note that by defaultenable-javascript
is turned off.
Config Option | Default | How to use |
---|---|---|
wkhtmltopdf_binary |
null |
Set this to the absolute location of wkhtmltopdf, otherwise nothing will work. |
render_host |
http://localhost/ |
As the page is generated on the server, the public facing host will typically not be accessible, and instead be localhost. You may want to change this when deploying to distributed environments. |
- Pages are generated in the context of the current session. So if the requesting user is a logged in administrator, the PDF will be generated with that member's state and permission level.
- It is advisable to get the following packages to avoid graphic / font issues with rendering: fontconfig, libXrender, libXext and urw-fonts. You can get these on CentOS with
yum install fontconfig libXrender libXext urw-fonts
- wkhtmltopdf will take the HTML you generate and create a PDF document out of it using Webkit as the rendering engine.
- Knp's Snappy provides the bridge functionality between wkhtmltopdf and PHP
- silverstripe-pdf's code is made available to you under the BSD license (see LICENSE)
- As of 2014-05-30, Knp Snappy is under the MIT license
- As of 2014-05-30, wkhtmltopdf is under the LGPL license.