Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void stampaReport(final String nomeFile, long p1, long p2, boolean autoprint) {
- String path = "/com/reports";
- final String reportFile = path + File.separatorChar + nomeFile;
- final HashMap parameters = new HashMap();
- if (p1 > 0 && p2 > 0) {
- parameters.put("numlista", p1);
- parameters.put("anno", p2);
- } else if (p1 > 0 && p2 <= 0) {
- parameters.put("id", p1);
- }
- if (autoprint) {
- Connection conn = null;
- try {
- InputStream report = getClass().getClassLoader().getResourceAsStream(reportFile);
- if (report == null) {
- mainWindow.showNotification("No report!");
- return;
- }
- conn = getDbHelp().getJDBCConnectionPool().reserveConnection();
- JasperPrint print = JasperFillManager.fillReport(report, parameters, conn);
- conn.commit();
- HtmlExporter exporterHTML = new HtmlExporter();
- final ByteArrayOutputStream output = new ByteArrayOutputStream();
- SimpleExporterInput exporterInput = new SimpleExporterInput(print);
- exporterHTML.setExporterInput(exporterInput);
- HtmlExporterOutput exporterOutput = new SimpleHtmlExporterOutput(output);
- exporterHTML.setExporterOutput(exporterOutput);
- SimpleHtmlExporterConfiguration exporterConfig = new SimpleHtmlExporterConfiguration();
- exporterConfig.setHtmlFooter("<script type='text/javascript'>window.print();</script>");
- exporterHTML.setConfiguration(exporterConfig);
- exporterHTML.exportReport();
- output.flush();
- StreamResource.StreamSource source = new StreamResource.StreamSource() {
- public InputStream getStream() {
- byte[] b = null;
- b = output.toByteArray();
- return new ByteArrayInputStream(b);
- }
- };
- StreamResource resource = new StreamResource(source, "report.html", getInstance());
- resource.getStream().setParameter("Content-Disposition", "attachment;filename=\"report.html\"");
- resource.setMIMEType("text/html");
- resource.setCacheTime(0);
- mainWindow.open(resource, "_new");
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- getDbHelp().getJDBCConnectionPool().releaseConnection(conn);
- }
- } else {
- try {
- StreamResource.StreamSource source = new StreamResource.StreamSource() {
- public InputStream getStream() {
- byte[] b = null;
- Connection conn = null;
- try {
- InputStream report = getClass().getClassLoader().getResourceAsStream(reportFile);
- if (report == null) {
- mainWindow.showNotification("No report!");
- return null;
- }
- conn = getDbHelp().getJDBCConnectionPool().reserveConnection();
- b = JasperRunManager.runReportToPdf(report, parameters, conn);
- conn.commit();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- getDbHelp().getJDBCConnectionPool().releaseConnection(conn);
- }
- return new ByteArrayInputStream(b);
- }
- };
- StreamResource resource = new StreamResource(source, "report.pdf", getInstance());
- resource.getStream().setParameter("Content-Disposition", "attachment;filename=\"report.pdf\"");
- resource.setMIMEType("application/pdf");
- resource.setCacheTime(0);
- mainWindow.open(resource, "_new");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement