White Report with jasper report from Java

562 Views Asked by At

I have a problem with jasper report: I created a report with iReport 5.6.0 and it is operating normally. It contains only parameters. My problem comes from java: when I try to create a pdf file from my java class, it creates a file with all white size 1 kb. From IReport it works properly. I tried to compile the ".jasper" file by iReport and also from java. The compilation id done properly but when I try to pass parameters is wrong. PDF creation is successful in the end but the page is all white. Helppppp !!!

p.s. Excuse me for may bad english!!

My java class:

package it.minervaice.report;

import it.minervaice.utility.T;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;

public class ReportUtility {              
String JASPER_REPORT_FOLDER         = "C:\\Users\\Minerva\\Desktop\\Dropbox\\workspace\\MinervaICE\\WebContent\\report\\";
String JASPER_FILENAME_MAIN_DATE    = "distintaCompletaPerData";
String JASPER_FILENAME_MAIN_ID      = "distintaCompletaPerID";
String JASPER_FILENAME_CHILD        = "dettaglioRicetta";   

JasperPrint jp = null;

public JasperPrint getReportCompilatoPerFlusso () {
    return jp;
}


// II° STEP:    rendering del report
public boolean renderingReportDaID(Integer id_distinta) {
    //Connection conn = DBUtility.getConnection();

    try {
        //Parametri da passare al jasper.
        Map<String, Object> parameters = new HashMap<> ();

        parameters.put("id_distinta", id_distinta);

        T.nota("inizio rendering");
        jp = JasperFillManager.fillReport(JASPER_REPORT_FOLDER + "java.jasper", parameters);
        T.nota("fine rendering");

    } catch (JRException e) {
        T.nota("errore rendering");
        e.printStackTrace();
        T.nota("errore rendering");
    }
    return true;
}



// III° STEP:   creazione pdf
public boolean creaPDF(String nomePDF) { 
    if ( nomePDF.equals("") ) {
        nomePDF = "report";
    }

    // generazione del file PDF
    try {
        T.nota("inizio creazione pdf");
        JasperExportManager.exportReportToPdfFile(jp, JASPER_REPORT_FOLDER + nomePDF + ".pdf");
        T.nota("fine creazione pdf");
    } catch (JRException e) {
        T.nota("errore creazione pdf"); e.printStackTrace();T.nota("errore creazione pdf");
    }

    return true;
}


// III° bis STEP:   scarica pdf con la finestra di download
public boolean downloadPDF(String nomePDF, HttpServletResponse resp) { 
    if ( nomePDF.equals("") ) {
        nomePDF = "report";
    }

    // generazione del file PDF
    try {
        T.nota("inizio creazione download pdf");

        String header = String.format("Attachment; Filename=\"%s.pdf\"", nomePDF );
        ServletOutputStream sos=resp.getOutputStream();
        resp.setContentType("APPLICATION/OCTET-STREAM");
        resp.setHeader("Content-Disposition", header);
        JasperExportManager.exportReportToPdfStream(jp, sos);
        sos.close();
        T.nota("fine creazione download pdf");
    } catch (JRException e) {


    } catch (IOException e) {
        T.nota("errore creazione download pdf");
        e.printStackTrace();
        T.nota("errore creazione download pdf");
    }

    return true;
}   


public static void main(String[] args) {

    ReportUtility ru = new ReportUtility();
    //ru.creaJasper();
    ru.renderingReportDaID(1);
    ru.creaPDF("ciccio");



}

}
1

There are 1 best solutions below

0
On

May be empty value that you are passing to Jasper Report via HashMap object. Therefore, Jasper Report creates PDF file without data. You have to check the values is passing or not. Bellow is the snap code and query in the Jasper Report : Query-

"SELECT * FROM testTable where id=$P{EMPID}"

. Heare EMPID is a parameter created at Jasper Report. Now the Java code to call the Jasper Report and passing the value to it as bellow....

HashMap<String, Object> hm=new HashMap<String, Object>();
hm.put("EMPID", objEmp.getEmpid());

String jasperreportpath="salaryslip.jrxml";    
String jasperReport =JasperCompileManager.compileReportToFile(jasperreportpath);

String exportPath=output_file_location_path+File.separator+"outputFileName.pdf";
JasperExportManager.exportReportToPdfFile(jasperPrint, exportPath); 

Remember, the key in HashMap object and parameter at Jasper Report are the same 'EMPID'.