XML a PDF usando FOP devuelve una página en blanco

I'm trying to convert an xml file to a pdf file using Apache FOP but I receive only a blank page, this is my code:

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;

}

Do you have any idea about the problem?

¡¡Gracias!!

preguntado el 28 de agosto de 12 a las 15:08

1 Respuestas

If you're getting null output then I think you're missing at the end something like:

pdfBytes = out.toByteArray();

If your output is not null (if you do have above) then you should provide contents of transformation.xsl maybe there's a problem.

Respondido 28 ago 12, 16:08

My out.pdf file is a blank page but if I make the transformation using Oxygen all works fine. I think that the problem is in the code. P.S. pdfBytes is null because firstly I want that the PDF generation works fine. - Diseño de marca

Do U have some static text or only dynamic based on report.xml? Are U using the same FOP version as Oxygen (I don't know this app, just read it comes with build in FOP)? Maybe youre local FOP conf isn't right? Any log output (warnings, error from FOP)? - Michał Bartnicki

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.