DEPRECATION WARNING

This documentation is not using the current rendering mechanism and is probably outdated. The extension maintainer should switch to the new system. Details on how to use the rendering mechanism can be found here.

EXT: exinit | wkhtmltopdf

Created:2010-02-18T17:33:18
Changed:2010-11-25T14:42:12
Classification:exinit_wkhtmltopdf
Description:forEditors, forAdmins, forBeginners
Keywords:wkhtmltopdf, PDF, HTML, WebKit, htmltopdf, html2pdf
Author:Thorsten Boock
Email:t.boock@exinit.de
Info 4:
Language:en

img-1 img-2 EXT: exinit | wkhtmltopdf - exinit_wkhtmltopdf

EXT: exinit | wkhtmltopdf

Extension Key: exinit_wkhtmltopdf

Language: en

Keywords: wkhtmltopdf, PDF, HTML, WebKit, htmltopdf, html2pdf

Copyright 2010, Thorsten Boock, <t.boock@exinit.de> (Exinit GmbH & Co. KG, http://www.exinit.de/ )

This document is published under the Open Content License

available from http://www.opencontent.org/opl.shtml

The content of this document is related to TYPO3

- a GNU/GPL CMS/Framework available from www.typo3.org

Table of Contents

EXT: exinit | wkhtmltopdf 1

`Introduction 3 <#__RefHeading__945_1580681928>`_

What does it do? 3

Screenshots 4

Safari showing typo3.org 4

A PDF generated by wkhtmltopdf showing typo3.org 4

`Users manual 5 <#__RefHeading__951_1580681928>`_

How to trigger the PDF generation 5

Caching 5

`Administration 6 <#__RefHeading__957_1580681928>`_

Requirements 6

Installation 6

`Configuration 7 <#__RefHeading__959_1580681928>`_

Reference 7

`Technical background 9 <#__RefHeading__1988_1580681928>`_

How it works 9

Security 9

`To-Do list 10 <#__RefHeading__967_1580681928>`_

`Testing on Microsoft Windows servers. 10 <#__RefHeading__1162_1580681928>`_

`ChangeLog 11 <#__RefHeading__969_1580681928>`_

`Thank you 12 <#__RefHeading__1390_1580681928>`_

Introduction

What does it do?

exinit_wkhtmltopdf generates PDF files from the HTML output of TYPO3 frontend pages and triggers their download. The rendering is done by an application called wkhtmltopdf which is shipped with the extension in form of an executable for Linux. Wkhtmltopdf uses the WebKit browser engine and therefore creates PDF files which look quite the same way they would look in webbrowsers using the same engine – for example Apple Safari or Google Chrome.

What differs it from other PDF generator extensions?
  • exinit_wkhtmltopdf has been created with the aim of combining outstanding features of the extensions pdf_generator2 and webkitpdf to provide the best user experience possible.
  • The usage of wkhtmltopdf makes webdesigners happy. XHTML and CSS2 are fully supported. Parts of CSS3 are supported, too.
  • If you use TemplaVoilà, you can mark subtemplates as “PDF Version” - exactly like you would mark them as “Print Version”. The so marked subtemplates will then be used for PDF rendering. If you don't create such a subtemplate, the default template will be used. Of course you don't need to use TemplaVoilà – the Template Auto-parser or CONTENT objects do the job just fine.
  • Generated PDF files are cached to take load from the server.

Screenshots

img-3 Safari showing typo3.org

img-4 A PDF generated by wkhtmltopdf showing typo3.org

Rendering comparison of Apple Safari and wkhtmltopdf. As you can see it looks quite the same. Wkhtmltopdf even executes javascript. This for example enables to you to use Cufón for font rendering in the PDF version.

Users manual

How to trigger the PDF generation

PDF file generation is triggered by the type parameter. The default page type number to operate on is 4096. Therefore you can render pdf files by appending &type=4096 to a pages URL. For example: index.php?id=1&type=4096

The page type number can be changed by TypoScript. You may want to take a look at the next chapter if you want to do so.

Caching

Generated PDF files are cached (saved on the hard disk) to take load of the server. They are deleted automatically as soon as you clear the TYPO3 cache in the backend.

Administration

Requirements

  • exinit_wkhtmltopdf requires at least TYPO3 4.3.0.
  • If you intend using the 32bit wkhtmltopdf binary shipped with this extension but your server is running a 64bit Linux Kernel you need to ensure that it includes IA-32 support and you have installed IA-32 runtime libraries. If you are hosting your TYPO3 project on a managed server or webspace package this will probably be the case. Alternatively you can download the AMD64 port of wkhtmltopdf.
  • If your server isn't running Linux you need to download a version of wkhtmltopdf suitable for your operating system.

An AMD64 port for Linux and executables for several different operating systems can be found on the projects website: http://code.google.com/p/wkhtmltopdf/ .

Installation

  • Download and install the extension via the extension manager. Change the extension configuration to your needs. Make sure you at least change the “sharedSecret” which is used to identify requests by wkhtmltopdf. If you don't change this value, users will be able to view the HTML source used for PDF file generation by requesting a page and adding the default shared secret to the URL.
  • Add the extensions' default TypoScript: Edit your root template, choose the “Includes” tab and add the extensions default TypoScript configuration by clicking on “wkhtmltopdf (exinit_wkhtmltopdf)”. Save the template record.
  • Customize the extensions' TypoScript setup. If you don't intend to use the wkhtmltopdf binary shipped with this extension you'll probably will want to change the path to the binary here. If you're not using TemplaVoilà you may want to change the path to the template file to be used. In any case you will probably want to configure the CSS files to be used.
  • Test if anything works as expected. For example you could open http://example.com/?type=4096 in your browser to generate a PDF file from your start page.

Configuration

Reference

Constants (general configuration)
ignoreTemplaVoilaInstallation

Property

ignoreTemplaVoilaInstallation

Data type

int

Description

Change this properties value to 1 if you don't want to use TemplaVoilà for rendering even if it's installed, loaded and configured for the current page.

Default

0

wkhtmltopdf.options.margin-left

Property

wkhtmltopdf.options.margin-left

Data type

string

Description

Document margin left.

Default

15mm

wkhtmltopdf.options.margin-right

Property

wkhtmltopdf.options.margin-right

Data type

string

Description

Document margin right.

Default

15mm

wkhtmltopdf.options.margin-top

Property

wkhtmltopdf.options.margin-top

Data type

string

Description

Document margin top.

Default

15mm

wkhtmltopdf.options.margin-bottom

Property

wkhtmltopdf.options.margin-bottom

Data type

string

Description

Document margin bottom.

Default

15mm

[plugin.tx_exinitwkhtmltopdf]

Example
plugin.tx_exinitwkhtmltopdf {
    ignoreTemplaVoilaInstallation = 0
    wkhtmltopdf {
            options {
                    margin-left = 15mm
                    margin-right = 15mm
                    margin-top = 15mm
                    margin-bottom = 15mm
            }
    }
}
Setup (general configuration)

Note: any properties can use stdWrap.

filenamePrefix

Property

filenamePrefix

Data type

string / stdWrap

Description

Prefix for the filename. The filename will be generated from this prefix followed by the page title, followed by an md5 hash sum.

Default

ewhtp_

wkhtmltopdf.executablePath

Property

wkhtmltopdf.executablePath

Data type

string / stdWrap

Description

Path to the wkhtmltopdf binary. The path can either be absolute or relative from the TYPO3 doc root.

Default

EXT:exinit_wkhtmltopdf/bin/wkhtmltopdf-i386

wkhtmltopdf.options

Property

wkhtmltopdf.options

Data type

array / stdWrap

Description

Options that will be passed to wkhtmltopdf during the PDF generation process. These get prefixed with “--” automatically.

Options specified in the constants are getting merged with these.

Example:

plugin.tx_exinitwkhtmltopdf {

wkhtmltopdf {

options {

footer-right = Footer

footer-right.stdWrap.warp = | Message

stop-slow-scripts =

}

}

}

Default

Example
plugin.tx_exinitwkhtmltopdf {
    filePrefix = ewhtp_
    wkhtmltopdf {
            executablePath = EXT:exinit_wkhtmltopdf/bin/wkhtmltopdf-i386
            options {
                    footer-right = Footer
                    footer-right.stdWrap.warp = | Message
                    stop-slow-scripts =
            }
    }
}
Setup (template configuration)

If you are not using TemplaVoilà and didn't customize the page object to do otherwise, this template object will be used for rendering of the TYPO3 pages HTML output. See http://wiki.typo3.org/TSref/TEMPLATE for details regarding template objects. You may override this object with anything else generating HTML output.

template.file

Property

template.file

Data type

string

Description

Path to the template file to use.

Default

EXT:exinit_wkhtmltopdf/res/template.html

workOnSubpart

Property

workOnSubpart

Data type

string

Description

The subpart to work on.

Default

DOCUMENT

marks

Property

marks

Data type

array

Description

Marker definition.

Default

main_content_area < styles.content.get

[lib.tx_exinitwkhtmltopdf_template]

Example
lib.tx_exinitwkhtmltopdf_template = TEMPLATE
lib.tx_exinitwkhtmltopdf_template {
    template = FILE
    template.file = EXT:exinit_wkhtmltopdf/res/template.html
    workOnSubpart = DOCUMENT
    marks.main_content_area < styles.content.get
}

Technical background

How it works

exinit_wkhtmltopdf can be seen as a gateway for wkhtmltopdf. The extension adds a new page object to the TypoScript setup and – once a page is requested with the corresponding page type - causes TYPO3 to use a custom page generator script instead of the default one provided by the cms package. The custom page generator queries the cache and – if the requested PDF file has been found in the cache – triggers the download. If the file hasn't been generated yet, the custom page generator executes wkhtmltopdf which renders and saves the PDF file which then gets added to the cache.

Security

The usage of exinit_wkhtmltopdf is quite restricted. Users can only access TYPO3 frontend pages by using it. It isn't possible to render any files on the webserver which aren't embedded in or referenced by the rendered HTML code of a frontend page. Extensions that work by passing the URLs to render as get or post parameters may pass over security restrictions. For example if you are identifying developers by the loopback IP-Address (127.0.0.1 / localhost), requests made by an external tool like wkhtmltopdf will be treated as being made from developers. In some cases you could thereby could give data to normal frontend users which isn't meant for them to be seen. Another scenario is the usage of Apaches' mod_info. Usually access to this module is limited to the loopback IP-Address. If a user somehow figures out the location that triggers the module to output content (for example /server-info) he could access it and get sensitive information like system paths by using a PDF generator that takes the URLs to render from get or post parameters.

To-Do list

Testing on Microsoft Windows servers.

ChangeLog

0.1.0

Version

0.1.0

Changes

First working version.

Thank you

At first I'd like to thank the authors of wkhtmltopdf for creating such an awesome tool and releasing it under the GPL.

As this extension is largely inspired by the extensions pdf_generator2 and webkitpdf a big thank you goes to their authors: Dev-Team Typoheads and Jens Ellerbrock.

I'd also like to thank Benjamin Bergmann for reviewing this manual and giving me optimization hints.

Last but not least, this extension couldn't have been developed without the admission and support of my employer, Exinit GmbH & Co. KG TYPO3 Internetagentur Hamburg .

img-2 12