Abstrakt do generowania raportu
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
/** * Bean abstrakcyjny dla generowania raportow * * @author ekaczynski@isolution.pl * created: 29 gru 2014 13:23:34 */ @ManagedBean public abstract class AbstractReport { private static final Logger LOGGER = LoggerFactory.getLogger(AbstractReport.class); private static final String TEMPLATES_DIRECTORY = "META-INF/jasper/"; @Autowired protected ReportService reportService; protected String templateName = "noReport.jrxml"; protected String outputFileName = "noReport.pdf"; protected String contentEncoding = "UTF-8"; protected String contentType = "application/pdf"; protected Map<String, Object> parameters = new HashMap<>(); protected JRDataSource dataSource = null; private DefaultStreamedContent file; /** * Dla wykonania akcji 'download' na przycisku pobierania */ public DefaultStreamedContent getDownload() { generateReport(); return file; } /** * Umozliwia rozszerzenie momentu generowania raportu */ protected JasperPrint generateReport(JasperReport jasperReport) throws JRException { LOGGER.debug("parameters: {}", parameters); LOGGER.debug("dataSource: {}", dataSource); return JasperFillManager.fillReport(jasperReport, parameters, dataSource); } /** * Generowanie raportu */ private void generateReport() { LOGGER.info("Generowanie raportu: {}, {}, {}", templateName, outputFileName, contentType); try { InputStream loadTemplate = loadTemplate(); JasperReport jasperReport = JasperCompileManager.compileReport(loadTemplate); JasperPrint jasperPrint = generateReport(jasperReport); jasperPrint.setName(outputFileName); byte[] pdfStream = JasperExportManager.exportReportToPdf(jasperPrint); file = new DefaultStreamedContent(new ByteArrayInputStream(pdfStream), contentType, outputFileName); file.setContentEncoding(contentEncoding); file.setContentType(contentType); file.setName(outputFileName); } catch (JRException e) { LOGGER.error("Blad w czasie generowania raportu", e); } } /** * Zaladowanie pliku z dysku */ private InputStream loadTemplate() { ClassLoader classloader = Thread.currentThread().getContextClassLoader(); return classloader.getResourceAsStream(TEMPLATES_DIRECTORY + templateName); } } |
Subreport
1 2 3 4 5 6 |
<subreport> <reportElement positionType="Float" x="10" y="19" width="545" height="1" isRemoveLineWhenBlank="true" uuid="602ca0be-0792-4b52-b73a-1514925e34b8"/> <subreportParameter name="parametr_przekazany"/> <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{moja_kolekcja})]]></dataSourceExpression> <subreportExpression><![CDATA["META-INF/jasper/podraport.jasper"]]></subreportExpression> </subreport> |
Coś jeszcze przydałoby się na ściągawce? Daj znać