Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static final String JASPER_FILE_MOVIMENTI = "scalarePdf/pdf_movimenti.jrxml";
- private static final String JASPER_FILE_SCALARE = "scalarePdf/pdf_scalare.jrxml";
- @SuppressWarnings("rawtypes")
- public byte[] rawPdf(BeScalare beScalare, String codTabulato, String output) throws JRException, IOException {
- JRBeanArrayDataSource dataSource = new JRBeanArrayDataSource(new Object[]{beScalare});
- //report's list
- List<byte[]> pdfFilesAsByteArray = new ArrayList<byte[]>();
- //Report one
- Class cScalare = this.getClass();
- ClassLoader clScalare = cScalare.getClassLoader();
- InputStream isScalare = clScalare.getResourceAsStream(JASPER_FILE_SCALARE);
- JasperDesign jasDesignScalare = JRXmlLoader.load(isScalare);
- //compile report one
- JasperReport reportScalare = JasperCompileManager.compileReport(jasDesignScalare);
- //parameters report one
- Map<String, Object> paramScalare = new HashMap<String, Object>();
- JRBeanCollectionDataSource itemsScalareSaldiPerValuta = new JRBeanCollectionDataSource(beScalare.getLstBeScalareSaldiPerValuta());
- paramScalare.put("scalareSaldiPerValuta", itemsScalareSaldiPerValuta);
- //fill report one
- JasperPrint jasperPrintScalare = JasperFillManager.fillReport(reportScalare, paramScalare, dataSource);
- pdfFilesAsByteArray.add(JasperExportManager.exportReportToPdf(jasperPrintScalare));
- //Report two
- Class c = this.getClass();
- ClassLoader cl = c.getClassLoader();
- InputStream is = cl.getResourceAsStream(JASPER_FILE_MOVIMENTI);
- JasperDesign jasDesign = JRXmlLoader.load(is);
- //compile report two
- JasperReport reportMovimenti = JasperCompileManager.compileReport(jasDesign);
- //parameters report two
- Map<String, Object> paramMovimenti = new HashMap<String, Object>();
- BufferedImage imgNumeroVerde = ImageIO.read(getClass().getResource("/scalarePdf/headphones.png"));
- paramMovimenti.put("numeroVerde", imgNumeroVerde);
- BufferedImage imgInternet = ImageIO.read(getClass().getResource("/scalarePdf/internet.png"));
- paramMovimenti.put("internet", imgInternet);
- JRBeanCollectionDataSource itemsScalareMovimenti = new JRBeanCollectionDataSource(beScalare.getLstBeScalareMovimenti());
- paramMovimenti.put("scalareMovimenti", itemsScalareMovimenti);
- //fill report two
- JasperPrint jasperPrintMovimenti = JasperFillManager.fillReport(reportMovimenti, paramMovimenti, dataSource);
- pdfFilesAsByteArray.add(JasperExportManager.exportReportToPdf(jasperPrintMovimenti));
- //merge the two reports in one
- ByteArrayOutputStream outStream = new ByteArrayOutputStream();
- Document document = null;
- PdfCopy writer = null;
- for (byte[] pdfByteArray : pdfFilesAsByteArray) {
- try {
- PdfReader reader = new PdfReader(pdfByteArray);
- int numberOfPages = reader.getNumberOfPages();
- if (document == null) {
- document = new Document(reader.getPageSizeWithRotation(1));
- writer = new PdfCopy(document, outStream); // new
- document.open();
- }
- PdfImportedPage page;
- for (int i = 0; i < numberOfPages;) {
- ++i;
- page = writer.getImportedPage(reader, i);
- writer.addPage(page);
- }
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- document.close();
- outStream.close();
- return outStream.toByteArray();
- }
- //Report two
- dataSource.moveFirst();
- //Report two
- dataSource = new JRBeanArrayDataSource(new Object[]{beScalare});
Add Comment
Please, Sign In to add comment