Apache FOP XML-XLS-FO生成无效的pdf [英] Apache FOP XML - XLS-FO generates invalid pdf

查看:145
本文介绍了Apache FOP XML-XLS-FO生成无效的pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Apache FOP从XML文件创建PDF文档,该XML文件使用xlst样式表进行格式设置,以将原始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文件不完整,是因为您忘记正确地close 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