/** * Saves the rendered report to the specified DeliverFile component, which the caller will return * back to the user. * * @param file the DeliverFile component to save the file into * @return a Throwable if an error occurred (which the caller should display to the user), or null * if the operation was successful */ public Throwable deliverTo(DeliverFile file) { try { NSMutableDataOutputStream outputStream = new NSMutableDataOutputStream(); PDFRenderOption option = new PDFRenderOption(); option.setOutputFormat("PDF"); option.setOutputStream(outputStream); IRenderTask task = reportEngine().createRenderTask(reportDocument()); task.setRenderOption(option); org.mozilla.javascript.Context.enter(); task.render(); org.mozilla.javascript.Context.exit(); outputStream.close(); close(); file.setFileData(outputStream.data()); file.setContentType("application/pdf"); file.setStartDownload(true); file.setDeliveredName(generatedReport().description() + ".pdf"); } catch (Exception e) { return e; } return null; }
private void setupRenderTask(IRenderTask rendTask, RptProperties properties) { String outputFormat = null; switch (properties.getOutputFormat()) { case HTML: outputFormat = "html"; break; case DOC: outputFormat = "doc"; break; case PDF: outputFormat = "pdf"; break; case PPT: outputFormat = "ppt"; break; case XLS: outputFormat = "xls"; break; default: outputFormat = "html"; } IRenderOption renderOption = new RenderOption(); renderOption.setOutputStream(new ByteArrayOutputStream(bufferSize)); renderOption.setOutputFormat(outputFormat); rendTask.setRenderOption(renderOption); rendTask.setLocale(properties.getLocale()); }
protected String renderPage(IReportDocument doc, long pageNo) throws Exception { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); assertTrue(pageNo <= doc.getPageCount()); IRenderTask renderTask = engine.createRenderTask(doc); try { HTMLRenderOption options = new HTMLRenderOption(); options.setOutputFormat("html"); options.setOutputStream(buffer); renderTask.setRenderOption(options); renderTask.setPageNumber((long) pageNo); renderTask.render(); List errors = renderTask.getErrors(); assertEquals(0, errors.size()); } finally { renderTask.close(); } return new String(buffer.toString("UTF-8")); }
@SuppressWarnings("unchecked") protected void renderMergedOutputModel( Map<String, Object> modelData, HttpServletRequest request, HttpServletResponse response) throws Exception { FileInputStream fis = null; IReportRunnable runnable = null; IReportDocument document = null; try { if (this.reportParameters == null) this.reportParameters = new HashMap<String, Object>(); for (String k : modelData.keySet()) this.reportParameters.put(k, modelData.get(k)); // 2) reportName property // 1) report name parameter is available, use that String reportName; reportName = StringUtils.hasText(this.reportName) ? this.reportName : request.getParameter(this.reportNameRequestParameter); // 'cat' String fullReportName = canonicalizeName(reportName); String documentName; documentName = StringUtils.hasText(this.documentName) ? this.documentName : request.getParameter(this.documentNameRequestParameter); // 'cat' String fullDocumentName = canonicalizeDocName(documentName); if (documentName == null) { fullDocumentName = reportName.replaceAll(".rptdesign", ".rptdocument"); } String format; if (this.reportOutputFormat != null) { format = this.reportOutputFormat; } else { format = request.getParameter(this.reportFormatRequestParameter); } ServletContext sc = request.getServletContext(); // / avoid creating an HTTP session if possible. if (format == null) { format = "html"; } Map<String, Object> mapOfOptions = new HashMap<String, Object>(); mapOfOptions.put( IModuleOption.RESOURCE_FOLDER_KEY, birtViewResourcePathCallback.resourceDirectory(sc, request, reportName)); mapOfOptions.put(IModuleOption.PARSER_SEMANTIC_CHECK_KEY, Boolean.FALSE); // set content type String contentType = birtEngine.getMIMEType(format); response.setContentType(contentType); setContentType(contentType); Map<String, Object> appContextMap = new HashMap<String, Object>(); appContextMap.put(EngineConstants.APPCONTEXT_BIRT_VIEWER_HTTPSERVET_REQUEST, request); if (this.dataSource != null) { appContextMap.put(IConnectionFactory.PASS_IN_CONNECTION, this.dataSource.getConnection()); if (this.closeDataSourceConnection) appContextMap.put(IConnectionFactory.CLOSE_PASS_IN_CONNECTION, Boolean.TRUE); } IEngineTask task = null; String pathForReport = birtViewResourcePathCallback.pathForReport(sc, request, fullReportName); fis = new FileInputStream(pathForReport); runnable = birtEngine.openReportDesign(fullReportName, fis, mapOfOptions); if (runnable != null && this.taskType == AbstractSingleFormatBirtView.RUNRENDERTASK) { task = birtEngine.createRunAndRenderTask(runnable); task.setParameterValues(discoverAndSetParameters(runnable, request)); IRunAndRenderTask runAndRenderTask = (IRunAndRenderTask) task; IRenderOption options = null == this.renderOption ? new RenderOption() : this.renderOption; options.setActionHandler(actionHandler); IRenderOption returnedRenderOptions = renderReport( modelData, request, response, this.birtViewResourcePathCallback, appContextMap, reportName, format, options); for (String k : appContextMap.keySet()) runAndRenderTask.getAppContext().put(k, appContextMap.get(k)); runAndRenderTask.setRenderOption(returnedRenderOptions); runAndRenderTask.run(); runAndRenderTask.close(); } else { // Run then Render if (runnable != null) { task = birtEngine.createRunTask(runnable); task.setParameterValues(discoverAndSetParameters(runnable, request)); IRunTask runTask = (IRunTask) task; for (String k : appContextMap.keySet()) runTask.getAppContext().put(k, appContextMap.get(k)); String pathForDocument = birtViewResourcePathCallback.pathForDocument(sc, request, fullDocumentName); runTask.run(pathForDocument); runTask.close(); document = birtEngine.openReportDocument(fullDocumentName, pathForDocument, mapOfOptions); task = birtEngine.createRenderTask(document); IRenderTask renderTask = (IRenderTask) task; IRenderOption options = null == this.renderOption ? new RenderOption() : this.renderOption; options.setActionHandler(actionHandler); IRenderOption returnedRenderOptions = renderReport( modelData, request, response, this.birtViewResourcePathCallback, appContextMap, reportName, format, options); for (String k : appContextMap.keySet()) renderTask.getAppContext().put(k, appContextMap.get(k)); if (renderRange != null) { renderTask.setPageRange(renderRange); } renderTask.setRenderOption(returnedRenderOptions); renderTask.render(); renderTask.close(); document.close(); } } } catch (Throwable th) { throw new RuntimeException(th); // nothing useful to do here } finally { if (null != fis) IOUtils.closeQuietly(fis); if (null != document) document.close(); } }