如何在 Java 中从 XML 创建 PDF?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/212577/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:27:14  来源:igfitidea点击:

How do you create a PDF from XML in Java?

javaxmlpdfxsltitext

提问by Philip Morton

At the moment, I'm creating an XML file in Java and displaying it in a JSP page by transforming it with XSL/XSLT. Now I need to take that XML file and display the same information in a PDF. Is there a way I can do this by using some kind of XSL file?

目前,我正在用 Java 创建一个 XML 文件,并通过使用 XSL/XSLT 对其进行转换将其显示在 JSP 页面中。现在我需要获取该 XML 文件并在 PDF 中显示相同的信息。有没有办法通过使用某种 XSL 文件来做到这一点?

I've seen the iTextJava-PDF library, but I can't find any way to use it with XML and a stylesheet.

我已经看过iTextJava-PDF 库,但我找不到任何方法将它与 XML 和样式表一起使用。

Any assistance would be much appreciated. Thanks in advance!

任何帮助将不胜感激。提前致谢!

回答by Mitchel Sellers

You might want to look at the XSL-FO libraries that are out there that can do PDF creation as a transformation. I'll try to find a link.

您可能想查看可以将 PDF 创建作为转换的 XSL-FO 库。我会试着找到一个链接。

回答by jeff porter

BIRThas a GUI for Eclipse that lets you define the PDF from XML, DB, CSV, etc etc.

BIRT有一个用于 Eclipse 的 GUI,允许您从 XML、DB、CSV 等定义 PDF。

回答by hakan

You can also check the Apache FOP Project here

您还可以在此处查看 Apache FOP 项目

回答by raffimd

Use JasperReports. You can either pull the data from Database or XML. You can export to many formats : pdf, excel, html, etc...

使用 JasperReports。您可以从数据库或 XML 中提取数据。您可以导出为多种格式:pdf、excel、html 等...

回答by Dean J

Coming in late, you can create a static PDF with Adobe's designer with editable fields, then create a matching XDP XML document.

稍后,您可以使用 Adob​​e 的设计器创建带有可编辑字段的静态 PDF,然后创建匹配的 XDP XML 文档。

回答by Shriram Kalpathy Mohan

There are two ways to do this.

有两种方法可以做到这一点。

  • Firstly, you can create a normal PDF which when read back will not give you the hierarchy of the original XML file. This is explained very elaborately in 'Section 9.4.2 Parsing XML'of the 'iText in Action : Edition 2'.

  • Secondly, you can create a tagged PDF which contains both the hierarchy of the XML as well as the data. This enables you to read back the PDF file and create an XML file from this(which exactly matches the original XML file). This concept is also dealt with in detail in '15.2.3 Adding structure'of the 'iText in Action : Edition 2'.

  • 首先,您可以创建一个普通的 PDF,它在回读时不会为您提供原始 XML 文件的层次结构。这'Section 9.4.2 Parsing XML''iText in Action : Edition 2'.

  • 其次,您可以创建一个标记的 PDF,其中包含 XML 的层次结构以及数据。这使您能够读回 PDF 文件并从中创建一个 XML 文件(与原始 XML 文件完全匹配)。这个概念也被作了详细论述'15.2.3 Adding structure''iText in Action : Edition 2'

Based on your requirements, you can use either of the approaches mentioned above.

根据您的要求,您可以使用上述任何一种方法。

回答by Yaroslav

You can apply XSL-Fo to your XML and transform it with Java transformer:

您可以将 XSL-Fo 应用于您的 XML 并使用 Java 转换器对其进行转换:

File xmlfile = new File(baseDir, xml);
File xsltfile = new File(baseDir, xsl);
File pdffile = new File(outDir, "ResultXMLPDF.pdf");

FopFactory fopFactory = FopFactory.newInstance();
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();

OutputStream out = new java.io.FileOutputStream(pdffile);
out = new java.io.BufferedOutputStream(out);

try
{
    Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
    // Setup XSLT
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer(new StreamSource(xsltfile));

    transformer.setParameter("versionParam", "1.0");

    Source src = new StreamSource(xmlfile);

    Result res = new SAXResult(fop.getDefaultHandler());

    transformer.transform(src, res);

} finally {
    out.close();
}

System.out.println("Success!");

回答by Peter Krauss

XML, CSS, XHTML, etc. consist in an "alive ecosystem" of open standards, while XSL-FOis an isolated standard.

XML、CSS、XHTML 等包含在开放标准的“活跃生态系统”中,而XSL-FO是一个孤立的标准。

... Historically XSL-FO and XSLT was created as twins brothers, but only XSLT remains an "alive standard", XSL-FO concentrates a lot of DNA in proprietary (Adobe) standards... now is obsolete.

... 从历史上看,XSL-FO 和 XSLT 是作为孪生兄弟创建的,但只有 XSLT 仍然是“活的标准”,XSL-FO 将大量 DNA 集中在专有 (Adobe) 标准中...现在已经过时了。

Strictly speaking, XSL-FO is part of an "abandoned way" that will not evolve, it ignores CSS, the "new way" to express layout in the "alive ecosystem".

严格来说,XSL-FO 是一种不会进化的“废弃方式”的一部分,它忽略了 CSS,一种在“活着的生态系统”中表达布局的“新方式”。

It is not a Java problem

这不是 Java 问题

See this answerabout the use of CSS-pagewith XML or XHTML.

请参阅有关将CSS 页面与 XML 或 XHTML 结合使用的答案