Menu

Developers:JasperReports_notes

Federico Alcantara

JasperReports is a popular printing engine in Java. Can be easily installed in your application. Once installed, your application can process clients requests and produce reports that can routed through Wubiq´s platform. For instance, JasperReports let you produce a PDF report or print through a Pageable / Printable interface.

Using Wubiq with JasperReports's generated PDF

With the JasperExportManager you can produce a PDF object that can be printed to appropriate PrintService(s). This is the preferred method as the output is excellent for most cases. Here is an example.

// Fill the report
JasperPrint jasperPrint = JasperFillManager.fillReport("MyReport.jasper", new HashMap());
// Export it to a PDF file
ByteArrayInputStream pdf = JasperExportManager.exportToPdf(jasperPrint);
// Print it using Java Print Services
PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
Doc doc = new SimpleDoc(pdf, DocFlavor.INPUT_STREAM.PDF, null);
PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
attributes.add(MediaSizeName.NA_LETTER);
DocPrintJob printJob = printService.createPrintJob();
printJob.print(doc, attributes);

See What you MUST Know About Printing about considerations when printing.

Printing through a Pageable

You might be using an application using JasperReports or any other program that prints directly through a printer job. You can also convert a jasper report to pageable and print it using either Jasper Print Services or Printer Job. Another compelling reason to print as Pageable is that some drivers can't print PDF, but all devices are able to output Pageable and Printable elements.

See the following example:

// Fill the report
JasperPrint jasperPrint = JasperFillManager.fillReport("MyReport.jasper", new HashMap());
// Export it to a PDF file
ByteArrayInputStream pdf = JasperExportManager.exportToPdf(jasperPrint);
// Convert it to Pageable
Pageable pageable = PdfUtils.convertToPageable(jasperPrint);
// Print it using Java Print Services
PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
Doc doc = new SimpleDoc(pageable, DocFlavor.SERVICE_FORMATTED.PAGEABLE, null);
PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
attributes.add(MediaSizeName.NA_LETTER);
DocPrintJob printJob = printService.createPrintJob();
printJob.print(doc, attributes);

Take into consideration that while PDF direct printing is my preferred printing method, Pageable/Printable is the most compatible way to print, so you might end up using Pageable/Printable as your default way of printing.