Play Framework:从使用 Javascript 进行绘图的模板生成 PDF
我有一个模板,其中包含一些用于在浏览器中生成图形的 JavaScript。我想使用相同的模板创建 PDF 并作为电子邮件附件发送。在这种情况下,不会有浏览器/客户端交互。
我正在使用 Play 网站上提供的 PDF 模块,并且我已经成功地使 PDF 渲染正常工作。唯一的问题是图表不会显示在 PDF 中,但所有其他静态文本都会显示。我假设由于在 PDF 生成之前未执行 Javascript,因此 PDF 中没有出现图表。
有谁对如何解决这个问题有任何想法?
I have a template that has some Javascript used to generate graphs in the browser. I would like to use that same template to create a PDF and send as an attachment in an e-mail. In this scenario, there would be no browser/client interaction.
I am using the PDF module that is available from the Play website and I have managed to get the PDF rendering to work. The only issue is that the graphs don't show up in the PDF but all other static text does. I'm assuming the graphs aren't appearing in the PDF due to the Javascript not being executed prior to the PDF generation.
Does anyone have any ideas on how to get around this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于您使用的是 Play 框架,最简单的方法可能是使用 Rhino。 http://www.mozilla.org/rhino/ 这是 Mozilla 的 Javascript 实现,运行在JVM,Play 框架运行于其上。您可能需要对 Javascript 进行一些更改,例如,如果它使用浏览器 Canvas API,您将必须创建一个外观对象,将这些绘图命令发送到 PDF 绘图对象而不是屏幕。或者如果您想要的话,两个地方都可以。
如果您提供有关 Javascript 图形代码的更多信息,您可能会得到更详细的答案。
Since you are using the Play framework, the easiest way is likely to be using Rhino. http://www.mozilla.org/rhino/ That is Mozilla's implementation of Javascript to run on the JVM, which is what the Play framework runs on. You may have to make some changes to the Javascript, for instance if it uses the browser Canvas API you would have to make a facade object that sends those drawing commands to the PDF drawing object instead of the screen. Or to both places if that is what you want.
You might get a more detailed answer if you give some more information about the Javascript graphing code.
如果您找不到更好的选择,请考虑直接使用 itext。它是 pdf-plugin 中使用的 pdf 渲染库。您必须在 pdf 生成方面手动重写模板,但您可以完全控制结果。
If you won't find better options consider using itext directly. It's a pdf rendering library that is used in pdf-plugin. You'll have to manually rewrite template in terms of pdf generation but you'll have full control of the result.
我不知道这是否对你有帮助,但这对我来说非常有效。
我将画布转换为 PNG 文件:
然后我将该数据字符串发送到服务器,以便 Play 生成 PDF,并将变量传递给 PDF 生成器
我的 pdf 代码如下所示:
I don´t know if this helps you, but this is what worked really fast for me.
I transformed the canvas into a PNG with:
Then I sent that datastring to the server so that Play generates a PDF and I passed the variable to the PDF Generator
My pdf code looks like this: