Hi , This is my second blog on jasper report. I will tell you that How to create a dynmic jrxml file using dynamic jasper.
In my previous blog I have discussed integration of jasper report in grails.
For using Dynamic jasper we need two jar file, DynamicJasper-4.0.3.jar and DynamicJasper-4.0.3-tests.jar. You need to import it in your lib file.
Note: Before download these jar file you need to check the compatibility with jasper-report version.
I am using jasper-report-4.7.1 which is compatible with these jar files of dynamic jasper.
For generating jrxml file firstly we need to create its design.
We have two option to generate its design.
1. Using FastReportBuilder
2. Using JasperDesign
As in comparison of both design tool I prefer jasperDesign because it gave me same procedure to develop my code as we create from Ireport tool. When I try to write this code using groovy , I faced some problem because of “$” sign. So I wrote this code in java file. I made a file suppose designUsingJasperDesign.java.
Here are follwing syntax for designing.
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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
[php] import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.design.*; import net.sf.jasperreports.engine.type.*; public class JasperDesignForTemplate { public JasperDesign design() throws Exception { // set basic design for main report JasperDesign jasperDesign = new JasperDesign(); jasperDesign.setName("report"); jasperDesign.setPageWidth(595); jasperDesign.setPageHeight(842); jasperDesign.setColumnWidth(515); jasperDesign.setColumnSpacing(0); jasperDesign.setLeftMargin(40); jasperDesign.setRightMargin(40); jasperDesign.setTopMargin(20); jasperDesign.setBottomMargin(20); JRDesignExpression expression = new JRDesignExpression(); // set any type of expression in //report //Parameters JRDesignParameter parameter = new JRDesignParameter(); parameter.setName("nameOfParameter"); parameter.setValueClass(java.lang.String.class); expression = new JRDesignExpression(); expression.setText(“anyTextYouWantToPassWithParameter”); jasperDesign.addParameter(parameter); } //Variable JRDesignVariable variable = new JRDesignVariable(); variable.setName("nameOfVariable"); variable.setValueClass(java.lang.String.class); expression = new JRDesignExpression(); expression.setText(“anyTextYouWantToPassWithVariable ”); jasperDesign.addVariable (variable); // field JRDesignField field = new JRDesignField(); field.setName(“nameOfField”); field.setValueClass(nameOfClass); // e.g. If you want to make a field of string type then you // should define it as String.class // title band JRDesignBand band = new JRDesignBand(); band.setHeight(20); // it will set height of title band as 20 pt. jasperDesign.setTitle(band); // end of title band // page header band band = new JRDesignBand(); band.setHeight(20); jasperDesign.setpageHeader(band); // end of page header band // column header band band = new JRDesignBand(); band.setHeight(20); jasperDesign.setColumnHeader(band); // end of column header band //detail band band = new JRDesignBand(); band.setHeight(20); ((JRDesignSection) jasperDesign.getDetailSection()).addBand(band); // end of detail band // column footer band band = new JRDesignBand(); band.setHeight(20); jasperDesign.setColumnFooter(band); // end of column footer band // page footer band band = new JRDesignBand(); band.setHeight(20); jasperDesign.setPageFooter(band); // end of page footer band // summary band band = new JRDesignBand(); band.setHeight(20); jasperDesign.setSummary(band); // end of summary band // background band band = new JRDesignBand(); band.setHeight(20); jasperDesign.setBackground(band); // end background band return jasperDesign; } [/php] |
Now here is code to make jrxml file from jasperDesign.
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 |
[php] Class CreateJrxmlFromJasperDesign(){ protected JasperPrint jp; protected JasperReport jr; protected Map params = new HashMap(); public void testReport() throws Exception { JasperDesignForTemplate jasperDesignForTemplate = new JasperDesignForTemplate(); jr = buildReport(); jp = JasperFillManager.fillReport(jr, params); exportReport(); } protected void exportReport() throws Exception { ReportExporter.exportReport(jp, “/path/to/directory/file.pdf”); exportToJRXML(); } protected void exportToJRXML() throws Exception { if (this.jr != null) { System.out.println("application path--------------------" + applicationPath); DynamicJasperHelper.generateJRXML(this.jr, "UTF-8", dir + "/" + fileName + ".jrxml"); } } } [/php] |
Once jrxml file generate , you can use for download your file in any format(e.g: pdf,). For download file using grails you can follow this link.
Recent Comments