元のxmlファイルをxml-fo形式のxmlファイルに変換するために、xlstスタイルシートでフォーマットされたxmlファイルからApache FOPでPDFドキュメントを作成しようとしています。私はこれに慣れていないので、簡単なハローワールドの例を作成しようとしましたが、成功しませんでした。
生成プロセスは成功したように見えますが (例外はありません)、生成された pdf ファイルは何らかの理由で無効です。ファイルのサイズは4.8KBで、libreoffice writerで開くと確かにファイルにデータが書き込まれていますが、pdfリーダーではファイルが開きません。
XML ファイルはかなり単純です。
<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>
Hello World! を印刷しようとしている XSL ファイル:
<?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 コード:
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にあります。