Code Snippet

Just another Code Snippet site

[Java] Generate PDF report using FOP (XSL-T/XSL-FO)

    /**
     * Generate a PDF report using FOP Library.
     * @param xstlIn XSL-FO file use to transform data.
     * @param content Data
     * @param response HttpServletResponse used to sendback PDF report
     */
    public void generatePdf(final InputStream xstlIn, final StringWriter content, final HttpServletResponse response) {

        OutputStream out = null;
        try {
            FopFactory fopFactory = FopFactory.newInstance();

            // Setup input
            LOGGER.debug("Setup input");
            byte[] bytes = content.toString().getBytes("UTF-8");
            Source src = new StreamSource(new ByteArrayInputStream(bytes));

            // Setup Transformer
            LOGGER.debug("Setup Transformer");
            TransformerFactory tFactory = TransformerFactory.newInstance();
            Transformer transformer = tFactory.newTransformer(new StreamSource(xstlIn));

            // Reset/Re-Initialize HttpResponse
            LOGGER.debug("Reset HttpResponse");
            response.reset();
            response.setContentType("application/force-download");
            response.setHeader("Content-Transfer-Encoding", "binary");
            response.setHeader("Content-Disposition", "attachment; filename=\report.pdf\"");
            response.setContentLength(-1); // This will cause the connector to stream the file chunk by chunk.

            // Init outputstream
            LOGGER.debug("Init OutputStream");
            out = new BufferedOutputStream(response.getOutputStream());

            // Setup FOP
            LOGGER.debug("Setup FOP");
            Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);

            // Make sure the XSL transformation's result is piped through to FOP
            LOGGER.debug("Create SAXResult");
            Result res = new SAXResult(fop.getDefaultHandler());

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

        } catch (TransformerConfigurationException e) {
            // TODO : handle exception
        } catch (TransformerException e) {
            // TODO : handle exception
            e.printStackTrace();
        } catch (FOPException e) {
            // TODO : handle exception
            e.printStackTrace();
        } catch (IOException e) {
            // TODO : handle exception
            e.printStackTrace();
        } finally {
            // TODO : close stream
        }
    }

Dependencies :

		<dependency>
			<groupId>org.apache.xmlgraphics</groupId>
			<artifactId>fop</artifactId>
			<version>1.0</version>
			<exclusions>
				<exclusion>
					<artifactId>xml-apis-ext</artifactId>
					<groupId>xml-apis</groupId>
				</exclusion>
				<exclusion>
					<artifactId>xml-apis</artifactId>
					<groupId>xml-apis</groupId>
				</exclusion>
			</exclusions>
		</dependency>

, , ,


Leave a Reply

Your email address will not be published. Required fields are marked *