Jasperberry - the most dynamic reports ever.

Jasperberry – the most dynamic reports ever.

Today I will describe in short my latest and simplest quick and dirty trick, generating jasper report dynamically. Jasperberry is a component written very quickly to help me
generate pdf report in a situation where I don’t know the number of columns I will have, I don’t know their widths, the data they contain etc, all is generated at runtime dynamically.
I know that there is DynamicJasper project but it seems to be dead, and its dead in a very strange way, I mean, I see that the authors commit new code but there is no chance to get any support unless, I guess, you pay for it.
With dynamic jasper I couldn’t generate report with polish fonts displayed properly, tried many different tricks, no support on the forum, no support on stack overflow, no nothing. After 2 days I have realized that I will write my own library quicker to solve my problem than get an answer from the dynamic jasper community (if there is any).
So here we have: jasperberry!


Don’t expect too much from it but if you want to display simple table with data, dynamic columns, adjustable fonts and colors than this is it.

In fact you don’t have to even know object properties to generate the report, we will use Apache DynaBeans for that:

This is a simple example code snipped showing you how to get started with the simplest and most dynamic reporting library ever (as well as lacking features probably but we hope its enough for simple projects)

This example assumes the following:

1. You want simple report
2. You dont have to know the object which properties you want to display in advance, by this I mean you can create report columns dynamically, and by using apache dyna beans your objects don’t have to even exists. You just dynamically create them and inject them into the report.

Here is an example test class showing you how to use jasperberry, for more info please contact us directly at softberries.com
Code:

public class JRXMLBuilderTest {

    private static List<JBColumnBean> columns;

    public JRXMLBuilderTest() {
    }

    @BeforeClass
    public static void setUpClass() throws Exception {
        columns = new ArrayList<JBColumnBean>();
        JBColumnBean col1 = new JBColumnBean();
        JBColumnBean col2 = new JBColumnBean();
        JBColumnBean col3 = new JBColumnBean();
        JBColumnBean col4 = new JBColumnBean();
        JBColumnBean col5 = new JBColumnBean();
        JBColumnBean col6 = new JBColumnBean();
        JBColumnBean col7 = new JBColumnBean();
        col1.setPreferredWidth(60);
        col1.setProperty("name");
        col1.setTitle("Imię");
        col2.setPreferredWidth(60);
        col2.setProperty("surname");
        col2.setTitle("Nazwisko");
        col3.setPreferredWidth(160);
        col3.setProperty("comment");
        col3.setTitle("Komentarz");
        col4.setPreferredWidth(100);
        col4.setProperty("comment2");
        col4.setTitle("Komentarz");
        col5.setPreferredWidth(100);
        col5.setProperty("comment3");
        col5.setTitle("Komentarz");
        col6.setPreferredWidth(100);
        col6.setProperty("comment4");
        col6.setTitle("Komentarz");
        col7.setPreferredWidth(100);
        col7.setProperty("comment5");
        col7.setTitle("Komentarz");
        columns.add(col1);
        columns.add(col2);
        columns.add(col3);
        columns.add(col4);
        columns.add(col5);
        columns.add(col6);
        columns.add(col7);
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
    }

    @Before
    public void setUp() {
    }

    @After
    public void tearDown() {
    }

