@Override public byte[] runReport() throws Exception { List<byte[]> byteList = generateReports(); if (byteList.isEmpty()) { HashMap<String, String> parameterMap = new HashMap<String, String>(); parameterMap.put("SUBREPORT_DIR", reportPath); parameterMap.put("directorName", ConfigurationProperties.getInstance().getPropertyValue(Property.labDirectorName)); List<ErrorMessages> errorMsgs = new ArrayList<ErrorMessages>(); ErrorMessages msgs = new ErrorMessages(); msgs.setMsgLine1(StringUtil.getMessageForKey("report.error.message.noPrintableItems")); errorMsgs.add(msgs); return JasperRunManager.runReportToPdf(reportPath + "NoticeOfReportError.jasper", parameterMap, new JRBeanCollectionDataSource( errorMsgs)); } else { return merge(byteList); } }
private ByteArrayOutputStream generateHsfoRxPDF(HttpServletRequest req){ HsfoRxDataHolder rx = (HsfoRxDataHolder) req.getSession() .getAttribute(HSFO_RX_DATA_KEY); JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(rx.getOutlines()); InputStream is = Thread.currentThread().getContextClassLoader() .getResourceAsStream("/oscar/form/prop/Hsfo_Rx.jasper"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { JasperRunManager.runReportToPdfStream(is, baos, rx.getParams(), ds); } catch (JRException e) { throw new RuntimeException(e); } return baos; }
private ByteArrayOutputStream generateHsfoRxPDF(HttpServletRequest req) { HsfoRxDataHolder rx = (HsfoRxDataHolder) req.getSession().getAttribute(HSFO_RX_DATA_KEY); JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(rx.getOutlines()); InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("/oscar/form/prop/Hsfo_Rx.jasper"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { JasperRunManager.runReportToPdfStream(is, baos, rx.getParams(), ds); } catch (JRException e) { throw new RuntimeException(e); } return baos; }
private ByteArrayOutputStream generateHsfoRxPDF(HttpServletRequest req){ HsfoRxDataHolder rx = (HsfoRxDataHolder) req.getSession() .getAttribute(HSFO_RX_DATA_KEY); JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(rx.getOutlines()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("/oscar/form/prop/Hsfo_Rx.jasper"); try { JasperRunManager.runReportToPdfStream(is, baos, rx.getParams(), ds); } catch (JRException e) { throw new RuntimeException(e); } finally { IOUtils.closeQuietly(is); } return baos; }
/** * */ public void run() throws JRException { long start = System.currentTimeMillis(); //Preparing parameters Image image = Toolkit.getDefaultToolkit().createImage("dukesign.jpg"); MediaTracker traker = new MediaTracker(new Panel()); traker.addImage(image, 0); try { traker.waitForID(0); } catch (Exception e) { e.printStackTrace(); } Map<String, Object> parameters = new HashMap<String, Object>(); parameters.put("ReportTitle", "The First Jasper Report Ever"); parameters.put("MaxOrderID", new Integer(10500)); parameters.put("SummaryImage", image); JasperRunManager.runReportToPdfFile("build/reports/FirstJasper.jasper", parameters, getDemoHsqldbConnection()); System.err.println("PDF running time : " + (System.currentTimeMillis() - start)); }
/** * @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); } }
/** * 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); } }
/** * 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); } }
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { ModelAndView mv = null; Integer pid = -1; try { pid = Integer.parseInt(request.getParameter("pid")); } catch (Exception ex) { pid = -1; } if (pid.equals(-1)) { return new ModelAndView("/error/wproblem"); } try { jasperReport = JasperCompileManager.compileReport(Config.getProperty("problems.report.path") + "Problem.jrxml"); Map<String, String> parametros = new HashMap<String, String>(); parametros.put("PID", String.valueOf(pid)); /* * try { jasperPrint = JasperFillManager.fillReport(jasperReport, * parametros, ds.getConnection()); } catch (SQLException ex) { * logger.error("Error generando Reporte."); ex.printStackTrace(); } * * * JasperExportManager.exportReportToPdfFile(jasperPrint, * "reporte.pdf"); */ // byte[] reporte = null; Connection conn = dataSource.getConnection(); reporte = JasperRunManager.runReportToPdf(jasperReport, parametros, conn); response.setContentType("application/pdf"); response.setHeader("Content-disposition", "inline; filename=" + pid + ".pdf"); response.setHeader("Cache-Control", "max-age=30"); response.setHeader("Pragma", "No-cache"); response.setDateHeader("Expires", 0); response.setContentLength(reporte.length); ServletOutputStream out = response.getOutputStream(); out.write(reporte, 0, reporte.length); out.flush(); out.close(); // conn.close(); return new ModelAndView("index"); } catch (Exception e) { System.out.println(e.getMessage()); } return mv; }
protected ActionForward performAction(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception { //no forward necessary if successful, returning stream String forward = FWD_FAIL; ActionError error = null; ActionMessages errors = new ActionMessages(); BaseActionForm dynaForm = (BaseActionForm) form; request.getSession().setAttribute(SAVE_DISABLED, "true"); String workplanType = dynaForm.getString("workplanType"); String workplanName; if (workplanType.equals("test") ){ String testID = (String)dynaForm.get("testTypeID"); workplanName = getTestTypeName(testID); }else { workplanType = Character.toUpperCase(workplanType.charAt(0))+ workplanType.substring(1); workplanName = dynaForm.getString("testName"); } //get workplan report based on testName workplanReport = getWorkplanReport(workplanType, workplanName); //set jasper report parameters HashMap<String, ?> parameterMap = workplanReport.getParameters(); //prepare report List<?> workplanRows = workplanReport.prepareRows(dynaForm); //set Jasper report file name String reportFileName = workplanReport.getFileName(); String reportFile = this.getServlet().getServletConfig().getServletContext().getRealPath("WEB-INF/reports/" + reportFileName + ".jasper"); try { byte[] bytes = null; JRDataSource dataSource = createReportDataSource(workplanRows); bytes = JasperRunManager.runReportToPdf(reportFile, parameterMap, dataSource); ServletOutputStream servletOutputStream = response.getOutputStream(); response.setContentType("application/pdf"); response.setContentLength(bytes.length); servletOutputStream.write(bytes, 0, bytes.length); servletOutputStream.flush(); servletOutputStream.close(); } catch (JRException jre) { LogEvent.logError("PringWorkplanReportAction","processRequest()", jre.toString()); error = new ActionError("error.jasper", null, null); } catch (Exception e) { LogEvent.logError("PrintWorkplanReportAction","processRequest()", e.toString()); error = new ActionError("error.jasper", null, null); } if (error != null) { errors.add(ActionMessages.GLOBAL_MESSAGE, error); request.setAttribute(Globals.ERROR_KEY, errors); forward = FWD_FAIL; } return mapping.findForward(forward); }
/** * Nearly every report does PDF this routine does that, but some do other things like export to CSV which involves messing with the response and the headers * @param response response, so that appropriate things can be set into it. * @param fullReportFilename - valid full path to write the report * @throws JRException * @throws IllegalStateException * @see us.mn.state.health.lims.reports.action.implementation.IReportCreator#runReport(java.lang.String, Response) */ @Override public byte[] runReport( ) throws Exception { return JasperRunManager.runReportToPdf(fullReportFilename, getReportParameters(), getReportDataSource()); }