简体   繁体   中英

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;
   }   

}

You can try this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM