Advertisement
jzgeorge

printPgReport

Oct 3rd, 2018
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.43 KB | None | 0 0
  1.     @SuppressWarnings({ "static-access", "serial", "rawtypes" })
  2.     public static void printPgReport(final String reportName, final Map parameter) {
  3.         emPg.getEntityManager().getTransaction().begin();
  4.         final java.sql.Connection conn = emPg.getEntityManager().unwrap(java.sql.Connection.class);
  5.         emPg.getEntityManager().getTransaction().commit();
  6.        
  7.         if (conn == null) {
  8.             System.out.println("Error de conexion...\n");
  9.             return;
  10.         }
  11.        
  12.         StreamResource.StreamSource source = null;
  13.         try {
  14.             source = new StreamResource.StreamSource() {
  15.                 @SuppressWarnings("unchecked")
  16.                 public InputStream getStream() {
  17.                     byte[] b = null;
  18.                     try {
  19.                         InputStream rep = getClass().getClassLoader().getResourceAsStream("/edu/ceac/app/reportes/"+reportName+".jasper");
  20.                         System.out.println(rep);
  21.                         if (rep != null) {
  22.                             parameter.put("IMAGE_PATH", "/edu/ceac/app/reportes/images/");
  23.                             parameter.put("SUBREPORT_PATH", "/edu/ceac/app/reportes/subreportes/");
  24.                             JasperReport report = (JasperReport) JRLoader.loadObject(rep);
  25.                             b = JasperRunManager.runReportToPdf(report, parameter, conn);
  26.                             System.out.println(Paths.get(".").toAbsolutePath().normalize().toString());;
  27.                             JasperPrint print = JasperFillManager.fillReport(report, parameter, conn);
  28.                             JasperExportManager.exportReportToPdfFile(print, Paths.get(".").toAbsolutePath().normalize().toString()+"/reportName.pdf");
  29.                         } else {
  30.                             Notification.show("Atención!!!", "No se encontró el reporte...", Type.WARNING_MESSAGE);
  31.                         }
  32.                     } catch (JRException e) {
  33.                         System.out.println("Error:\n");
  34.                         e.printStackTrace();
  35.                     }
  36.                     return new ByteArrayInputStream(b);
  37.                 }
  38.             };
  39.         } catch(Exception e) {
  40.             Notification.show("Atención!!!", "Ocurrió un problema al generar el archivo...", Type.WARNING_MESSAGE);
  41.         }
  42.        
  43.         StreamResource resource = null;
  44.         try {
  45.             resource = new StreamResource(source, reportName + "_" + System.currentTimeMillis() + ".pdf");
  46.         } catch (Exception e) {
  47.             System.out.println("Error:\n");
  48.             e.printStackTrace();
  49.             return;
  50.         }
  51.         Window window = new Window();
  52.         window.setWidth(70, Unit.PERCENTAGE);
  53.         window.setHeight(90, Unit.PERCENTAGE);
  54.         BrowserFrame extF = new BrowserFrame("PDF File", resource);
  55.         extF.setWidth("100%");
  56.         extF.setHeight("100%");
  57.         window.setContent(extF);
  58.         window.center();
  59.         window.setModal(true);
  60.         UI.getCurrent().addWindow(window);
  61.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement