开发者

Apache FOP generating blank page

开发者 https://www.devze.com 2022-12-29 01:07 出处:网络
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.

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 tak开发者_JAVA百科es 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

精彩评论

暂无评论...
验证码 换一张
取 消