Wednesday, January 23, 2013

How to apply dynamic colors to jasper report ?

No comments:
How to apply dynamic colors to jasper report ?

After some googling, i found the way to apply colors dynamically to jasper report.

My working code is,

We can access all the jasper files styles and everything with the help of JasperDesign class.
Just create some styles like header, body and footer and set colors for those style from java file.

The code "styleList[j].setBackcolor(
userBackGRDColor);" - sets the back groud color of a style

    @Test
    public void dynamicColorPdf() {
        try {
            System.out.println("pdf report creation started .... ");

            String outFileNamePDF = "/Users/user/Tried/POC/
imageStreamToPdf/dynamicColorPdf.pdf";
            new File(outFileNamePDF).
getParentFile().mkdirs();
            String containerJrxmlFile = "/Users/user/Tried/POC/
imageStreamToPdf/dynamicColorPdf.jrxml";
           
            Map<String, Object> parameters = new HashMap<String,Object>();
           
            InputStream reportStream = new FileInputStream(
containerJrxmlFile);
            BufferedInputStream bufferedInputStream = new BufferedInputStream(
reportStream);
            JasperDesign jasperDesign = JRXmlLoader.load(
bufferedInputStream);
           
            JasperReport jasperReport = JasperCompileManager.
compileReport(jasperDesign);
            JasperPrint jasperPrint = JasperFillManager.fillReport(
jasperReport, parameters, new JREmptyDataSource());
           
            // applying styles
            java.awt.Color userForeColor = new java.awt.Color(255, 69, 0);
            java.awt.Color userBackGRDColor = new java.awt.Color(255, 0, 0);
            JRStyle[] styleList = jasperPrint.getStyles();
            System.out.println("styleList.length => " + styleList.length);
            for (int j = 0; j < styleList.length; j++) {
                System.out.println("styleList[j].getName() => " + styleList[j].getName());
                    if (styleList[j].getName().equals("style1")) {
                            styleList[j].setBackcolor(userBackGRDColor);
                            styleList[j].setForecolor(userForeColor);
                            jasperPrint.addStyle(styleList[j], true);
                    } else if (styleList[j].getName().equals("style2")) {
                        styleList[j].setBackcolor(userBackGRDColor);
                            styleList[j].setForecolor(userForeColor);
                            jasperPrint.addStyle(styleList[j], true);
                    }
            }

           
            JRExporter exporter = new net.sf.jasperreports.engine.
export.JRPdfExporter();
            exporter.setParameter(
JRExporterParameter.OUTPUT_FILE_NAME, outFileNamePDF);
            exporter.setParameter(
JRExporterParameter.JASPER_PRINT, jasperPrint);
            exporter.exportReport();
           
            System.out.println("pdf report created .... ");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Happy coding :)

--
Thank you.


Regards,
Kaleeswaran.S
Read More

How to image stream to jasper report ?

6 comments:
How to image stream to jasper report ?

It is very easy to pass image stream to jasper report pdf.

Steps are,

1) Convert the image to stream and set it in parameter ("logo")
2) In jrxml file , create a parameter with parameter class as "java.io.InputStream"
3) Place a image on the jrxml design, set "Image Expression" to $P{logo} and "Expression class" to java.lang.String

    @Test
    public void createPdfWithImageAsStream() {
        try {
            System.out.println("pdf report creation started .... ");

            String outFileNamePDF = "/Users/user/Tried/POC/
imageStreamToPdf/createPdfWithImageAsStream.pdf";
            new File(outFileNamePDF).
getParentFile().mkdirs();
            String containerJrxmlFile = "/Users/user/Tried/POC/
imageStreamToPdf/createPdfWithImageAsStream.jrxml";
           
            // pass logo as stream.
            Map<String, Object> parameters = new HashMap<String,Object>();
            File img = new File("/Users/user/Desktop/
wallpaper.jpg");
            InputStream fis = new FileInputStream(img);
            System.out.println("printnng")
;
            parameters.put("logo", fis);
           
            InputStream reportStream = new FileInputStream(
containerJrxmlFile);
            BufferedInputStream bufferedInputStream = new BufferedInputStream(
reportStream);
            JasperDesign jasperDesign = JRXmlLoader.load(
bufferedInputStream);
           
            JasperReport jasperReport = JasperCompileManager.
compileReport(jasperDesign);
            JasperPrint jasperPrint = JasperFillManager.fillReport(
jasperReport, parameters, new JREmptyDataSource());
           
            JRExporter exporter = new net.sf.jasperreports.engine.
export.JRPdfExporter();
            exporter.setParameter(
JRExporterParameter.OUTPUT_FILE_NAME, outFileNamePDF);
            exporter.setParameter(
JRExporterParameter.JASPER_PRINT, jasperPrint);
            exporter.exportReport();
           
            System.out.println("pdf report created .... ");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Happy coding :)

--
Thank you.


Regards,
Kaleeswaran.S
Read More

Thursday, December 13, 2012

Negative seek offset error / Checksum validation fails

No comments:
Negative seek offset error / Checksum validation fails

If you face any of these issue , while using nexus with tomcat. Do not doubt the nexus, whose all versions are stable and works fine.

This is the issue with Tomcat. I faced this issue when i use tomcat apache-tomcat-7.0.28 with nexus.

I changed the nexus version from 1.9.2 to 2.3 and tried. Faced similar issue. and changed the tomcat to apache-tomcat-7.0.23 in which it works fine.

apache-tomcat-7.0.28 might have implemented new security mechanism. I didn't care about tomcat versions .  I go with tomcat-7.0.23. If you know the reason , update it. It might help others.


--
Thank you.


Regards,
Kaleeswaran.S

Read More

Thursday, November 29, 2012

Plist File Parsing Using Java

No comments:
Plist File Parsing Using Java

Below example illustrates , how to iterate over a plist file Nodes using apache's   XMLPropertyListConfiguration

  public void infoPlistConversion(String plistPath) throws Exception {
        XMLPropertyListConfiguration conf = new XMLPropertyListConfiguration(plistPath);
        System.out.println("root node children size => "+ conf.getRootNode().getChildrenCount());
        System.out.println("conf root => " + conf.getRootNode().getChild(0).getName());
        System.out.println("conf.getRoot().getChild(0).getValue() => " + conf.getRootNode().getChild(0).getValue());
        List children = conf.getRoot().getChildren();
        printChildrenValue(children);
    }
   
    private void printChildrenValue(List children) {
        if (children instanceof List) {
            List<PListNode> configs = (List<PListNode>) children;
            for (PListNode pListNode : configs) {
                // sub nodes
                if (pListNode.getValue() == null && pListNode.getChildrenCount() > 1) {
                    List pListNodeChildren = pListNode.getChildren();
                    System.out.println("-------------------------------------------------");
                    System.out.println("plist Name root => "+ pListNode.getName());
                    printChildrenValue(pListNodeChildren);
                // just print values
                } else {
                    // if it is ConfigParam and elements are not null
                    System.out.println("plist Name => "+ pListNode.getName());
                    System.out.println("plist Value => "+ pListNode.getValue());
                }
            }
        }
    }

--
Thank you.


Regards,
Kaleeswaran.S

Read More

Monday, October 29, 2012

Tomcat Maven Plugin Remote Debugging using Eclipse

No comments:
I am developing a maven based web application. i am running the application with tomcat maven plugin like

mvn tomcat:run

I want to debug the application from eclipse.

Step 1:
export MAVEN_OPTS="-Xmx512M -XX:MaxPermSize=512m" which allocates 512 MB of memory.

Step 2:
export MAVEN_OPTS="-agentlib:jdwp=transport=dt_socket,address=2480,server=y,suspend=n"
Step 3:

run the application like mvn tomcat:run

step 4:

connect to port 2480 in eclipse remote debugging.

Enjoy. Happy Debugging.
--
Thank you.


Regards,
Kaleeswaran.S

Read More