将 Freemarker 转换为 PDF
我正在使用 freemarker 设计报告,我遇到一个问题,我需要 PDF 格式的处理后的输出。
我想要做的是将 HTML + CSS fremarker 模板传递给 freemarker 引擎,并将处理后的 HTML 作为 PDF 输出。我当前遇到的问题是如何将处理后的 freemarker 转换为 PDF
try {
Configuration cfg = new Configuration();
Template tpl = cfg.getTemplate("example.ftl");
OutputStreamWriter output = new OutputStreamWriter(System.out);
Map testHashMap = new HashMap();
testHashMap.put("test", "testValue");
tpl.process(testHashMap, output);
} catch (Exception e) {
e.printStackTrace();
}
在互联网上搜索时我找不到有关此主题的任何信息,但我发现了 iText 框架
try {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(new StringBufferInputStream(buf.toString()));
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(doc, null);
renderer.layout();
OutputStream os = response.getOutputStream();
renderer.createPDF(os);
os.close();
} catch (Exception ex) {
ex.printStackTrace();
}
现在的问题是如何组合这两个代码片段生成pdf?
非常感谢所有帮助
问候, 米琳达D
I am designing reports using freemarker, I have a problem where I need the processed output in a PDF format.
What I want to do is to pass an HTML + CSS fremarker template to the freemarker engine and output the processed HTML as an PDF. The current problem I have is on how to convert the processed freemarker to a PDF
try {
Configuration cfg = new Configuration();
Template tpl = cfg.getTemplate("example.ftl");
OutputStreamWriter output = new OutputStreamWriter(System.out);
Map testHashMap = new HashMap();
testHashMap.put("test", "testValue");
tpl.process(testHashMap, output);
} catch (Exception e) {
e.printStackTrace();
}
While searching on thje internet I couldnt find any information on this topic, but I found out about the iText framework
try {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(new StringBufferInputStream(buf.toString()));
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(doc, null);
renderer.layout();
OutputStream os = response.getOutputStream();
renderer.createPDF(os);
os.close();
} catch (Exception ex) {
ex.printStackTrace();
}
The problem now is how do I combine these two code fragments to generate a pdf?
All help is really appreciated
Regards,
MilindaD
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不建议使用iText,因为它受AGPL许可。
如果您根据 AGPL 许可获得的软件提供服务,则需要提供对该服务的完整软件的免费访问(可能存在“纯粹聚合”概念隐含的限制)。
您可以将 openhtmtopdf 用于商业目的,也可以免费使用,因为它受 LGPL 许可。
I would NOT suggest to use iText as it comes under AGPL license.
If you provide a service based on software you got under AGPL licensing, you need to provide free access to the complete software for the service (probably with the limits implied by the “mere aggregation” concept).
You can use openhtmtopdf for commercial purpose as well for free as it comes under LGPL license.
我认为最好使用两个不同的管道并将它们视为同一模型的两个不同视图。
数据-> Freemarker 变压器 -> HTML
数据 -> iText 转换器 -> 或者您可以在html
上使用 XSLT 并使用 XSL-FO(如 Apache FOP),但这对我来说似乎有点过分了。
I think it would be better to use two different pipelines and see them as two different views of the same model.
Data -> Freemarker transfomer -> HTML
Data -> iText transformer -> pdf
or you could use XSLT on the html and use XSL-FO like Apache FOP, but it seems overkill to me.
Pupeteer 似乎是一个不错的选择。您可以运行单独的节点进程来访问 API 和打印。
https://blog.risingstack.com/pdf-from-html -node-js-puppeteer/
Pupeteer seems to be a good option. You can run a separate node process to access your API and print.
https://blog.risingstack.com/pdf-from-html-node-js-puppeteer/