Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void printReport(final String filename, long p1) {
- String path = "/com/myapp/reports/";
- final String reportFile = path + filename;
- final HashMap parameters = new HashMap();
- parameters.put("id", p1);
- 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) {
- Notification.show("No report!", Type.ERROR_MESSAGE);
- 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");
- resource.getStream().setParameter("Content-Disposition", "attachment;filename=\"report.pdf\"");
- resource.setMIMEType("application/pdf");
- resource.setCacheTime(0);
- Window w = new Window();
- w.setModal(true);
- w.setCloseShortcut(KeyCode.ESCAPE, null);
- w.setResizable(false);
- //w.setClosable(false);
- w.setHeight(90.0f, Unit.PERCENTAGE);
- w.setWidth(90.0f, Unit.PERCENTAGE);
- Embedded e = new Embedded("Print", resource);
- e.setMimeType("application/pdf");
- e.setType(Embedded.TYPE_OBJECT);
- e.setSizeFull();
- VerticalLayout layout = new VerticalLayout();
- layout.setSizeFull();
- layout.addComponent(e);
- w.setContent(layout);
- UI.getCurrent().addWindow(w);
- w.focus();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement