JFreeReportのwebレポート生成pdfの実装



 
 
import java.io.IOException;
import java.net.URL;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jfree.report.Boot;
import org.jfree.report.JFreeReport;
import org.jfree.report.modules.output.pageable.base.PageableReportProcessor;
import org.jfree.report.modules.output.pageable.pdf.PDFOutputTarget;
import org.jfree.report.modules.parser.base.ReportGenerator;
import org.jfree.report.util.Log;
import org.jfree.report.util.ReportConfiguration;
import org.jfree.xml.ElementDefinitionException;

/**
 * 
 * <p>JFreeReport pdf 。<p>
 *
 *   2013-5-13<br>
 * @author  longgangbai<br>
 * @version $Revision$ 2013-5-13
 * @since   3.0.0
 */
public class JFreeReportServlet extends HttpServlet implements Servlet {
    //
    private static final long serialVersionUID = 1L;

    /**
    * @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
    */
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/pdf");
        ServletOutputStream out = resp.getOutputStream();
        try {
            // initialize JFreeReport
            Boot.start();
            ReportConfiguration.getGlobalConfig().setLogLevel("Error");
            // update the log system to use the new settings ...
            Log.getJFreeReportLog().init();
            final URL in = getServletContext().getResource("/resources/swing-icons.xml");
            if (in == null) {
                throw new Exception("swing-icons.xml can't be found.");
            }
            final JFreeReport report = parseReport(in);
            report.setData(new com.easyway.jfreereport.ext.SwingIconsDemoTableModel());
            PDFOutputTarget target = new PDFOutputTarget(out, report.getDefaultPageFormat(), false);
            target.configure(report.getReportConfiguration());
            target.open();
            PageableReportProcessor proc = new PageableReportProcessor(report);
            proc.setOutputTarget(target);
            proc.processReport();
            target.close();
        } catch (Exception ex) {
            System.err.println("Generate PDF failed: " + ex);
        } finally {
            try {
                out.close();
            } catch (Exception e) {
                System.err.println("Output PDF failed: " + e);
            }
        }
    }

    /**
    * @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
    */
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    /**
     * Reads the report from the swing-icons.xml report template.
     *
     * @param templateURL The template location.
     *
     * @return A report.
     * @throws ElementDefinitionException if the report generator encountered an error.
     * @throws IOException if there was an IO error while reading from the URL.
     */
    private JFreeReport parseReport(final URL templateURL) throws IOException, ElementDefinitionException {
        final ReportGenerator generator = ReportGenerator.getInstance();
        return generator.parseReport(templateURL);
    }
}