/** * Generates an HTML report from a pre-compiled report and returns it into a file. * * @param jasperPrint * JasperPrint object which contains a compiled report. * @param exportParameters * Export parameters than can be added to configure the resulting report. * @param file * The file used to return the report. * @throws JRException * In case there is any error generating the report an exception is thrown with the * error message. */ private static void saveHTMLReportToFile(JasperPrint jasperPrint, Map<Object, Object> exportParameters, File file) throws JRException { final HtmlExporter htmlExporter = new HtmlExporter(); SimpleExporterInput exporterInput = new SimpleExporterInput(jasperPrint); SimpleHtmlExporterOutput exporterOutput = new SimpleHtmlExporterOutput(file); if (exportParameters != null && exportParameters.size() > 0) { SimpleHtmlReportConfiguration exportConfiguration = new SimpleHtmlReportConfiguration(); setHtmlConfigurationFromExportParameters(exportParameters, exportConfiguration, exporterOutput); htmlExporter.setConfiguration(exportConfiguration); } else { SimpleHtmlReportConfiguration reportExportConfiguration = new SimpleHtmlReportConfiguration(); reportExportConfiguration.setSizeUnit(HtmlSizeUnitEnum.POINT); htmlExporter.setConfiguration(reportExportConfiguration); } htmlExporter.setExporterInput(exporterInput); htmlExporter.setExporterOutput(exporterOutput); htmlExporter.exportReport(); }
/** * */ public void html() throws JRException { long start = System.currentTimeMillis(); List<JasperPrint> jasperPrintList = new ArrayList<JasperPrint>(); jasperPrintList.add((JasperPrint)JRLoader.loadObjectFromFile("build/reports/Report1.jrprint")); jasperPrintList.add((JasperPrint)JRLoader.loadObjectFromFile("build/reports/Report2.jrprint")); jasperPrintList.add((JasperPrint)JRLoader.loadObjectFromFile("build/reports/Report3.jrprint")); HtmlExporter exporter = new HtmlExporter(); exporter.setExporterInput(SimpleExporterInput.getInstance(jasperPrintList)); exporter.setExporterOutput(new SimpleHtmlExporterOutput("build/reports/BatchExportReport.html")); exporter.exportReport(); System.err.println("HTML creation time : " + (System.currentTimeMillis() - start)); }
/** * Generates an HTML report from a pre-compiled report and returns it into an output stream. * * @param jasperPrint * JasperPrint object which contains a compiled report. * @param exportParameters * Export parameters than can be added to configure the resulting report. * @param outputStream * The output stream used to return the report. * @throws JRException * In case there is any error generating the report an exception is thrown with the * error message. */ private static void saveHTMLReportToOutputStream(JasperPrint jasperPrint, Map<Object, Object> exportParameters, OutputStream outputStream) throws JRException { final HtmlExporter htmlExporter = new HtmlExporter(); SimpleExporterInput exporterInput = new SimpleExporterInput(jasperPrint); SimpleHtmlExporterOutput exporterOutput = new SimpleHtmlExporterOutput(outputStream); if (exportParameters != null && exportParameters.size() > 0) { SimpleHtmlReportConfiguration exportConfiguration = new SimpleHtmlReportConfiguration(); setHtmlConfigurationFromExportParameters(exportParameters, exportConfiguration, exporterOutput); htmlExporter.setConfiguration(exportConfiguration); } else { SimpleHtmlReportConfiguration reportExportConfiguration = new SimpleHtmlReportConfiguration(); reportExportConfiguration.setSizeUnit(HtmlSizeUnitEnum.POINT); htmlExporter.setConfiguration(reportExportConfiguration); } htmlExporter.setExporterInput(exporterInput); htmlExporter.setExporterOutput(exporterOutput); try { htmlExporter.exportReport(); } catch (Exception e) { // Handle the exception to ignore the error thrown when the user closes the browser before // getting the whole HTML report Throwable t = e.getCause(); if (t != null && !(t.toString().contains("ClientAbortException"))) { throw new JRException(e.getMessage(), e); } } }
/** * */ public void html() throws JRException { long start = System.currentTimeMillis(); File sourceFile = new File("build/reports/NoPageBreakReport.jrprint"); JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".html"); HtmlExporter exporter = new HtmlExporter(); exporter.setExporterInput(new SimpleExporterInput(jasperPrint)); exporter.setExporterOutput(new SimpleHtmlExporterOutput(destFile)); SimpleHtmlExporterConfiguration exporterConfig = new SimpleHtmlExporterConfiguration(); exporterConfig.setBetweenPagesHtml(""); exporter.setConfiguration(exporterConfig); SimpleHtmlReportConfiguration reportConfig = new SimpleHtmlReportConfiguration(); reportConfig.setRemoveEmptySpaceBetweenRows(true); exporter.setConfiguration(reportConfig); exporter.exportReport(); System.err.println("HTML creation time : " + (System.currentTimeMillis() - start)); }
@Override public void save(JasperPrint jasperPrint, File file) throws JRException { if ( !file.getName().toLowerCase().endsWith(EXTENSION_HTM) && !file.getName().toLowerCase().endsWith(EXTENSION_HTML) ) { file = new File(file.getAbsolutePath() + EXTENSION_HTML); } if ( !file.exists() || JOptionPane.OK_OPTION == JOptionPane.showConfirmDialog( null, MessageFormat.format( getBundleString("file.exists"), new Object[]{file.getName()} ), getBundleString("save"), JOptionPane.OK_CANCEL_OPTION ) ) { HtmlExporter exporter = new HtmlExporter(getJasperReportsContext()); exporter.setExporterInput(new SimpleExporterInput(jasperPrint)); exporter.setExporterOutput(new SimpleHtmlExporterOutput(file)); exporter.exportReport(); } }
/** * Exports the generated report object received as parameter into HTML format, * placing the result into the second file parameter. * <p> * The images are placed as distinct files inside a directory having the same name * as the HTML destination file, plus the "_files" suffix. * * @param jasperPrint report object to export * @param destFileName file name to place the HTML content into * @see net.sf.jasperreports.engine.export.JRPdfExporter */ public void exportToHtmlFile( JasperPrint jasperPrint, String destFileName ) throws JRException { HtmlExporter exporter = new HtmlExporter(jasperReportsContext); exporter.setExporterInput(new SimpleExporterInput(jasperPrint)); exporter.setExporterOutput(new SimpleHtmlExporterOutput(destFileName)); exporter.exportReport(); }
@Override protected JRElement createColumnGroupCell(ColumnGroup columnGroup, Cell cell, JRDesignElementGroup elementGroup) { JRDesignFrame frame = (JRDesignFrame) createCell(elementGroup, cell, columnGroup.getWidth(), fillColumn.getWidth(), xOffset, yOffset, null, deriveUUID(), true); frame.getPropertiesMap().setProperty(HtmlExporter.PROPERTY_HTML_CLASS, "jrcolGroupHeader"); return frame; }
public void exportXhtml() throws JRException { HtmlExporter exporter = new HtmlExporter(); exporter.setExporterInput(new SimpleExporterInput(jasperPrint)); exporter.setExporterOutput(new SimpleHtmlExporterOutput(this.output .getAbsolutePath() + ".x.html")); exporter.exportReport(); }
@Override protected HtmlExporter getExporter(JasperReportsConfiguration jContext, JRExportProgressMonitor monitor, File file) { HtmlExporter exp = new HtmlExporter(jContext); exp.setExporterOutput(new SimpleHtmlExporterOutput(file)); SimpleHtmlReportConfiguration rconf = new SimpleHtmlReportConfiguration(); setupReportConfiguration(rconf, monitor); exp.setConfiguration(rconf); return exp; }
/** * Exports by MIME type. * * @param mimeType MIME type * @param jp Jasper print * @param os outputstream * @throws JRException */ protected void export(String mimeType, JasperPrint jp, OutputStream os) throws JRException { if ("application/pdf".equalsIgnoreCase(mimeType)) { exportReport(new JRPdfExporter(), jp, os); } else if ("text/xml".equalsIgnoreCase(mimeType)) { exportReport(new HtmlExporter(), jp, os); } else if ("application/rtf".equalsIgnoreCase(mimeType)) { exportReport(new JRRtfExporter(), jp, os); } else if ("application/xls".equalsIgnoreCase(mimeType)) { exportReport(new JRXlsExporter(), jp, os); } else if ("application/odt".equalsIgnoreCase(mimeType)) { exportReport(new JROdtExporter(), jp, os); } else if ("application/ods".equalsIgnoreCase(mimeType)) { exportReport(new JROdsExporter(), jp, os); } else if ("application/docx".equalsIgnoreCase(mimeType)) { exportReport(new JRDocxExporter(), jp, os); } else if ("application/xlsx".equalsIgnoreCase(mimeType)) { exportReport(new JRXlsxExporter(), jp, os); } else if ("application/pptx".equalsIgnoreCase(mimeType)) { exportReport(new JRPptxExporter(), jp, os); } else if ("text/xhmtl".equalsIgnoreCase(mimeType)) { exportReport(new JRXhtmlExporter(), jp, os); } else { throw new IllegalArgumentException("JasperRenderer does not support " + mimeType + " MIME type."); } }
@Override public GenericElementHandler getHandler(String elementName, String exporterKey) { if (handlers.containsKey(elementName)) { if (HtmlExporter.HTML_EXPORTER_KEY.equals(exporterKey)) { return handlers.get(elementName); } } return null; }
@Override public void service( HttpServletRequest request, HttpServletResponse response ) throws IOException, ServletException { ServletContext context = this.getServletConfig().getServletContext(); response.setContentType("text/html"); PrintWriter out = response.getWriter(); try { File reportFile = new File(context.getRealPath("/reports/WebappReport.jasper")); if (!reportFile.exists()) throw new JRRuntimeException("File WebappReport.jasper not found. The report design must be compiled first."); JasperReport jasperReport = (JasperReport)JRLoader.loadObjectFromFile(reportFile.getPath()); Map<String, Object> parameters = new HashMap<String, Object>(); parameters.put("ReportTitle", "Address Report"); parameters.put("BaseDir", reportFile.getParentFile()); JasperPrint jasperPrint = JasperFillManager.fillReport( jasperReport, parameters, new WebappDataSource() ); HtmlExporter exporter = new HtmlExporter(); request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint); exporter.setExporterInput(new SimpleExporterInput(jasperPrint)); SimpleHtmlExporterOutput output = new SimpleHtmlExporterOutput(out); output.setImageHandler(new WebHtmlResourceHandler("image?image={0}")); exporter.setExporterOutput(output); exporter.exportReport(); } catch (JRException e) { out.println("<html>"); out.println("<head>"); out.println("<title>JasperReports - Web Application Sample</title>"); out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"../stylesheet.css\" title=\"Style\">"); out.println("</head>"); out.println("<body bgcolor=\"white\">"); out.println("<span class=\"bnew\">JasperReports encountered this error :</span>"); out.println("<pre>"); e.printStackTrace(out); out.println("</pre>"); out.println("</body>"); out.println("</html>"); } }
/** * */ public void html() throws JRException { long start = System.currentTimeMillis(); File sourceFile = new File("build/reports/StylesReport.jrprint"); JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".html"); HtmlExporter exporter = new HtmlExporter(); exporter.setExporterInput(new SimpleExporterInput(jasperPrint)); exporter.setExporterOutput(new SimpleHtmlExporterOutput(destFile)); exporter.exportReport(); System.err.println("HTML creation time : " + (System.currentTimeMillis() - start)); }
@Override public GenericElementHandler getHandler(String elementName, String exporterKey) { if (NAME.equals(elementName) && JRPdfExporter.PDF_EXPORTER_KEY.equals(exporterKey)) { return new HtmlElementPdfHandler(); } else if (NAME.equals(elementName) && HtmlExporter.HTML_EXPORTER_KEY.equals(exporterKey)) { return new HtmlElementHtmlHandler(); } else if (NAME.equals(elementName) && JRXlsExporter.XLS_EXPORTER_KEY.equals(exporterKey)) { return new HtmlElementXlsHandler(); } else if (NAME.equals(elementName) && JRGraphics2DExporter.GRAPHICS2D_EXPORTER_KEY.equals(exporterKey)) { return new HtmlElementGraphics2DHandler(); } else if (NAME.equals(elementName) && JRDocxExporter.DOCX_EXPORTER_KEY.equals(exporterKey)) { return new HtmlElementDocxHandler(); } else if (NAME.equals(elementName) && JRPptxExporter.PPTX_EXPORTER_KEY.equals(exporterKey)) { return new HtmlElementPptxHandler(); } else if (NAME.equals(elementName) && JRXlsxExporter.XLSX_EXPORTER_KEY.equals(exporterKey)) { return new HtmlElementXlsxHandler(); } else if (NAME.equals(elementName) && JRRtfExporter.RTF_EXPORTER_KEY.equals(exporterKey)) { return new HtmlElementRtfHandler(); } else if (NAME.equals(elementName) && JROdtExporter.ODT_EXPORTER_KEY.equals(exporterKey)) { return new HtmlElementOdtHandler(); } else if (NAME.equals(elementName) && JROdsExporter.ODS_EXPORTER_KEY.equals(exporterKey)) { return new HtmlElementOdsHandler(); } return null; }
protected void addHtmlClass(String className) { String originalClasses = printProperties.get(HtmlExporter.PROPERTY_HTML_CLASS); String newClasses = originalClasses == null ? className : (originalClasses + " " + className); setPrintProperty(HtmlExporter.PROPERTY_HTML_CLASS, newClasses); }
@Override public String getHtmlFragment(JRHtmlExporterContext context, JRGenericPrintElement element) { ReportContext reportContext = context.getExporterRef().getReportContext(); Map<String, Object> contextMap = new HashMap<String, Object>(); contextMap.put("mapCanvasId", "map_canvas_" + element.hashCode()); HtmlExporter htmlExporter = (HtmlExporter)context.getExporterRef(); contextMap.put("elementX", htmlExporter.toSizeUnit((float)element.getX())); contextMap.put("elementY", htmlExporter.toSizeUnit((float)element.getY())); contextMap.put("elementWidth", element.getWidth()); contextMap.put("elementHeight", element.getHeight()); if (element.getModeValue() == ModeEnum.OPAQUE) { contextMap.put("backgroundColor", JRColorUtil.getColorHexa(element.getBackcolor())); } contextMap.put("gotReportContext", reportContext != null); if (reportContext == null) { Float latitude = (Float)element.getParameterValue(MapComponent.ITEM_PROPERTY_latitude); latitude = latitude == null ? MapComponent.DEFAULT_LATITUDE : latitude; Float longitude = (Float)element.getParameterValue(MapComponent.ITEM_PROPERTY_longitude); longitude = longitude == null ? MapComponent.DEFAULT_LONGITUDE : longitude; Integer zoom = (Integer)element.getParameterValue(MapComponent.PARAMETER_ZOOM); zoom = zoom == null ? MapComponent.DEFAULT_ZOOM : zoom; String mapType = (String)element.getParameterValue(MapComponent.ATTRIBUTE_MAP_TYPE); mapType = (mapType == null ? MapComponent.DEFAULT_MAP_TYPE.getName() : mapType).toUpperCase(); contextMap.put("latitude", latitude); contextMap.put("longitude", longitude); contextMap.put("zoom", zoom); contextMap.put("mapType", mapType); List<Map<String,Object>> markerList = (List<Map<String,Object>>)element.getParameterValue(MapComponent.PARAMETER_MARKERS); String markers = markerList == null || markerList.isEmpty() ? "[]" : JacksonUtil.getInstance(context.getJasperReportsContext()).getJsonString(markerList); contextMap.put("markerList", markers); List<Map<String,Object>> pathList = (List<Map<String,Object>>)element.getParameterValue(MapComponent.PARAMETER_PATHS); String paths = pathList == null || pathList.isEmpty() ? "[]" : JacksonUtil.getInstance(context.getJasperReportsContext()).getJsonString(pathList); contextMap.put("pathsList", paths); String reqParams = (String)element.getParameterValue(MapComponent.PARAMETER_REQ_PARAMS); if(reqParams != null) { contextMap.put(MapComponent.PARAMETER_REQ_PARAMS, reqParams); } if (context.getValue(FIRST_ATTEMPT_PARAM) == null) { context.setValue(FIRST_ATTEMPT_PARAM, true); //FIXME: support for parametrized http://maps.google.com/maps/api/js script (see MapElementHtmlTemplate.vm) contextMap.put("exporterFirstAttempt", true); } } return VelocityUtil.processTemplate(MAP_ELEMENT_HTML_TEMPLATE, contextMap); }
@Override public String getHtmlFragment(JRHtmlExporterContext context, JRGenericPrintElement element) { JRPrintText labelPrintText = (JRPrintText)element.getParameterValue(IconLabelElement.PARAMETER_LABEL_TEXT_ELEMENT); if (labelPrintText == null) { return null; } JRBasePrintFrame frame = new JRBasePrintFrame(element.getDefaultStyleProvider()); frame.setX(element.getX()); frame.setY(element.getY()); frame.setWidth(element.getWidth()); frame.setHeight(element.getHeight()); frame.setStyle(element.getStyle()); frame.setBackcolor(element.getBackcolor()); frame.setForecolor(element.getForecolor()); frame.setMode(element.getModeValue()); JRLineBox lineBox = (JRLineBox)element.getParameterValue(IconLabelElement.PARAMETER_LINE_BOX); if (lineBox != null) { frame.copyBox(lineBox); } frame.addElement(labelPrintText); JRPrintText iconPrintText = (JRPrintText)element.getParameterValue(IconLabelElement.PARAMETER_ICON_TEXT_ELEMENT); if (iconPrintText != null) { frame.addElement(iconPrintText); } HtmlExporter htmlExporter = (HtmlExporter)context.getExporterRef(); List<JRPrintElement> elements = new ArrayList<JRPrintElement>(); elements.add(frame); try { htmlExporter.exportElements(elements); } catch (IOException e) { throw new JRRuntimeException(e); } return ""; }
@Override public String getHtmlFragment(JRHtmlExporterContext context, JRGenericPrintElement element) { Boolean showLegend = (Boolean) element.getParameterValue(GeoMapPrintElement.PARAMETER_SHOW_LEGEND); GeoMapDataMode dataMode = (GeoMapDataMode) element.getParameterValue(GeoMapPrintElement.PARAMETER_DATA_MODE); String region = (String) element.getParameterValue(GeoMapPrintElement.PARAMETER_REGION); String valueLabel = (String) element.getParameterValue(GeoMapPrintElement.PARAMETER_VALUE_LABEL); if (valueLabel == null) { valueLabel = ""; } @SuppressWarnings("unchecked") List<Color> colors = (List<Color>) element.getParameterValue(GeoMapPrintElement.PARAMETER_COLORS); String stringColors = null; if (colors != null && !colors.isEmpty()) { stringColors = ""; for (Color color : colors) { stringColors += "," + getColorString(color); } stringColors = StringUtils.removeStart(stringColors, ","); } @SuppressWarnings("unchecked") Set<GeoMapData> dataset = (Set<GeoMapData>) element.getParameterValue(GeoMapPrintElement.PARAMETER_DATASET); VelocityContext velocityContext = new VelocityContext(); velocityContext.put("showLegend", showLegend); if (dataMode != null) { switch (dataMode) { case REGIONS: velocityContext.put("dataMode", "regions"); break; case MARKERS: velocityContext.put("dataMode", "markers"); break; default: break; } } velocityContext.put("id", "map_" + element.hashCode()); velocityContext.put("region", region); velocityContext.put("valueLabel", valueLabel); velocityContext.put("colors", stringColors); velocityContext.put("dataset", dataset); @SuppressWarnings("unchecked") Exporter<ExporterInput, ? extends HtmlReportConfiguration, ? extends HtmlExporterConfiguration, HtmlExporterOutput> exporter = context.getExporterRef(); HtmlExporter htmlExporter2 = exporter instanceof HtmlExporter ? (HtmlExporter)exporter : null; if(htmlExporter2 == null) { velocityContext.put("xhtml", "xhtml"); velocityContext.put("elementX", ((HtmlExporter)context.getExporterRef()).toSizeUnit((float) element.getX())); velocityContext.put("elementY", ((HtmlExporter)context.getExporterRef()).toSizeUnit((float) element.getY())); } else { velocityContext.put("elementX", ((HtmlExporter)context.getExporterRef()).toSizeUnit((float) element.getX())); velocityContext.put("elementY", ((HtmlExporter)context.getExporterRef()).toSizeUnit((float) element.getY())); } velocityContext.put("elementWidth", element.getWidth()); velocityContext.put("elementHeight", element.getHeight()); if (element.getModeValue() == ModeEnum.OPAQUE) { velocityContext.put("backgroundColor", JRColorUtil.getColorHexa(element.getBackcolor())); } return VelocityUtil.processTemplate(GEOMAP_ELEMENT_HTML_TEMPLATE, velocityContext); }
/** * Generates an HTML report using the SimpleExporterInput, SimpleHtmlExporterOutput and * SimpleHtmlReportConfiguration received as parameters. * * @param exporterInput * SimpleExporterInput object with the input data. * @param exporterOutput * SimpleHtmlExporterOutput object with the output data. * @param exportConfiguration * SimpleHtmlReportConfiguration with the configuration data. * @throws JRException * In case there is any error generating the report an exception is thrown with the * error message. */ public static void saveHTMLReport(SimpleExporterInput exporterInput, SimpleHtmlExporterOutput exporterOutput, SimpleHtmlReportConfiguration exportConfiguration) throws JRException { final HtmlExporter htmlExporter = new HtmlExporter(); htmlExporter.setExporterInput(exporterInput); htmlExporter.setExporterOutput(exporterOutput); htmlExporter.setConfiguration(exportConfiguration); htmlExporter.exportReport(); }