问题
I am generating a pdf using grails rendering plugin. When in development, I include images using tag with src to an image on server. It works fine, but not anymore when the app is deployed. The images just disappear.
I am redering through a template, so when i call this template to in gsp, it is ok (ie i see the html version of the pdf with the images), bu when called in my controller using renderPdf, no images.
Again only in deployed app (war), not in development.
Any hints?
EDIT: Under popular request here is the code:
the _pdf.gsp file can be as simple as
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head/>
<body>
<img src="path/to/image.jpg"/>
</body>
</html>
the relevant part of the controller is
def pdf={
renderPdf(template: "/file/pdf", filename: "myfile.pdf")
}
回答1:
I have been looking at this for two frustrating days, and have tried many things. My advice, and it is the advice that they do give in the documentation is to stick to inline images. By that I mean define the image in its byte form. You won't regret it, and you will be sure the rendering plugin will play nice with other plugins like ui-performance and resources.
First create the file object like so
def paid = new File(ApplicationHolder.application.parentContext.servletContext.getRealPath("/images/paid.jpg"))
then pass in the bytes of the file into the model
renderPdf(template:invoiceTemplate, model:[paidBytes:paid.bytes])
Now in your pdf template use the following tag:
<rendering:inlineJpeg bytes="${paidBytes}" />
If you have no dependency issues then you should be set. If you do run into dependency issues like I did and noted HERE, then look at the version of iText your pulling in.
回答2:
It most certainly has to do with what's stated in the docs for the rendering plugin:
"All links to resources (e.g. images, css) must be accessible by the application . This is due to the linked resources being accessed by application and not a browser. Depending on your network config in production, this may require some special consideration."
Probable tip: If you are running Linux on your server, add your site's domain name to /etc/hosts so it resolves to 127.0.0.1. If other OS do accordingly.
回答3:
As wwwclaes says, it is about resources available to the application.
But, if you are using asset-pipeline, you can make it a little simpler.
Controller:
def assetResourceLocator
...
def myAction() {
...
renderPdf(template:invoiceTemplate, model:[rl:assetResourceLocator])
}
...
Then in your template:
<rendering:inlineJpeg bytes="${rl?.findAssetForURI('paid.jpg').byteArray}" />
Now you can use rl for any number of images in your template without mucking up the model in the controller with presentation items.
来源:https://stackoverflow.com/questions/8209707/grails-rendering-plugin-does-not-render-images-when-deployed