JasperReport with spring MVC : print report on cli

2019-03-30 09:06发布

问题:

I am developping a Spring MVC - Angularjs application.
I need to print reports, I chose JasperReport to do that.

Before I move on, I want to know if I can generate a report, then directly print it on the default printer set up on the client computer (printer which can change according to users) without displaying it on screen.
I have been looking for answers on this specific need, but couldn't find any.

If anyone knows about it....

Source to generate report and print it:

HashMap<String, Object> params = new HashMap<String, Object>(); 
params.put("Title", "My Report");

InputStream reportStream = this.getClass().getResourceAsStream(TEMPLATE); 
JasperDesign jd = JRXmlLoader.load(reportStream);
JasperReport jr = JasperCompileManager.compileReport(jd);
JasperPrint jp = JasperFillManager.fillReport(jr, params, datasource.getDataSource());

JasperPrintManager.printReport(jp, false);

回答1:

You can create an object in HTML page which hold PDF & then print it using print() method or use java.awt.print.PrinterJob. See below examples.

  • Print embedded PDF from browser with Javascript, HTML5, AngularJS
  • Print a PDF from the browser
  • Print PDF directly from JavaScript
  • Print JasperPrint directly to printer ? - Java PrinterJob
  • Different examples using java.awt.print.PrinterJob

Hope this helps you