Apache FOP XML - XLS-FO 生成无效的 pdf [英] Apache FOP XML - XLS-FO generates invalid pdf
问题描述
我正在尝试使用 Apache FOP 从使用 xlst 样式表格式化的 xml 文件创建 PDF 文档,以将原始 xml 文件转换为 xml-fo 格式的 xml 文件.由于我是新手,我尝试创建一个简单的 hello world 示例,但没有成功.
I'm trying to create a PDF document with Apache FOP from an xml file that is formatted with an xlst stylesheet to convert the original xml file to an xml-fo formatted xml file. As I'm new to this, I tried to create a simple hello world example, without success.
生成过程好像成功了(无一例外)但是生成的pdf文件由于某种原因无效.文件大小为4.8KB,用libreoffice writer打开时,确实有数据写入文件,但pdf阅读器打不开.
The generation process seems to succeed (no exceptions) but the generated pdf file is invalid for some reason. The size of the file is 4.8KB, and when opened with libreoffice writer, data has definitely been written to the file, but the file does not open in pdf readers.
XML 文件相当简单:
The XML file is rather straightforward:
<rentalRequest>
<firstName>foo</firstName>
<lastName>bar</lastName>
<email>foo@bar.com</email>
<street>foo street</street>
<houseNo>42</houseNo>
<postalCode>4242</postalCode>
<city>bar city</city>
</rentalRequest>
XSL 文件,只是尝试打印 Hello World!:
The XSL file, simply attempting to print Hello World!:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="all">
<fo:region-body />
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="all">
<fo:flow flow-name="xsl-region-body">
<fo:block>
Hello World!
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
</xsl:stylesheet>
使用JAXP + FOP生成pdf文件的java代码:
The java code to generate a pdf file using JAXP + FOP:
public void buildWithXSL(String xml) throws Exception {
OutputStream out = new BufferedOutputStream(new FileOutputStream(new File("rentalrequest.pdf")));
// setup xml input source
StreamSource xmlSource =
new StreamSource(new ByteArrayInputStream(xml.getBytes()));
File xslFile = new File("src/main/resources/xml/rentalrequest2fo.xsl");
FileInputStream xslFileStream = new FileInputStream(xslFile);
StreamSource xslSource = new StreamSource(xslFileStream);
TransformerFactory tfactory = TransformerFactory.newInstance();
Transformer transformer = tfactory.newTransformer(xslSource);
FopFactory fopFactory = FopFactory.newInstance();
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
// perform transformation
Result res = new SAXResult(fop.getDefaultHandler());
transformer.transform(xmlSource, res);
}
生成的pdf文件可以在http://www.filedropper.com/rentalrequest
The generated pdf file can be found at http://www.filedropper.com/rentalrequest
推荐答案
您的 PDF 文件不完整,因为您忘记正确关闭
OutputStream
.当您打开 OutputStream
(或 InputStream
)时,请始终使用以下模式:
You got an incomplete PDF file because you forgot to properly close
the OutputStream
. Always use the following pattern when you open an OutputStream
(or InputStream
for that matter):
OutputStream out = new [Something]OutputStream([something]);
try {
//write to the OutputStream
} finally {
out.close();
}
这篇关于Apache FOP XML - XLS-FO 生成无效的 pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!