Friday, May 22, 2020

Dev Tips#39 Jasper report in Java 8 and Netbeans 8

In this video I show you how to create a jasper report in Java 8 and Netbeans 8 with dependencies handled by maven to export a PDF. How to export a PDF in java.




Settings.xml:




  

    <mirrors>
        <mirror>
            <id>mirror1</id>
            <mirrorOf>central</mirrorOf>
            <name>mirror1</name>
            <url>https://repo.maven.apache.org/maven2</url>   
        </mirror>
    </mirrors>

  
  


Dependencies:




  

    <dependencies>
        <dependency>
    <groupId>net.sf.jasperreports</groupId>
    <artifactId>jasperreports</artifactId>
    <version>6.8.0</version>
    <type>jar</type>
    <scope>compile</scope>
    <exclusions>
        <exclusion>
            <artifactId>commons-collections</artifactId>
            <groupId>commons-collections</groupId>
        </exclusion>
        <exclusion>
            <artifactId>commons-beanutils</artifactId>
            <groupId>commons-beanutils</groupId>
        </exclusion>
        <exclusion>
            <artifactId>commons-digester</artifactId>
            <groupId>commons-digester</groupId>
        </exclusion>
        <exclusion>
            <artifactId>commons-logging</artifactId>
            <groupId>commons-logging</groupId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
<groupId>commons-digester</groupId>
<artifactId>commons-digester</artifactId>
<version>2.1</version>
<scope>compile</scope>
<optional>false</optional>
</dependency>

<dependency>
    <groupId>net.sf.jasperreports</groupId>
    <artifactId>jasperreports-fonts</artifactId>
    <version>6.8.0</version>
</dependency>
    </dependencies>

  
  


Java code:




  
package com.mycompany.mavenprojectdemov1;

import java.util.HashMap;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader; 
/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
      

        String fileNameJrxml = "E:/Jasper/HelloPDF.jrxml"; 
        String fileNamePdf = "E:/Jasper/HelloPDFv2.pdf";
 
        try {
            System.out.println("Loading the .JRMXML file ....");
            JasperDesign jasperDesign = JRXmlLoader.load(fileNameJrxml);
            System.out.println("Compiling the .JRMXML file to .JASPER file....");
            JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
            HashMap hm = new HashMap();
            System.out.println("filling parameters to .JASPER file....");
            JasperPrint jprint = (JasperPrint) JasperFillManager.fillReport(jasperReport, hm, new JREmptyDataSource());
            System.out.println("exporting the JASPER file to PDF file....");
            JasperExportManager.exportReportToPdfFile(jprint, fileNamePdf);
            System.out.println("Successfully completed the export");

        } catch (Exception e) {
            System.out.print("Exception:" + e);
        }

    }
}

  




No comments:

Post a Comment