public void export(HttpServletRequest req, HttpServletResponse res) throws Exception { res.setContentType("text/html;charset=UTF-8"); JRHtmlExporter exporter=new JRHtmlExporter(DefaultJasperReportsContext.getInstance()); JasperPrint jasperPrint=this.getJasperPrint(req); req.getSession().setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint); exporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint); OutputStream ouputStream = res.getOutputStream(); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream); String path=req.getContextPath(); if(path.endsWith("/")){ path+="dorado/bdf2/jasperreports/html.image"; }else{ path+="/dorado/bdf2/jasperreports/html.image"; } exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI,path+"?image="); try { exporter.exportReport(); } catch (JRException e) { throw new ServletException(e); } finally { if (ouputStream != null) { ouputStream.flush(); ouputStream.close(); } } }
private void exportPrintObject(ReporterConfig rc, final JasperPrint print, final ComponentExecutionApi reporter) { // build the export filename String filename = buildFilename(reporter, rc); // do each export option if (rc.isCsv()) { export(new JRCsvExporter(), print, filename, "csv", rc.isOpenExportFile()); } if (rc.isDocx()) { export(new JRDocxExporter(), print, filename, "docx", rc.isOpenExportFile()); } if (rc.isOdt()) { export(new JROdtExporter(), print, filename, "odt", rc.isOpenExportFile()); } if (rc.isHtml()) { export(new JRHtmlExporter(), print, filename, "html", rc.isOpenExportFile()); } if (rc.isPdf()) { export(new JRPdfExporter(), print, filename, "pdf", rc.isOpenExportFile()); } if (rc.isXls()) { export(new JRXlsExporter(), print, filename, "xls", rc.isOpenExportFile()); } // do show viewer at the end so it pops up after everything else if (rc.isShowViewer()) { reporter.submitControlLauncher(new ControlLauncherCallback() { @Override public void launchControls(ComponentControlLauncherApi launcherApi) { class DisposableViewer extends JRViewer implements Disposable { DisposableViewer(JasperPrint jrPrint) { super(jrPrint); } @Override public void dispose() { } } DisposableViewer viewer = new DisposableViewer(print); launcherApi.registerPanel("report", null, viewer, true); } }); } }
private void exportToHTML() { log.debug("exporting to HTML"); htmlExporter = new JRHtmlExporter(); setExportParameter(JRHtmlExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.TRUE); setExportParameter(JRHtmlExporterParameter.IMAGES_URI,"servlets/image?image="); setExportParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.TRUE); setExportParameter(JRHtmlExporterParameter.IS_OUTPUT_IMAGES_TO_DIR, Boolean.TRUE); setExportParameter(JRHtmlExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE); htmlExporter.setParameters(getExportParameters()); try { start = System.currentTimeMillis(); htmlExporter.exportReport(); log.info("export running time (msec): " + (System.currentTimeMillis() - start)); } catch (JRException jre) { jre.printStackTrace(); } }
public static byte[] getHTML(StorageService storageService, JasperPrint jasperPrint) throws JRException { byte[] content = null; ByteArrayOutputStream baos = null; try { baos = new ByteArrayOutputStream(); JRHtmlExporter exporter = new JRHtmlExporter(); // Jasper Images will be stored to reports home directory // so to be accesed from HTML exporter.setParameter(JRHtmlExporterParameter.IS_OUTPUT_IMAGES_TO_DIR, Boolean.TRUE); exporter.setParameter(JRHtmlExporterParameter.IMAGES_DIR_NAME, storageService.getSettings().getReportsHome()); exporter.setParameter(JRHtmlExporterParameter.SIZE_UNIT, "px"); exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "./"); content = getBytes(exporter, baos, jasperPrint); } finally { if (baos != null) { try { baos.flush(); baos.close(); } catch (Exception e) { e.printStackTrace(); } } } return content; }
@Test public void renderWithWriter() throws Exception { StringWriter writer = new StringWriter(); JasperPrint print = JasperFillManager.fillReport(getReport(), getParameters(), getDataSource()); JasperReportsUtils.render(new JRHtmlExporter(), print, writer); String output = writer.getBuffer().toString(); assertHtmlOutputCorrect(output); }
private static JRExporter getJREXporter(final String extension) { if ("pdf".equalsIgnoreCase(extension)) { JRPdfExporter exporter = new JRPdfExporter(); // exporter.setParameter(JRPdfExporterParameter.PDF_JAVASCRIPT, "this.print();"); return exporter; } else if ("html".equalsIgnoreCase(extension)) { return new JRHtmlExporter(); } else if ("xls".equalsIgnoreCase(extension)) { JRXlsExporter exporterXLS = new JRXlsExporter(); exporterXLS.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE); exporterXLS.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE); exporterXLS.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE); exporterXLS.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE); return exporterXLS; } else if ("txt".equalsIgnoreCase(extension)) { return new JRTextExporter(); } else if ("csv".equalsIgnoreCase(extension)) { return new JRCsvExporter(); } else if ("docx".equalsIgnoreCase(extension)) { return new JRDocxExporter(); } return null; }
@Override protected AbstractJasperReportsView getViewImplementation() { ConfigurableJasperReportsView view = new ConfigurableJasperReportsView(); view.setExporterClass(JRHtmlExporter.class); view.setUseWriter(true); view.setContentType("application/pdf"); return view; }
public void testRenderWithWriter() throws Exception { StringWriter writer = new StringWriter(); JasperPrint print = JasperFillManager.fillReport(getReport(), getParameters(), getDataSource()); JasperReportsUtils.render(new JRHtmlExporter(), print, writer); String output = writer.getBuffer().toString(); assertHtmlOutputCorrect(output); }
protected JRHtmlExporter getHtmlExporter() { JRHtmlExporter exporter = new JRHtmlExporter(); String random = "" + Math.random() * 1000.0; random = random.replace('.', '0').replace(',', '0'); exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, Utils.getWebContextPath(EnterpriseApplication.getInstance()) + "/image?r=" + random + "&image="); return exporter; }
protected JRHtmlExporter getHtmlExporter() { JRHtmlExporter exporter = new JRHtmlExporter(); String random = "" + Math.random() * 1000.0; random = random.replace('.', '0').replace(',', '0'); exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, getWebContextPath() + "/image?r=" + random + "&image="); return exporter; }
@Override public String getHtmlFragment(JRHtmlExporterContext context, JRGenericPrintElement element) { Map<String, Object> contextMap = new HashMap<String, Object>(); contextMap.put("mapCanvasId", "map_canvas_" + element.hashCode()); if (context.getExporter() instanceof JRXhtmlExporter) { contextMap.put("xhtml", "xhtml"); JRXhtmlExporter xhtmlExporter = (JRXhtmlExporter) context.getExporter(); contextMap.put("elementX", xhtmlExporter.toSizeUnit(element.getX())); contextMap.put("elementY", xhtmlExporter.toSizeUnit(element.getY())); } else { JRHtmlExporter htmlExporter = (JRHtmlExporter) context.getExporter(); contextMap.put("elementX", htmlExporter.toSizeUnit(element.getX())); contextMap.put("elementY", htmlExporter.toSizeUnit(element.getY())); } contextMap.put("elementId", element.getKey()); contextMap.put("elementWidth", element.getWidth()); contextMap.put("elementHeight", element.getHeight()); if (element.getModeValue() == ModeEnum.OPAQUE) { contextMap.put("backgroundColor", JRColorUtil.getColorHexa(element.getBackcolor())); } WmsRequestBuilder requestBuilder = WmsMapElementImageProvider.mapRequestBuilder(element); try { contextMap.put("wmsMapUrl", requestBuilder.toMapUrl()); } catch (MalformedURLException e) { throw new RuntimeException("Unable to build WMS map service url", e); } return VelocityUtil.processTemplate(MAP_ELEMENT_HTML_TEMPLATE, contextMap); }
@Override protected JRExporter createExporter() { return new JRHtmlExporter(); }
/** * metodo responsavel pelo preenchimento do relatorio, a partir do arquivo jasper compilado, do nome do arquivo html, * e considerando os dados sob a forma de List e os par�metros * * @param nomeArquivo * @param streamRelatorio * @param parametros * @param dados * @throws RelatorioException */ public void download(String nomeArquivo, InputStream streamRelatorio, Map parametros, List dados) throws RelatorioException { String caminhoRelatorio = caminhoRelatorioHTML + nomeArquivo; System.out.println("CAMINHO RELAT�RIO: " + caminhoRelatorio); File relatorio = new File(caminhoRelatorio); byte[] buffer = null; try { FacesContext context = FacesContext.getCurrentInstance(); HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse(); HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getResponse(); response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment;filename=\"" + nomeArquivo + "\""); if (parametros == null) { parametros = new HashMap(); } //Neste ponto, a nossa lista preenchida com o modelo � entregue ao Jasper, que cria um DataSource com ela JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(dados); //Aqui � onde ocorre de fato o preenchimento do relat�rio, com os par�metros e DataSource passados JasperPrint impressao = JasperFillManager.fillReport(streamRelatorio, parametros, dataSource); //Ponto onde o relat�rio com os dados montados � exportado para o formato HTML JasperExportManager.exportReportToHtmlFile(impressao, caminhoRelatorio); FileInputStream fis = new FileInputStream(relatorio); OutputStream os = response.getOutputStream(); response.setContentType("text/html"); JRHtmlExporter htmlExporter = new JRHtmlExporter(); request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, impressao); htmlExporter.setParameter(JRExporterParameter.JASPER_PRINT, impressao); htmlExporter.setParameter(JRExporterParameter.OUTPUT_WRITER, os); htmlExporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "/gesplan/image?image="); htmlExporter.exportReport(); int read = 0; buffer = new byte[1024]; while ((read = fis.read(buffer)) != -1) { os.write(buffer, 0, read); } os.flush(); os.close(); fis.close(); FacesContext.getCurrentInstance().responseComplete(); } catch (Throwable ex) { ex.printStackTrace(); throw new RelatorioException(ex); } finally { buffer = null; } }
@SuppressWarnings("unchecked") protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String nomeDoRelatorio = (String)request.getAttribute("nomeDoRelatorio"); System.out.println(">>>>>>>>>>>>>>>Nome do relatorio="+nomeDoRelatorio); List lista = (List)request.getAttribute("lista"); HashMap<String, String> parametros = (HashMap<String, String>)request.getAttribute("parametros"); ServletContext context = this.getServletContext(); String nomeDoArquivoCompilado = context.getRealPath(nomeDoRelatorio); System.out.println(">>>>>>>>>>>>>>>>>>>> nomeDoArquivoCompilado="+nomeDoArquivoCompilado); File arquivo = new File(nomeDoArquivoCompilado); PrintWriter printWriter = response.getWriter(); try { JasperReport relatorioCompilado = (JasperReport) JRLoader.loadObject(arquivo); JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(lista); JasperPrint impressao = JasperFillManager.fillReport(relatorioCompilado, parametros, ds); JRHtmlExporter htmlExporter = new JRHtmlExporter(); response.setContentType("text/html"); request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, impressao); htmlExporter.setParameter(JRExporterParameter.JASPER_PRINT, impressao); htmlExporter.setParameter(JRExporterParameter.OUTPUT_WRITER, printWriter); htmlExporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "/gesplan/image?image="); htmlExporter.exportReport(); } catch(JRException e) { e.printStackTrace(printWriter); } }
@Override public GenericElementHandler getHandler(String elementName, String exporterKey) { if (WmsMapPrintElement.WMS_MAP_ELEMENT_NAME.equals(elementName)) { if (JRGraphics2DExporter.GRAPHICS2D_EXPORTER_KEY .equals(exporterKey)) { return WmsMapElementGraphics2DHandler.getInstance(); } if (JRHtmlExporter.HTML_EXPORTER_KEY.equals(exporterKey) || JRXhtmlExporter.XHTML_EXPORTER_KEY.equals(exporterKey)) { return WmsMapElementHtmlHandler.getInstance(); } else if (JRPdfExporter.PDF_EXPORTER_KEY.equals(exporterKey)) { return WmsMapElementPdfHandler.getInstance(); } else if (JRXlsExporter.XLS_EXPORTER_KEY.equals(exporterKey)) { return WmsMapElementXlsHandler.getInstance(); } else if (JExcelApiExporter.JXL_EXPORTER_KEY.equals(exporterKey)) { return WmsMapElementJExcelApiHandler.getInstance(); } // else // if(JExcelApiMetadataExporter.JXL_METADATA_EXPORTER_KEY.equals(exporterKey)) // { // return MapElementJExcelApiMetadataHandler.getInstance(); // } else if (JRXlsxExporter.XLSX_EXPORTER_KEY.equals(exporterKey)) { return WmsMapElementXlsxHandler.getInstance(); } else if (JRDocxExporter.DOCX_EXPORTER_KEY.equals(exporterKey)) { return WmsMapElementDocxHandler.getInstance(); } else if (JRPptxExporter.PPTX_EXPORTER_KEY.equals(exporterKey)) { return WmsMapElementPptxHandler.getInstance(); } else if (JRRtfExporter.RTF_EXPORTER_KEY.equals(exporterKey)) { return WmsMapElementRtfHandler.getInstance(); } else if (JROdtExporter.ODT_EXPORTER_KEY.equals(exporterKey)) { return WmsMapElementOdtHandler.getInstance(); } else if (JROdsExporter.ODS_EXPORTER_KEY.equals(exporterKey)) { return WmsMapElementOdsHandler.getInstance(); } } return null; }
/** * Render a report in HTML format using the supplied report data. * Writes the results to the supplied {@code Writer}. * @param report the {@code JasperReport} instance to render * @param parameters the parameters to use for rendering * @param writer the {@code Writer} to write the rendered report to * @param reportData a {@code JRDataSource}, {@code java.util.Collection} or object array * (converted accordingly), representing the report data to read fields from * @throws JRException if rendering failed * @see #convertReportData */ public static void renderAsHtml(JasperReport report, Map<String, Object> parameters, Object reportData, Writer writer) throws JRException { JasperPrint print = JasperFillManager.fillReport(report, parameters, convertReportData(reportData)); render(new JRHtmlExporter(), print, writer); }
/** * Render a report in HTML format using the supplied report data. * Writes the results to the supplied {@code Writer}. * @param report the {@code JasperReport} instance to render * @param parameters the parameters to use for rendering * @param writer the {@code Writer} to write the rendered report to * @param reportData a {@code JRDataSource}, {@code java.util.Collection} or object array * (converted accordingly), representing the report data to read fields from * @param exporterParameters a {@link Map} of {@link JRExporterParameter exporter parameters} * @throws JRException if rendering failed * @see #convertReportData */ public static void renderAsHtml(JasperReport report, Map<String, Object> parameters, Object reportData, Writer writer, Map<JRExporterParameter, Object> exporterParameters) throws JRException { JasperPrint print = JasperFillManager.fillReport(report, parameters, convertReportData(reportData)); JRHtmlExporter exporter = new JRHtmlExporter(); exporter.setParameters(exporterParameters); render(exporter, print, writer); }
@Override public ByteArrayOutputStream exportReportToHtmlStream(WebApplicationContext context, Organization organization, String code, Map<String,Object> parameters, String destFileName) throws Exception { JasperPrint jasperPrint = executeReport(organization, code, parameters); // create the stream out report ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); // get context uri String uri = context.getHttpSession().getServletContext().getContextPath(); JRHtmlExporter exporter = new JRHtmlExporter(); String random = "" + Math.random() * 1000.0; random = random.replace('.', '0').replace(',', '0'); exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, uri + "/image?r=" + random + "&image="); context.getHttpSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream); exporter.exportReport(); outputStream.flush(); outputStream.close(); return outputStream; }