JasperReport, show and print report

24,733

Solution 1

You're specifying the JasperPrint file and not the JasperReport file. Let me break down the files and what they are:

  • report.jrxml - An xml definition of a jasper report - this defines a report, but cannot be used directly to generate output.
  • report.jasper - A compiled jrxml file (JasperReport). This can be used as input to fill the report with data.
  • report.jprint - A report that's been filled with data, and is ready to be exported to multiple output formats

Here's some code to start with the jrxml file the designer creates to get you to an printed pdf output:

Connection connection = PersistenceSessionFactory.getSqlSession().getConnection();
JasperReport report = JasperCompileManager.compileReport( "FancyPantsReport.jrxml" );

// setup parameters for use with the report
HashMap<String, Object> params = new HashMap<String,Object>();
params.put( "sqlDate", fromDate );

// Fill the report data from the sql connection and parameters
JasperPrint printedReport = JasperFillManager.fillReport(report, params, connection);

String outputFilename = "FancyPants-" + dateString + ".pdf";
JasperExportManager.exportReportToPdfFile( printedReport, outputFilename );

LOG.info("Report Generated in " + (System.currentTimeMillis() - start) + "ms");

Notice it uses the compile to get a JasperReport from the jrxml, then the FillManager to get a JasperPrint from the JasperReport, and finally exports the JasperPrint to pdf.

Solution 2

You can use Jasper viewer to preview reports and print it.

Here is an example!

public void generateReport() throws PrinterException {

try {  
String sourceFileName = "src/bill/report.jasper";
String printFileName = null;
Purchase_BeanFactory DataBean = new Purchase_BeanFactory();
JRBeanCollectionDataSource beanColDataSource = new     JRBeanCollectionDataSource(DataBean.generateCollection());
Map parameters = new HashMap();
printFileName = JasperFillManager.fillReportToFile(
     sourceFileName,
     parameters,
     beanColDataSource);

JasperViewer jv=new JasperViewer("src/bill/report.jrprint", false, false);

//set title for the jasper viewer
jv.setTitle("Your Title");

jv.setVisible(true);
//set icon to the jasper viewer
jv.setIconImage(
(new 
ImageIcon(getClass().getResource("path/to/image.png")).getImage())); 

} catch (Exception e) {
System.out.println("e");
} 
}

Solution 3

if you want to print a JasperReport you have to call the fillReport with a JasperReport file (*.jasper).

If you want to get an PDF file you may use following source:

JRPdfExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE, outFile);
exporter.exportReport();

jp is here your *.jrprint file.

Solution 4

You can use the following to produce and print the report:

JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(list);
InputStream jasperStream = YourClass.class.getResourceAsStream(TEMPLATE_BASE_PATH);
 JasperPrint jasperPrint = JasperFillManager.fillReport(jasperStream, parameters, dataSource);
JasperViewer viewer = new JasperViewer(jasperPrint, false);
viewer.setVisible(true);
Share:
24,733
blow
Author by

blow

Updated on July 05, 2022

Comments

  • blow
    blow almost 2 years

    I exported a .jrprint file created with iReport. Now I want to preview the report and finally print it, how can I do this?

    I'm trying with:

    JRBeanCollectionDataSource ds=new JRBeanCollectionDataSource(list);
    JasperPrint jrprint=JasperFillManager.fillReport("report.jrprint", null, ds);
    

    But I have this exception

    java.lang.ClassCastException: net.sf.jasperreports.engine.JasperPrint cannot be cast to net.sf.jasperreports.engine.JasperReport
    
  • Kieveli
    Kieveli over 11 years
    Huh? Just starting with Jasper, and there's not enough detail here.