Grails 渲染插件在部署时不会渲染图像

发布于 2024-12-17 02:50:20 字数 678 浏览 0 评论 0原文

我正在使用 grails 渲染插件生成 pdf。 在开发过程中,我使用带 src 的标签将图像包含到服务器上的图像中。它工作正常,但部署应用程序后就不再工作了。图像就消失了。

我正在通过模板进行重新渲染,因此当我在 gsp 中调用此模板时,这是可以的(即我看到带有图像的 pdf 的 html 版本),但是当使用 renderPdf 在我的控制器中调用时,没有图像。

同样,仅在已部署的应用程序(战争)中,而不是在开发中。

有什么提示吗?

编辑:根据流行的要求,这里是代码:

_pdf.gsp 文件可以像

<!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>

控制器的相关部分一样简单

def pdf={
     renderPdf(template: "/file/pdf", filename: "myfile.pdf")
}

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")
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

九命猫 2024-12-24 02:50:20

我已经看了这两天令人沮丧的事情,并尝试了很多东西。我的建议(也是他们在文档中给出的建议)是坚持使用内联图像。我的意思是以其字节形式定义图像。您不会后悔的,并且您将确信渲染插件将与其他插件(例如 ui-performance 和 resources)配合良好。

首先像这样创建文件对象

def paid = new File(ApplicationHolder.application.parentContext.servletContext.getRealPath("/images/paid.jpg"))

,然后将文件的字节传递到模型中

renderPdf(template:invoiceTemplate, model:[paidBytes:paid.bytes])

现在在您的 pdf 模板中使用以下标记:

<rendering:inlineJpeg bytes="${paidBytes}" />

如果您没有依赖性问题,那么您应该被设置。
如果您确实遇到了像我一样的依赖性问题,并在此处中指出,那么查看您引入的 iText 版本。

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.

失退 2024-12-24 02:50:20

这肯定与渲染插件的文档中所述有关:

“所有资源链接(例如图像、CSS)必须可由应用程序访问。这是因为链接的资源是由应用程序而不是浏览器访问的根据生产中的网络配置,这可能需要一些特殊考虑。”

可能的提示:如果您的服务器上运行的是 Linux,请将站点的域名添加到 /etc/hosts,以便其解析为 127.0.0.1。如果其他操作系统也这样做。

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.

丿*梦醉红颜 2024-12-24 02:50:20

正如 wwwclaes 所说,它与应用程序可用的资源有关。

但是,如果您使用资产管道,则可以使其变得更简单一些。

控制器:

def assetResourceLocator
...
def myAction() {
...
renderPdf(模板:invoiceTemplate, 模型:[rl:assetResourceLocator])
}
...

然后在您的模板中:


现在,您可以对模板中的任意数量的图像使用 rl,而无需用演示项目弄乱控制器中的模型。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文