Skip to content

salimhb/grails-wkhtmltopdf

 
 

Repository files navigation

Grails wkhtmltopdf

This plugin provides an easy integration of wkhtmltopdf, a command line tool to render HTML into PDF.

This enables you to write a Grails View in HTML as usual, but render as PDF.

Wkhtmltopdf

wkhtmltopdf and wkhtmltoimage are open source (LGPLv3) command line tools to render HTML into PDF and various image formats using the Qt WebKit rendering engine. These run entirely "headless" and do not require a display or display service.
https://wkhtmltopdf.org/

Installation

In addition to adding the plugin as dependency to your Grails project, the wkhtmltopdf binary must be installed. See instructions at https://wkhtmltopdf.org/ or install using your package manager (apt-get, yum, brew etc)

Finally make sure the following command works as expected:

wkhtmltopdf www.google.com test.pdf

Configuration

Put the following line into your application.groovy (or corresponding property in application.yml) and adjust the path to your wkhtmltopdf binary (which wkhtmltopdf)

grails.plugin.wkhtmltopdf.binary = "/usr/local/bin/wkhtmltopdf"

Typical paths:

OS Path

OS X

/usr/local/bin/wkhtmltopdf

Linux

/usr/bin/wkhtmltopdf

Windows

C:/local/wkhtmltopdf/wkhtmltopdf.exe

Supported Grails versions

Grails 3.0.0 - 3.0.2

Not supported

Grails 3.0.3

Add the pdf mime type to grails.mime.types in application.yml

pdf: application/pdf

Grails 3.0.4 - 3.0.7

Supported, but only with mail plugin 2.0.0.RC4. (See grails/grails-mail#16)

Grails 3.0.8 > *

Supported

Usage

To stream the content of a Controller-action as PDF just call /some/someAction.pdf

class SomeController {
    def someAction() {
        def someInstance = SomeDomainObject.get(params.id)

        render( filename:"File ${someInstance.id}.pdf",
                view:"/some/someGspTemplate",
                model:[someInstance:someInstance],
                header:"/pdf/someHeader",
                footer:"/pdf/someFooter",
                marginLeft:20,
                marginTop:35,
                marginBottom:20,
                marginRight:20,
                headerSpacing:10)
    }
}

Or create binary PDF data and use them for any other purpose