    /** * Test of build method, of class JRXMLBuilder. */
    @Test
    public void testBuild() {
        try {
            System.out.println("build");
            List<JBReportElement> elements = new ArrayList<JBReportElement>();
            //col header font
            JBFont colHeaderFont = new JBFont();
            colHeaderFont.setBold(true);
            colHeaderFont.setSize(10);
            colHeaderFont.setForegroundColor("#555555");
            //detail font
            JBFont detailFont = new JBFont();
            detailFont.setSize(6);
            detailFont.setForegroundColor("#000000");
            detailFont.setPdfEncoding("Cp1257");
            JBPage page = new JBPage();
            JBReport report = new JBReport(page, colHeaderFont, detailFont, "#CCCCFF", "#DEDEDE", "#FFFFFF");
            JBFont titleFont = new JBFont();
            titleFont.setBold(true);
            titleFont.setSize(20);
            titleFont.setForegroundColor("#FF0000");
            titleFont.setPdfEncoding("Cp1257");
            //add title elements
            elements.add(new JBStaticTextElement("Hello World!", 450, 10, 300, titleFont));
            elements.add(new JBImageElement("C:\\\\Documents and Settings\\\\admin\\\\Moje dokumenty\\\\NetBeansProjects\\\\JASPER_BERRY\\\\test.jpg", 0, 0, 350, 242));
            report.setTitleElements(elements);
            JRXMLBuilder instance = new JRXMLBuilder(columns, report);
            boolean expResult = true;
            boolean result = instance.build();
            JasperReport jr = JasperCompileManager.compileReport("jasperberry_by_softberries.jrxml");
            //PREPARE SOME DATA
            DynaProperty[] props = new DynaProperty[columns.size()];
            for (int i = 0; i < columns.size(); i++) {
                props[i] = new DynaProperty(columns.get(i).getProperty(), String.class);
            }
            BasicDynaClass dynaClass = new BasicDynaClass("tester", null, props);
            List<DynaBean> beans = new ArrayList<DynaBean>();
            for (int i = 0; i < 100; i++) {
                DynaBean db = dynaClass.newInstance();
                db.set("name", "Krzysztof " + i);
                db.set("surname", "Grajek " + i);
                db.set("comment", "Komentarz ");
                db.set("comment2", "Komentarz ");
                db.set("comment3", "Komentarz ");
                db.set("comment4", "Komentarz ");
                //
                db.set("comment5", "Komentarz ");
                beans.add(db);
            }
            JasperPrint jp = JasperFillManager.fillReport(jr, null, new JRBeanCollectionDataSource(beans));
            JasperExportManager.exportReportToPdfFile(jp, "jasperberry.pdf");
            assertEquals(expResult, result);
        } catch (Exception ex) {
            Logger.getLogger(JRXMLBuilderTest.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

You should find similar snippet in a test suite of the project.
The project itself is licensed under LGPL and its available from sourceforge here

10 responses on “Jasperberry – the most dynamic reports ever.

  1. Dj Mamana March 23, 2010 at 2:45 am

    Hi there, this is Juan (AKA: Dj) from the dynamic jasper (DJ) team.
    I’m sorry to hear we dissapointed you with the required support you needed.
    We are always trying to release new versions, but to be truth, we are up to the neck of work. We have no intention to have only paid support, that’s why we have tons of examples and the forums, but sometimes we are really loaded of work and cannot answers to the questions in the forum.

    We made DJ free and open source for the community and we’ll keep it like this, but it is also true that it is not DJ the project that pays my rent 🙁

    regards
    Dj

  2. admin March 23, 2010 at 9:51 am

    Hello Juan,

    Its a pleasure to have a comment from you on my tiny blog, I mean it.
    I didn’t want to be rude or anything, I was just trying to explain myself why I did the library at all, as dynamic jasper (except for the font problem 🙂 ) is a great project.
    Keep up the good work.
    Cheers

  3. Azia March 25, 2010 at 8:10 am

    Hi there,

    Many many Thanks for this but can you please tell me about the file “jasperberry_by_softberries.jrxml” ? is it will be any empty file created in Jasperreports? or some work required at designing of this file?

    Regards

  4. admin March 25, 2010 at 9:06 am

    The .jrxml file is created like a normal report file in e.g iReport software, but instead of visual designer, the library does the work of including special sections inside that file. Basically, all the jasperberry library does is to construct that file, then the jrxml is compiled to ‘jasper’ file and the report is ready.
    I hope I did make it clearer.
    Regards
    Kris

  5. Azia March 26, 2010 at 3:02 pm

    Thanks you so much for your reply and time.
    can you please guide me is Jaspwerberry is free and from which location I can download..
    Does dynamic jasper community has given sample project ? I have downloaded but there was no such dynamic report sample.

  6. admin March 27, 2010 at 10:16 am

    I’m not sure If I’ve grasped exactly your needs. First, both projects ‘DynamicJasper’ and ‘JasperBerry’ are opensource, DynamicJasper is much more sophisticated than JasperBerry, I have created JasperBerry cos I had problems with ‘Dynamic Jasper’.
    You can find dynamic jasper project page here: http://dynamicjasper.sourceforge.net/
    and my small jasperberry library here: http://jasperberry.sourceforge.net/
    Cheers

  7. Azia March 29, 2010 at 2:12 pm

    Nice effort thanks and good luck

  8. aravind June 4, 2010 at 11:43 am

    HI Really Great Work

  9. djo July 1, 2010 at 12:13 pm

    hello it’s great work
    i have tried dynamic jasper and i will try using jasperberry also.
    for dynamic jasper they provide a war file for demo “Dynamic Jasper Playground”.
    did you provide a war file like this?

  10. viren balaut November 30, 2010 at 6:42 pm

    I am bit worried if we can add the Jfree with the help of these. I am not fiding adding the Jfree Chart with the help of dynamic jasper report/

Leave a Reply