/** * We need to write text to the response Writer. * @param exporter the JasperReports exporter to use * @param populatedReport the populated {@code JasperPrint} to render * @param response the HTTP response the report should be rendered to * @throws Exception if rendering failed */ protected void renderReportUsingWriter( JRExporter exporter, JasperPrint populatedReport, HttpServletResponse response) throws Exception { // Copy the encoding configured for the report into the response. String contentType = getContentType(); String encoding = (String) exporter.getParameter(JRExporterParameter.CHARACTER_ENCODING); if (encoding != null) { // Only apply encoding if content type is specified but does not contain charset clause already. if (contentType != null && !contentType.toLowerCase().contains(WebUtils.CONTENT_TYPE_CHARSET_PREFIX)) { contentType = contentType + WebUtils.CONTENT_TYPE_CHARSET_PREFIX + encoding; } } response.setContentType(contentType); // Render report into HttpServletResponse's Writer. JasperReportsUtils.render(exporter, populatedReport, response.getWriter()); }
/** * We need to write text to the response Writer. * @param exporter the JasperReports exporter to use * @param populatedReport the populated {@code JasperPrint} to render * @param response the HTTP response the report should be rendered to * @throws Exception if rendering failed */ protected void renderReportUsingWriter(net.sf.jasperreports.engine.JRExporter exporter, JasperPrint populatedReport, HttpServletResponse response) throws Exception { // Copy the encoding configured for the report into the response. String contentType = getContentType(); String encoding = (String) exporter.getParameter(net.sf.jasperreports.engine.JRExporterParameter.CHARACTER_ENCODING); if (encoding != null) { // Only apply encoding if content type is specified but does not contain charset clause already. if (contentType != null && !contentType.toLowerCase().contains(WebUtils.CONTENT_TYPE_CHARSET_PREFIX)) { contentType = contentType + WebUtils.CONTENT_TYPE_CHARSET_PREFIX + encoding; } } response.setContentType(contentType); // Render report into HttpServletResponse's Writer. JasperReportsUtils.render(exporter, populatedReport, response.getWriter()); }
/** * @see org.kuali.kfs.sys.batch.service.ReportGenerationService#generateReportToOutputStream(java.util.Map, java.lang.Object, * java.lang.String, java.io.ByteArrayOutputStream) */ public void generateReportToOutputStream(Map<String, Object> reportData, Object dataSource, String template, ByteArrayOutputStream baos) { ClassPathResource resource = getReportTemplateClassPathResource(template.concat(ReportGeneration.DESIGN_FILE_EXTENSION)); if (resource == null || !resource.exists()) { throw new IllegalArgumentException("Cannot find the template file: " + template.concat(ReportGeneration.DESIGN_FILE_EXTENSION)); } try { if (reportData != null && reportData.containsKey(ReportGeneration.PARAMETER_NAME_SUBREPORT_TEMPLATE_NAME)) { Map<String, String> subReports = (Map<String, String>) reportData.get(ReportGeneration.PARAMETER_NAME_SUBREPORT_TEMPLATE_NAME); String subReportDirectory = (String) reportData.get(ReportGeneration.PARAMETER_NAME_SUBREPORT_DIR); compileSubReports(subReports, subReportDirectory); } String designTemplateName = template.concat(ReportGeneration.DESIGN_FILE_EXTENSION); InputStream jasperReport = new FileInputStream(compileReportTemplate(designTemplateName)); JRDataSource jrDataSource = JasperReportsUtils.convertReportData(dataSource); JasperRunManager.runReportToPdfStream(jasperReport, baos, decorateReportData(reportData), jrDataSource); } catch (Exception e) { LOG.error(e); throw new RuntimeException("Fail to generate report.", e); } }
/** * We need to write binary output to the response OutputStream. * @param exporter the JasperReports exporter to use * @param populatedReport the populated {@code JasperPrint} to render * @param response the HTTP response the report should be rendered to * @throws Exception if rendering failed */ protected void renderReportUsingOutputStream(net.sf.jasperreports.engine.JRExporter exporter, JasperPrint populatedReport, HttpServletResponse response) throws Exception { // IE workaround: write into byte array first. ByteArrayOutputStream baos = createTemporaryOutputStream(); JasperReportsUtils.render(exporter, populatedReport, baos); writeToResponse(response, baos); }
/** * The dataSource can be an instance of JRDataSource, java.util.Collection or object array. * * @see org.kuali.ole.sys.batch.service.ReportGenerationService#generateReportToPdfFile(java.util.Map, java.lang.Object, java.lang.String, * java.lang.String) */ public void generateReportToPdfFile(Map<String, Object> reportData, Object dataSource, String template, String reportFileName) { ClassPathResource resource = getReportTemplateClassPathResource(template); if (resource == null || !resource.exists()) { throw new IllegalArgumentException("Cannot find the template file: " + template); } try { if (reportData != null && reportData.containsKey(PARAMETER_NAME_SUBREPORT_TEMPLATE_NAME)) { Map<String, String> subReports = (Map<String, String>) reportData.get(PARAMETER_NAME_SUBREPORT_TEMPLATE_NAME); String subReportDirectory = (String) reportData.get(PARAMETER_NAME_SUBREPORT_DIR); compileSubReports(subReports, subReportDirectory); } String realTemplateNameWithoutExtension = removeTemplateExtension(resource); String designTemplateName = realTemplateNameWithoutExtension.concat(DESIGN_FILE_EXTENSION); String jasperReportName = realTemplateNameWithoutExtension.concat(JASPER_REPORT_EXTENSION); compileReportTemplate(designTemplateName, jasperReportName); JRDataSource jrDataSource = JasperReportsUtils.convertReportData(dataSource); reportFileName = reportFileName + PDF_FILE_EXTENSION; File reportDirectory = new File(StringUtils.substringBeforeLast(reportFileName, SEPARATOR)); if(!reportDirectory.exists()) { reportDirectory.mkdir(); } JasperRunManager.runReportToPdfFile(jasperReportName, reportFileName, reportData, jrDataSource); } catch (Exception e) { LOG.error(e); throw new RuntimeException("Fail to generate report.", e); } }
/** * @see org.kuali.ole.sys.batch.service.ReportGenerationService#generateReportToOutputStream(java.util.Map, java.lang.Object, * java.lang.String, java.io.ByteArrayOutputStream) */ public void generateReportToOutputStream(Map<String, Object> reportData, Object dataSource, String template, ByteArrayOutputStream baos) { ClassPathResource resource = getReportTemplateClassPathResource(template); if (resource == null || !resource.exists()) { throw new IllegalArgumentException("Cannot find the template file: " + template); } try { if (reportData != null && reportData.containsKey(PARAMETER_NAME_SUBREPORT_TEMPLATE_NAME)) { Map<String, String> subReports = (Map<String, String>) reportData.get(PARAMETER_NAME_SUBREPORT_TEMPLATE_NAME); String subReportDirectory = (String) reportData.get(PARAMETER_NAME_SUBREPORT_DIR); compileSubReports(subReports, subReportDirectory); } String realTemplateNameWithoutExtension = removeTemplateExtension(resource); String designTemplateName = realTemplateNameWithoutExtension.concat(DESIGN_FILE_EXTENSION); String jasperReportName = realTemplateNameWithoutExtension.concat(JASPER_REPORT_EXTENSION); compileReportTemplate(designTemplateName, jasperReportName); JRDataSource jrDataSource = JasperReportsUtils.convertReportData(dataSource); InputStream inputStream = new FileInputStream(jasperReportName); JasperRunManager.runReportToPdfStream(inputStream, (OutputStream) baos, reportData, jrDataSource); } catch (Exception e) { LOG.error(e); throw new RuntimeException("Fail to generate report.", e); } }
/** * We need to write binary output to the response OutputStream. * @param exporter the JasperReports exporter to use * @param populatedReport the populated {@code JasperPrint} to render * @param response the HTTP response the report should be rendered to * @throws Exception if rendering failed */ protected void renderReportUsingOutputStream( JRExporter exporter, JasperPrint populatedReport, HttpServletResponse response) throws Exception { // IE workaround: write into byte array first. ByteArrayOutputStream baos = createTemporaryOutputStream(); JasperReportsUtils.render(exporter, populatedReport, baos); writeToResponse(response, baos); }
/** * The dataSource can be an instance of JRDataSource, java.util.Collection or object array. * * @see org.kuali.kfs.sys.batch.service.ReportGenerationService#generateReportToPdfFile(java.util.Map, java.lang.Object, java.lang.String, * java.lang.String) */ public void generateReportToPdfFile(Map<String, Object> reportData, Object dataSource, String template, String reportFileName) { ClassPathResource resource = getReportTemplateClassPathResource(template.concat(ReportGeneration.DESIGN_FILE_EXTENSION)); if (resource == null || !resource.exists()) { throw new IllegalArgumentException("Cannot find the template file: " + template.concat(ReportGeneration.DESIGN_FILE_EXTENSION)); } try { if (reportData != null && reportData.containsKey(ReportGeneration.PARAMETER_NAME_SUBREPORT_TEMPLATE_NAME)) { Map<String, String> subReports = (Map<String, String>) reportData.get(ReportGeneration.PARAMETER_NAME_SUBREPORT_TEMPLATE_NAME); String subReportDirectory = (String) reportData.get(ReportGeneration.PARAMETER_NAME_SUBREPORT_DIR); compileSubReports(subReports, subReportDirectory); } String designTemplateName = template.concat(ReportGeneration.DESIGN_FILE_EXTENSION); InputStream jasperReport = new FileInputStream(compileReportTemplate(designTemplateName)); JRDataSource jrDataSource = JasperReportsUtils.convertReportData(dataSource); reportFileName = reportFileName + ReportGeneration.PDF_FILE_EXTENSION; File reportDirectory = new File(StringUtils.substringBeforeLast(reportFileName, File.separator)); if(!reportDirectory.exists()) { reportDirectory.mkdir(); } JasperRunManager.runReportToPdfStream(jasperReport, new FileOutputStream(reportFileName), decorateReportData(reportData), jrDataSource); } catch (Exception e) { LOG.error(e); throw new RuntimeException("Fail to generate report.", e); } }
/** * Convert the given report data value to a {@code JRDataSource}. * <p>The default implementation delegates to {@code JasperReportUtils} unless * the report data value is an instance of {@code JRDataSourceProvider}. * A {@code JRDataSource}, {@code JRDataSourceProvider}, * {@code java.util.Collection} or object array is detected. * {@code JRDataSource}s are returned as is, whilst {@code JRDataSourceProvider}s * are used to create an instance of {@code JRDataSource} which is then returned. * The latter two are converted to {@code JRBeanCollectionDataSource} or * {@code JRBeanArrayDataSource}, respectively. * @param value the report data value to convert * @return the JRDataSource * @throws IllegalArgumentException if the value could not be converted * @see org.springframework.ui.jasperreports.JasperReportsUtils#convertReportData * @see net.sf.jasperreports.engine.JRDataSource * @see net.sf.jasperreports.engine.JRDataSourceProvider * @see net.sf.jasperreports.engine.data.JRBeanCollectionDataSource * @see net.sf.jasperreports.engine.data.JRBeanArrayDataSource */ protected JRDataSource convertReportData(Object value) throws IllegalArgumentException { if (value instanceof JRDataSourceProvider) { return createReport((JRDataSourceProvider) value); } else { return JasperReportsUtils.convertReportData(value); } }