java 从 XML 或 HTML 生成 PDF 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1777342/
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
PDF file generation from XML or HTML
提问by Venkat Sadasivam
Are there any API/solution to generate PDF report from XML file data and definition. For example the XML definition/data could be:
是否有任何 API/解决方案可以从 XML 文件数据和定义生成 PDF 报告。例如,XML 定义/数据可以是:
<pdf>
<paragraph font="Arial">Title of report</paragraph>
</pdf>
Converting HTML to PDF will be also a good solution I feel.
将 HTML 转换为 PDF 也会是我觉得不错的解决方案。
Currently we write Java code using iText API. I want to externalize the code so that non-technical person can edit and make changes.
目前我们使用 iText API 编写 Java 代码。我想将代码具体化,以便非技术人员可以进行编辑和更改。
回答by Pascal Thivent
Have a look at Apache FOP. Use an XSLT stylesheet to convert the XML (or XHTML) into XSL-FO. Then use FOP to read the XSL-FO document and format it to a PDF document (see Hello World with FOP).
看看Apache FOP。使用 XSLT 样式表将 XML(或 XHTML)转换为 XSL-FO。然后使用 FOP 读取 XSL-FO 文档并将其格式化为 PDF 文档(请参阅Hello World with FOP)。
Apache FOP can use a lot of memory for large documents (e.g., a 200-page PDF), which may require tweaking the JVM memory settings.
Apache FOP 可以为大型文档(例如,200 页的 PDF)使用大量内存,这可能需要调整JVM 内存设置。
回答by jt.
iTexthas a facility for generating PDFs from XML (and HTML, I think). Here is the DTD, but I found it difficult to sort out. Aside from that, I never found any good documentation on what is supported. My approach was to look at the source for SAXiTextHandlerand ElementTagsto figure out what was acceptable. Although not ideal, it is pretty straight-forward.
iText具有从 XML(和 HTML,我认为)生成 PDF 的工具。这是 DTD,但我发现很难理清。除此之外,我从未找到任何关于支持内容的好的文档。我的方法是查看SAXiTextHandler和ElementTags的来源,以确定可接受的内容。虽然不理想,但它非常简单。
<itext orientation="portrait" pagesize="LETTER" top="36" bottom="36" left="36" right="36" title="My Example" subject="My Subject" author="Me">
<paragraph size="8" >This is an example</paragraph>
</itext>
...
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.xml.SAXiTextHandler;
...
String inXml = ""; //use xml above as an example
ByteArrayOutputStream temp = new ByteArrayOutputStream();
Document document = new Document();
PdfWriter writer = null;
try
{
writer = PdfWriter.getInstance(document, temp);
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
parser.parse(new ByteArrayInputStream(inXml), new SAXiTextHandler(document));
}
catch (Exception e)
{
// instead, catch the proper exception and do something meaningful
e.printStackTrace();
}
finally
{
if (writer != null)
{
try
{
writer.close();
}
catch (Exception ignore)
{
// ignore
}
} // if
}
//temp holds the PDF
回答by medopal
Take a look at JasperReports, it uses iText to export files i think, and its IDE is simple and can be used by non-programmers.
看看JasperReports,我认为它使用 iText 导出文件,它的 IDE 很简单,非程序员也可以使用。
Edit: i forgot to mention, you can use JasperReports engine directly in your application, or you can use iReport "Designer for JasperReports"
编辑:我忘了提,你可以直接在你的应用程序中使用 JasperReports 引擎,或者你可以使用iReport “Designer for JasperReports”
回答by Thorbj?rn Ravn Andersen
You will want to use a well supported XML format for this, as it will allow you to leverage the work of others.
为此,您将需要使用受良好支持的 XML 格式,因为它可以让您利用其他人的工作。
A well supported XML format is DocBook XML - http://www.docbook.org/- and this - http://sagehill.net/docbookxsl/index.html- appears to be a good resource on doing the XML -> PDF using XSLT with the Docbook style sheets and other formats.
支持良好的 XML 格式是 DocBook XML - http://www.docbook.org/- 而这个 - http://sagehill.net/docbookxsl/index.html- 似乎是一个很好的 XML 资源 -> PDF将 XSLT 与 Docbook 样式表和其他格式一起使用。
This approach allows you to use any XSLT processor and any XSL/FO processor to get to your result. This gives you easy scriptability as well as the freedom to switch implementations if needed - notably older Apache FOP implementations degraded badly when the resulting PDF got "too large".
这种方法允许您使用任何 XSLT 处理器和任何 XSL/FO 处理器来获得结果。这使您可以轻松编写脚本并在需要时自由切换实现 - 特别是当生成的 PDF 变得“太大”时,较旧的 Apache FOP 实现会严重降级。
回答by Thorbj?rn Ravn Andersen
Princeis one of the best tools out there. It uses CSS for styles, so if you appreciate that method of separating data from display (read: your users are able to do that too), it may be a very good fit for you. (The control over display that browsers offer through CSS is, by comparision, primitive.)
Prince是目前最好的工具之一。它使用 CSS 作为样式,因此如果您喜欢这种将数据与显示分离的方法(阅读:您的用户也可以这样做),它可能非常适合您。(相比之下,浏览器通过 CSS 提供的对显示的控制是原始的。)

