jayz
jayz

Reputation: 401

Print Jasper Report Using a Printer

I am creating a report using Jasper report and save it as a pdf file in the drive. I want to get a print out using a printer of that report. After a brief google search I found there are two ways to do that. 1)open the pdf in the print preview mode and then print 2)send the jasperPrint file directly to the the printer.

Below is the code that I have done so far. Can someone please guide me to do one of the above methods using java in java web application. Thank you.

public int printCashReceipt(){
try{
   Connection connection = util.DatabaseConnection.getDbConnection();
   JasperReport jasperReport = JasperCompileManager.compileReport(getClass().getResourceAsStream("../reports/ReceiptCash.jrxml"));

   Map<String, Object> map = new HashMap<>();
   map.put("receipt_id", 6);

   JasperPrint jp = JasperFillManager.fillReport(jasperReport, map, connection);

   File outDir = new File("C:/reports");
   outDir.mkdirs();

   JasperExportManager.exportReportToPdfFile(jp, "C:/reports/ReceiptCash.pdf");
   System.out.println("Done!");
   connection.close();

   return 1;
   }
   catch(Exception e){
       System.out.println(e);
       return 0;
   }   

}

Upvotes: 0

Views: 2595

Answers (1)

SerefAltindal
SerefAltindal

Reputation: 349

You can try this

JasperPrint print = JasperFillManager.fillReport(jp, parameters, jrBeanDatasource);     
JasperViewer.viewReport(print, true);

Upvotes: 1

Related Questions