Apache FOP生成空白页面

时间:2010-04-28 19:46:48

标签: java php apache-fop


我正在尝试使用Apache FOP和Java生成PDF。我正在使用一个有效的xsl-fo文件,我可以使用命令行FOP创建一个pdf。

当我尝试使用Apache FOP库运行FOP时出现问题。在java / php桥上运行。新娘配置正确,java / php通信。在java方面,我有一个函数,它接受一个包含xsl-fo的字符串,并返回一个包含pdf的String。当我执行此函数并将输出重定向到stdout然后重定向到文件,或者跨越java / php桥时,pdf显示为空白,其大小大约是我通过命令行检索的正确pdf的两倍。我假设我遇到了某种编码问题。

之前有没有人见过这个问题?

这是我的java代码

public String ConvertFoToPdf(String fo) {

    // Will contain the results after the transformation.
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    // Input string
    StringReader sr = new StringReader(fo);

    // Should be UTF-8;
    String strEncoding = Charset.defaultCharset().name();

    // Resulting string.
    String pdfResult = "";

    try
    {
        // Get an instance of the fop factory
        FopFactory fopFactory = FopFactory.newInstance();
        FOUserAgent foUserAgent = fopFactory.newFOUserAgent();

        // Construct fop with desired output format
        Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);

        // Setup JAXP using identity transformer
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer(); 

        // Setup input stream
        Source src = new StreamSource(sr);

        // Resulting SAX events (the generated FO) must be piped through to FOP            
        Result res = new SAXResult(fop.getDefaultHandler());           

        // Set the encoding on the transformer.           
        transformer.setOutputProperty(OutputKeys.ENCODING, strEncoding); 

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

        // Put the byte array stream into a string
        pdfResult = out.toString(strEncoding);
    }

    // Catch all exceptions for simplicities sake.
    catch (Exception e){

        // Log        
    }

    return pdfResult;
}

0 个答案:

没有答案
相关问题