Search in sources :

Example 1 with JRPdfExporter

use of net.sf.jasperreports.engine.export.JRPdfExporter in project bamboobsc by billchen198318.

the class JReportUtils method fillReportToResponse.

public static void fillReportToResponse(String reportId, Map<String, Object> paramMap, HttpServletResponse response) throws ServiceException, Exception {
    if (StringUtils.isBlank(reportId)) {
        throw new java.lang.IllegalArgumentException("error, reportId is blank");
    }
    TbSysJreport sysJreport = new TbSysJreport();
    sysJreport.setReportId(reportId);
    DefaultResult<TbSysJreport> result = sysJreportService.findEntityByUK(sysJreport);
    if (result.getValue() == null) {
        throw new ServiceException(result.getSystemMessage().getValue());
    }
    sysJreport = result.getValue();
    String jasperFileFullPath = Constants.getDeployJasperReportDir() + "/" + sysJreport.getReportId() + "/" + sysJreport.getReportId() + ".jasper";
    File jasperFile = new File(jasperFileFullPath);
    if (!jasperFile.exists()) {
        jasperFile = null;
        throw new Exception("error, Files are missing : " + jasperFileFullPath);
    }
    InputStream reportSource = new FileInputStream(jasperFile);
    Connection conn = null;
    try {
        conn = DataUtils.getConnection();
        ServletOutputStream ouputStream = response.getOutputStream();
        JasperPrint jasperPrint = JasperFillManager.fillReport(reportSource, paramMap, conn);
        response.setContentType("application/pdf");
        response.setHeader("Content-disposition", "inline; filename=" + sysJreport.getReportId() + ".pdf");
        JRPdfExporter jrPdfExporter = new JRPdfExporter();
        jrPdfExporter.setExporterInput(new SimpleExporterInput(jasperPrint));
        jrPdfExporter.setExporterOutput(new SimpleOutputStreamExporterOutput(ouputStream));
        SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
        jrPdfExporter.setConfiguration(configuration);
        configuration.setOwnerPassword(Constants.getEncryptorKey1());
        jrPdfExporter.exportReport();
        ouputStream.flush();
        ouputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        DataUtils.doReleaseConnection(conn);
        if (null != reportSource) {
            try {
                reportSource.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        reportSource = null;
        jasperFile = null;
    }
}
Also used : TbSysJreport(com.netsteadfast.greenstep.po.hbm.TbSysJreport) ServletOutputStream(javax.servlet.ServletOutputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SimpleOutputStreamExporterOutput(net.sf.jasperreports.export.SimpleOutputStreamExporterOutput) JasperPrint(net.sf.jasperreports.engine.JasperPrint) Connection(java.sql.Connection) SimpleExporterInput(net.sf.jasperreports.export.SimpleExporterInput) JRPdfExporter(net.sf.jasperreports.engine.export.JRPdfExporter) IOException(java.io.IOException) JRException(net.sf.jasperreports.engine.JRException) IOException(java.io.IOException) ServiceException(com.netsteadfast.greenstep.base.exception.ServiceException) FileInputStream(java.io.FileInputStream) ServiceException(com.netsteadfast.greenstep.base.exception.ServiceException) SimplePdfExporterConfiguration(net.sf.jasperreports.export.SimplePdfExporterConfiguration) ZipFile(net.lingala.zip4j.core.ZipFile) File(java.io.File)

Example 2 with JRPdfExporter

use of net.sf.jasperreports.engine.export.JRPdfExporter in project midpoint by Evolveum.

the class JasperReports method getData.

public static byte[] getData(JasperPrint print) {
    JRPdfExporter exporter = new JRPdfExporter();
    exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
    exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, os);
    try {
        exporter.exportReport();
    } catch (JRException e) {
        e.printStackTrace();
    }
    return os.toByteArray();
}
Also used : JRException(net.sf.jasperreports.engine.JRException) JRPdfExporter(net.sf.jasperreports.engine.export.JRPdfExporter) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 3 with JRPdfExporter

use of net.sf.jasperreports.engine.export.JRPdfExporter in project dhis2-core by dhis2.

the class JRExportUtils method export.

/**
     * Export the provided JasperPrint the format given by type.
     *
     * @param type the type to export to. XLS, PDF and HTML are supported.
     * @param out the OutputStream to export to.
     * @param jasperPrint the JasperPrint to export.
     * @throws JRException on export failure.
     */
public static void export(String type, OutputStream out, JasperPrint jasperPrint) throws JRException {
    if (TYPE_XLS.equals(type)) {
        SimpleXlsReportConfiguration config = new SimpleXlsReportConfiguration();
        config.setDetectCellType(true);
        config.setRemoveEmptySpaceBetweenRows(true);
        config.setRemoveEmptySpaceBetweenRows(true);
        config.setCollapseRowSpan(true);
        config.setWhitePageBackground(false);
        JRXlsExporter exporter = new JRXlsExporter();
        exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
        exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(out));
        exporter.setConfiguration(config);
        exporter.exportReport();
    } else if (TYPE_PDF.equals(type)) {
        JRPdfExporter exporter = new JRPdfExporter();
        exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
        exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(out));
        exporter.exportReport();
    }
}
Also used : SimpleXlsReportConfiguration(net.sf.jasperreports.export.SimpleXlsReportConfiguration) JRXlsExporter(net.sf.jasperreports.engine.export.JRXlsExporter) SimpleOutputStreamExporterOutput(net.sf.jasperreports.export.SimpleOutputStreamExporterOutput) SimpleExporterInput(net.sf.jasperreports.export.SimpleExporterInput) JRPdfExporter(net.sf.jasperreports.engine.export.JRPdfExporter)

Aggregations

JRPdfExporter (net.sf.jasperreports.engine.export.JRPdfExporter)3 JRException (net.sf.jasperreports.engine.JRException)2 SimpleExporterInput (net.sf.jasperreports.export.SimpleExporterInput)2 SimpleOutputStreamExporterOutput (net.sf.jasperreports.export.SimpleOutputStreamExporterOutput)2 ServiceException (com.netsteadfast.greenstep.base.exception.ServiceException)1 TbSysJreport (com.netsteadfast.greenstep.po.hbm.TbSysJreport)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Connection (java.sql.Connection)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 ZipFile (net.lingala.zip4j.core.ZipFile)1 JasperPrint (net.sf.jasperreports.engine.JasperPrint)1 JRXlsExporter (net.sf.jasperreports.engine.export.JRXlsExporter)1 SimplePdfExporterConfiguration (net.sf.jasperreports.export.SimplePdfExporterConfiguration)1 SimpleXlsReportConfiguration (net.sf.jasperreports.export.SimpleXlsReportConfiguration)1