|
| 1 | +package com.hmkcode; |
| 2 | + |
| 3 | +import net.sf.jasperreports.engine.*; |
| 4 | +import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource; |
| 5 | +import org.springframework.util.ResourceUtils; |
| 6 | + |
| 7 | +import java.io.*; |
| 8 | +import java.util.*; |
| 9 | + |
| 10 | +public class App |
| 11 | +{ |
| 12 | + // name and destination of output file e.g. "report.pdf" |
| 13 | + private static String destFileName = "report.pdf"; |
| 14 | + public static void main( String[] args ) throws FileNotFoundException, JRException { |
| 15 | + |
| 16 | + System.out.println( "generating jasper report..." ); |
| 17 | + |
| 18 | + // 1. compile template ".jrxml" file |
| 19 | + JasperReport jasperReport = getJasperReport(); |
| 20 | + |
| 21 | + // 2. parameters "empty" |
| 22 | + Map<String, Object> parameters = getParameters(); |
| 23 | + |
| 24 | + // 3. datasource "java object" |
| 25 | + JRDataSource dataSource = getDataSource(); |
| 26 | + |
| 27 | + JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource); |
| 28 | + JasperExportManager.exportReportToPdfFile(jasperPrint, destFileName); |
| 29 | + |
| 30 | + } |
| 31 | + |
| 32 | + private static JasperReport getJasperReport() throws FileNotFoundException, JRException { |
| 33 | + File template = ResourceUtils.getFile("classpath:report.jrxml"); |
| 34 | + return JasperCompileManager.compileReport(template.getAbsolutePath()); |
| 35 | + } |
| 36 | + private static Map<String, Object> getParameters(){ |
| 37 | + Map<String, Object> parameters = new HashMap<>(); |
| 38 | + parameters.put("createdBy", "hmkcode"); |
| 39 | + return parameters; |
| 40 | + } |
| 41 | + |
| 42 | + private static JRDataSource getDataSource(){ |
| 43 | + |
| 44 | + List<Country> countries = new LinkedList<>(); |
| 45 | + |
| 46 | + countries.add(new Country("IS", "Iceland", "https://i.pinimg.com/originals/72/b4/49/72b44927f220151547493e528a332173.png")); |
| 47 | + countries.add(new Country("TR", "Turkey", "https://i.pinimg.com/originals/82/63/23/826323bba32e6e5a5996062c3a3c662f.png")); |
| 48 | + countries.add(new Country("ZA", "South Africa", "https://i.pinimg.com/originals/f5/c7/8d/f5c78da001b46e26481c04fb93473454.png")); |
| 49 | + countries.add(new Country("PL", "Poland", "https://i.pinimg.com/originals/7f/ae/21/7fae21c4854010b11127218ead743863.png")); |
| 50 | + countries.add(new Country("CA", "Canada", "https://i.pinimg.com/originals/4d/d4/01/4dd401733ba25e6442fc8696e04e5846.png")); |
| 51 | + |
| 52 | + countries.add(new Country("PA", "Panama", "https://i.pinimg.com/originals/84/dc/e4/84dce49e52ebfb5b3814393069807e0a.png")); |
| 53 | + countries.add(new Country("HR", "Croatia", "https://i.pinimg.com/originals/f5/8c/94/f58c94a2a2b3221328fc1e2b7acfa656.png")); |
| 54 | + countries.add(new Country("JP", "Japan", "https://i.pinimg.com/originals/a5/d6/88/a5d688289cd6850016f14fe93b17da01.png")); |
| 55 | + countries.add(new Country("DE", "Germany", "https://i.pinimg.com/originals/af/c9/b2/afc9b2592a9f1cf591e8a52256ae1e9f.png")); |
| 56 | + countries.add(new Country("BR", "Brazil", "https://i.pinimg.com/originals/e4/03/c4/e403c4447a3bd8940459ae4f50856bed.png")); |
| 57 | + |
| 58 | + |
| 59 | + return new JRBeanCollectionDataSource(countries); |
| 60 | + } |
| 61 | +} |
0 commit comments