doumiao0498 2010-04-28 19:46
浏览 24

Apache FOP生成空白页面


I am trying to generate a PDF using Apache FOP and Java. I am using a valid xsl-fo file which I can create a pdf with using the command line FOP.

My problem occurs when I try to run FOP using the Apache FOP Libraries. Running across a java/php bridge. The bride is properly configured and java / php communicate. On the java side I have a function that takes in a string containing xsl-fo and returns a String which contains a pdf. When I execute this function and redirect the output to stdout then to file, or across the java / php bridge, the pdf appears blank and its size is roughly double that of the correct pdf that I retrieve via command line. I assume I am having some kind of encoding problem.

Has anyone seen this issue before?

Here is my java code

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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
    • ¥15 不小心不正规的开发公司导致不给我们y码,
    • ¥15 我的代码无法在vc++中运行呀,错误很多
    • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
    • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
    • ¥30 ppOCRLabel导出识别结果失败
    • ¥15 Centos7 / PETGEM
    • ¥15 csmar数据进行spss描述性统计分析
    • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
    • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题