Java 使用 iText 和 apache 速度模板 (.vm) 生成 PDF 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21853809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generate PDF files using iText and apache velocity template(.vm)
提问by TechnoCrat
What is the general workflow to generate a PDF using iText and an Apache Velocity template file (.vm) in Java?
在 Java 中使用 iText 和 Apache Velocity 模板文件 (.vm) 生成 PDF 的一般工作流程是什么?
I am interested in knowing steps like: parse template file, put Java object in context and steps to be performed to generate pdf etc.
我有兴趣了解以下步骤:解析模板文件,将 Java 对象放入上下文中以及要执行的步骤以生成 pdf 等。
I know this is a very basic question. But I am not able to find even a single example of this type on the web. I found XDocReport, but I am interested to know other alternatives as well.
我知道这是一个非常基本的问题。但是我在网上找不到这种类型的一个例子。我找到了XDocReport,但我也有兴趣了解其他替代方案。
Please help me with some sample project link or at least the steps to get started.
请帮助我提供一些示例项目链接或至少入门步骤。
回答by Angelo
Generating PDF using iText and an Apache Velocity template file (.vm) in Java directly is not possible because:
无法直接在 Java 中使用 iText 和 Apache Velocity 模板文件 (.vm) 生成 PDF,因为:
- PDF is binary format,
- Velocity generates plain text content.
- PDF是二进制格式,
- Velocity 生成纯文本内容。
On other words, Velocity cannot generate PDF.
换句话说,Velocity 无法生成 PDF。
XDocReport is able to generate a docx/odt report by merging a docx/odt template which contains some Velocity/Freemarker syntax with Java context. The generated docx/odt report can be convert it to pdf/xhtml.
XDocReport 能够通过将包含一些 Velocity/Freemarker 语法的 docx/odt 模板与 Java 上下文合并来生成 docx/odt 报告。生成的 docx/odt 报告可以转换为 pdf/xhtml。
It works because docx/odt are a zip which contains several xml entries. If you unzip a docx you will see word/document.xml. In this entry, you will see the content that you have typed with MS Word. word/document.xml is a plain text, so Velocity can be used in this case.
它有效是因为 docx/odt 是一个包含多个 xml 条目的 zip。如果您解压缩 docx,您将看到 word/document.xml。在此条目中,您将看到使用 MS Word 键入的内容。word/document.xml 是纯文本,所以在这种情况下可以使用 Velocity。
Here the XDocReport process to generate pdf from a docx template which uses Velocity:
这里是从使用 Velocity 的 docx 模板生成 pdf 的 XDocReport 过程:
- Load docx template. this step consist to unzip the docx and stores in a map each xml entries (name entry as key and byte array as value). For instance map contains a key with word/document.xml and the xml content of this entry as value.
- Loop for each xml entries which must be merged with Java context. For instance word/document.xml is merged with Java context by using Velocity and the result of merge replace the word/document.xml value of the map
- Rebuild a new docx by zipping each entries of the map.
- 加载 docx 模板。此步骤包括解压缩 docx 并将每个 xml 条目(名称条目作为键和字节数组作为值)存储在映射中。例如 map 包含一个带有 word/document.xml 的键和这个条目的 xml 内容作为值。
- 循环每个必须与 Java 上下文合并的 xml 条目。例如 word/document.xml 通过使用 Velocity 与 Java 上下文合并,合并的结果替换地图的 word/document.xml 值
- 通过压缩地图的每个条目重建一个新的 docx。
At this step we have a generated docx (the report).
在这一步,我们有一个生成的 docx(报告)。
To convert it to another format, XDocReport provides a docx-to-pdf converter based on Apache POI and iText. Here the XDocReport process to convert a docx to pdf:
为了将其转换为另一种格式,XDocReport 提供了一个基于 Apache POI 和 iText 的 docx-to-pdf 转换器。这里是将 docx 转换为 pdf 的 XDocReport 过程:
- Load docx with Apache POI
- Loop for each structures of POI (XWPFParagraph, etc.) to create iText structure (iText Paragraph).
- 使用 Apache POI 加载 docx
- 对 POI 的每个结构(XWPFParagraph 等)循环创建 iText 结构(iText Paragraph)。
Note that XDocReport is modular and you can use other converters as well.
请注意,XDocReport 是模块化的,您也可以使用其他转换器。
回答by Prasad Paravatha
Yes, you can. It all depends on how complex you want the PDFs to be.
是的你可以。这完全取决于您希望 PDF 的复杂程度。
Here are the steps for basic functionality
以下是基本功能的步骤
- Generate a HTML file using Apache Velocity template file (.vm).
- Use
com.itextpdf.text.html.simpleparser.HTMLWorker
(deprecated) to parse/convert that HTML file into a PDF. - Additionally, you can use
com.itextpdf.text.pdf.PdfCopy.PageStamp
to add content (borders, stamps, notes, annotations etc) to an existing PDF.
- 使用 Apache Velocity 模板文件 (.vm) 生成 HTML 文件。
- 使用
com.itextpdf.text.html.simpleparser.HTMLWorker
(已弃用)将该 HTML 文件解析/转换为 PDF。 - 此外,您可以使用
com.itextpdf.text.pdf.PdfCopy.PageStamp
向现有 PDF 添加内容(边框、图章、注释、注释等)。
There is also com.itextpdf.tool.xml.XMLWorker
for more advanced HTML conversion (adding style sheets etc)
还有com.itextpdf.tool.xml.XMLWorker
更高级的 HTML 转换(添加样式表等)
回答by Mark Ma
At first,we use freemarker template to generate a html file,and then render html to a pdf file by IItextRender .Finally, we can view pdf file in browser,there has a very useful javascript tools called pdfjs. Maybe you can try it.
首先,我们使用freemarker模板生成一个html文件,然后通过IItextRender将html渲染成pdf文件。最后,我们可以在浏览器中查看pdf文件,有一个非常有用的javascript工具叫做pdfjs。也许你可以试试。