Giter Site home page Giter Site logo

grails-wkhtmltopdf's Introduction

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.
http://wkhtmltopdf.org/

Installation

In addition to adding the plugin as dependency to your Grails project, the wkhtmltopdf binary must be installed. See instructions at http://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

class SomeService {

    static transactional = false

    def doSomething() {
        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 assets are absolute and reachable by the host machine

Development mode:

grails.assets.url = "http://localhost:8080/assets/"

Production:

grails.assets.url = "https://example.com/assets/"

Options

See the following command for all options available:

wkhtmltopdf --extended-help

Known issues

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

  • Not tested on Windows (except Windows 7)

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.