Skip to content

ph4t/grails-wkhtmltopdf

 
 

Repository files navigation

Grails Wkhtmltox / Wkhtmltopdf

This plugin provides an easy integration of the wkhtmltox library into Grails.

Wkhtmltopdf

"Simple shell utility to convert html to pdf using the webkit rendering engine, and qt." http://wkhtmltopdf.org/ GNU Lesser GPL

Installation

mac

brew install wkhtmltopdf

linux

apt-get install wkhtmltopdf

Finally make sure the following command works as expected:

wkhtmltopdf www.google.de test.pdf

Configuration

Put the following line into your application.groovy and adjust the path to your wkhtmltopdf binary (which wkhtmltopdf)

unix

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

windows

grails.plugin.wkhtmltopdf.binary = "C:/local/wkhtmltopdf/wkhtmltopdf.exe"

If using Grails 3.0.3 or lower, add the pdf mime type to grails.mime.types in application.yml

pdf: application/pdf

Usage

To stream the content of an controller-action as pdf just call: /context/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

class SomeService {

    static transactional = false

        def byte[] pdfData = wkhtmltoxService.makePdf(
                view: "/pdf/someGspTemplate",
                model: [someInstance: someInstance],
                header: "/pdf/someHeader",
                footer: "/pdf/someFooter",
                marginLeft: 20,
                marginTop: 35,
                marginBottom: 20,
                marginRight: 20,
                headerSpacing: 10)


        // DO Something e.g. send as mail
        //sendAsynchronousMail {
        //    multipart true
        //    to "[email protected]"
        //    subject "see PDF Attachment";
        //    attachBytes "PDF Attachment.pdf", "application/pdf", pdfData
        //    body "see my pdf attachment"
        //}
    }
}

Write your gsps as usual, just make sure, that the urls to the resources are absolute and reachable by the host machine

<link rel="stylesheet" href="${resource(dir: '/css/style.css', absolute: true)}" type="text/css"/>
<img src="${resource(dir: '/images/image.jpg', absolute: true)}" width="200px"/>

Options

See the following command for all options available:

wkhtmltopdf --extended-help

Known issues

  • wkhtmltox must work ( try: wkhtmltopdf www.myhomepage.com myhomepage.pdf )

  • not tested on Windows (except windows7)

Changes since Grails 2 version

  • Properties renamed from grails.plugin.wkhtmltox.xxx to grails.plugin.wkhtmltopdf.xxx

  • Removed support for property grails.plugin.wkhtmltox.makeBinaryAvailableClosure. Was not documented and didn’t work as expected.

  • Package renamed to org.grails.plugins.wkhtmltopdf

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Groovy 100.0%