Apache FOP 生成空白页

发布于 2024-08-30 20:25:12 字数 1939 浏览 3 评论 0原文


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

当我尝试使用 Apache FOP 库运行 FOP 时,出现问题。跨 java/php 桥运行。新娘正确配置并与java / php进行通信。在java方面,我有一个函数,它接受包含xsl-fo的字符串并返回包含pdf的字符串。当我执行此函数并将输出重定向到 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;
}

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文