1

Apache FOP を使用して xml ファイルを pdf ファイルに変換しようとしていますが、空白のページしか表示されません。これが私のコードです。

protected byte[] buildPDF(byte[] xml) throws IOException {
    FileOutputStream fos;
    byte[] pdfBytes = null;

    try{

        // Setup input and output files
        File xmlfile = new File(MULTIMEDIA_PATH + File.separator + "xml/report.xml");
        File xsltfile = new File(MULTIMEDIA_PATH + File.separator + "xml/transformation.xsl");

        fos = new FileOutputStream(xmlfile);
        fos.write(xml);
        fos.close();

        // Step 1: Construct a FopFactory
        FopFactory fopFactory = FopFactory.newInstance();
        FOUserAgent foUserAgent = fopFactory.newFOUserAgent();

        // Step 2: Setup output stream
        OutputStream out = new BufferedOutputStream(new FileOutputStream((MULTIMEDIA_PATH + File.separator + "xml/result.pdf")));

        try {
            // Step 3: Construct FOP with desired output format
            Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);

            // Step 4: Setup JAXP using identity transformer
            Source xslt = new StreamSource(xsltfile);
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer(xslt);

            // Step 5: Setup input and output for XSLT transformation
            Source src = new StreamSource(xmlfile);
            Result res = new SAXResult(fop.getDefaultHandler());

            // Step 6: Start XSLT transformation and FOP processing
            transformer.transform(src, res);

        } finally {
            out.close();
        }

    } catch (Exception e) {
        e.printStackTrace(System.err);
        System.exit(-1);
    }       

    return pdfBytes;

}

問題について何か考えはありますか?

ありがとう!!

4

1 に答える 1

0

null 出力を取得している場合は、最後に次のようなものが欠けていると思います。

pdfBytes = out.toByteArray();

出力が null でない場合 (上記の場合)、transformation.xsl の内容を提供する必要があります。問題がある可能性があります。

于 2012-08-28T15:16:18.493 に答える