Coder Social home page Coder Social logo

grails-wkhtmltopdf's Introduction

Grails Wkhtmltox / Wkhtmltopdf

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

Wkhtmltopdf

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

Installation

mac

brew install wkhtmltopdf

linux

apt-get install wkhtmltopdf

see: http://code.google.com/p/wkhtmltopdf/wiki/compilation compiled versions available at: http://code.google.com/p/wkhtmltopdf/downloads/list

finally make sure the following command works as expected:

wkhtmltopdf www.google.de test.pdf

Configuration

put the following line into your Config.groovy and adjust the path to your wkhtmltox binary ( which wkhtmltopdf )

unix

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

windows

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

also add the pdf mime type to Config.groovy

grails.mime.types = [
    all:           '*/*',
    .....
   pdf:         'application/x-pdf'
]

Usage

to stream the content of an controller-action as pdf just call: /context/some/someAction.pdf

class SomerController {
    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 implements Serializable {

		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/x-pdf", pdfData
        //    body "see my pdf attachment"
        //}
    }
}

write your gsps as usual, just make sure, that the url's to the resources are absolute and reachable by the host maschine

<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

grails-wkhtmltopdf's People

Contributors

julienrott avatar quorak avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

grails-wkhtmltopdf's Issues

Mime type application/x-pdf makes it difficult to open downloaded PDF on Android

I am unable to open a downloaded PDF on Android (4.4.2). Looks like this is because header "Content-Type" in the response is "application/x-pdf".
If I change this to "application/pdf", then everything works fine.
As "application/pdf" was registered 20 years ago, according to http://stackoverflow.com/questions/312230/proper-mime-media-type-for-pdf-files, it should be safe to use "application/pdf" instead of "application/x-pdf".

This plugin works great on my Window7 box

Hi,

This is not an issue. Just want to let people to know that this plugin is tested by me to work fine on Windows box.

I used the Wkhtmltopdf windows installer and installed it to the following directory:

C:\local\wkhtmltopdf

In Config.pdf, I have

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

Things work just fine after that.

Cheers,
Jingzhao

Temp files not deleted?

Hi,

When I looked at the source code, I found that for

header:"/pdf/someHeader",
footer:"/pdf/someFooter",

Temporary files are generated. However, I don't see where those temp files are deleted. Would this become a problem when I have many PDF exports on my Grails website? Or am I missing something?

Thanks,
Jingzhao

Commit rights

Hi Tobias,

I tried to contact you at the email address mentioned in the plugin descriptor. But it is maybe not in use anymore?

We are using this excellent plugin in a project, but we have a couple of issues we need to sort out. See my pull requests. Are you still maintaining this plugin? If so, can you have a look at my pull requests, at least the fix for #9 ? If not, can I get commit rights to this repo, and ask the Grails team about rights to publish new versions? Alternatively, I could maintain the plugin in my fork if you don't want ownership anymore.

Best Regards,
Ronny Løvtangen

With Grails 2.2.1: No such property: mergedConfig Error

Hi, since I ported my app to Grails 2.2.1,
I get the following error message:

No such property: mergedConfig for class: org.codehaus.groovy.grails.commons.DefaultGrailsApplication. Stacktrace follows:
Message: No such property: mergedConfig for class: org.codehaus.groovy.grails.commons.DefaultGrailsApplication
Line | Method
->> 53 | makePdf in org.wkhtmltox.WkhtmltoxService

I suspect that it has to do with the "Plugin Config" -Plugin, but I have no idea how to fix this error.

Any hints are greatly appreciated
Johannes

No pdf output.

Hello

am trying use the Grails wkhtmltopdf plugin but i can't get a rendered pdf file. Only the gsp template is shown instead.

What I have done so far:

This is the link, which calls the action in the Controller:


g:link action='downloadQuestionnaire' id="${questionnaireInstance?.id}">Download questionnaire /g:link

This is the action in the Controller:


def downloadQuestionnaire = {
    def questionnaire = Questionnaire.get(params.id) 

    render( filename:"File ${questionnaire.id}.pdf",
    view:'/templates/pdf/_questionnaire.gsp',
    model:[questionnaireInstance: questionnaire],
    marginLeft:20,
    marginTop:35,
    marginBottom:20,
    marginRight:20,
    headerSpacing:10,
    )
}

_questionnaire.gsp is the template, which should be rendered as pdf.

In config.groovy


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

and i checked, that wkhtmltopdf ist working correctly.

To my understanding of the wkhtmltopdf plugin, the template _questionnaire.gsp should be rendered as pdf available to download. But in my case, the gsp-template is shown with the data of the questionnaireInstance. There are no error messages at all.

Any ideas, how to get the pdf output?

Yours Johannes

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.