To take full advantage of Wubiq, you must become familiar with the Java Printing API. While you can dig deep into it, this section will clarify the most relevant topics.
All printing occurs through a printer device represented by a PrintService object. A PrintService expose all the require characteristics (???) for producing a printing output. So in order to print you must indicate to which PrintService. The API provides several methods for acquiring a proper PrintService, most of them in the PrintServiceLookup class.
Once you have a PrintService, you might need to define the attributes required for printing, such as printing media, copies, color, orientation, etc. These attributes implements one or more of the following interfaces: DocAttribute, PrintRequestAttribute, PrintJobAttribute and PrintServiceAttribute.
Of course, you can print a PDF, PNG, JPG, text document. As you may know the document can be in a input stream, in a byte array, char array or a reader. You have to tell to the API which flavor of document are you printing. For example a PDF can be read through a input stream thus your DocFlavor is DocFlavor.INPUT_STREAM.PDF.
Selecting the DocFlavor is not that simple, because PrintService has clearly defined capabilities. You can have a PrintService that can process almost any type of DocFlavor, but you can also have a PrintService that can handle only one or two flavors. The only way to know is to ask the PrintService itself with PrintService.isDocFlavorSupport(). But let me tell you that almost all PrintService instances are capable of handling DocFlavor.SERVICE_FORMATTED.PAGEABLE and DocFlavor.SERVICE_FORMATTED.PRINTABLE.
Somebody must take care of the print job. The API preferred way is through the DocPrintJob, as this implementation can route all DocFlavor types. Also you (or your application) can use PrinterJob instance which can only process DocFlavor.SERVICE_FORMATTED.PAGEABLE and DocFlavor.SERVICE_FORMATTED.PRINTABLE. Wubiq can take care of both printing ways.
DocPrintJob allows you to print almost any type of document. Suppose that you have a png image that you wish to print to the default printer.
//Let's make a input stream out of the png image.
InputStream image = new FileInputStream("my_image.png");
// Let's select the print service, in this case the default printer.
PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
// It is not a good practice to assume that you have a valid PrintService.
if (printService == null) {
throw new RuntimeException("Oops!, you don´t have any printer");
}
// It is a good practice to check if the selected PrintService can handle the document.
if (!printService.isDocFlavorSupported(DocFlavor.INPUT_STREAM.PNG)) {
throw new RuntimeException("Printer can't handle png´s");
}
// Now let's create the Doc object
DocAttributeSet docAttributes = new HashDocAttributeSet();
docAttributes.add(OrientationRequested.LANDSCAPE);
Doc doc = new SimpleDoc("image", DocFlavor.INPUT_STREAM, docAttributes);
// Once you have a PrintService and a Doc, you need the print job.
DocPrintJob printJob = printService.createPrintJob();
// Then set the print job and print request attributes
PrintRequestAttributeSet requestAttributes = new HashPrintRequestAttributeSet();
requestAttributes.add(MediaSizeName.NA_LETTER);
printJob.print(doc, requestAttributes);// Let's gather the PrintService first
PrintService printService = PrintServiceLookup.lookupDefaultPrintService()
if (printService == null) {
throw new RuntimeException("Printer not available");
}
// Get a new instance of PrinterJob. You must first set it up to let Wubiq handle the printing routing.
// The initialization is only required once per JVM instance, calling it multiple times is not harmful.
PrinterJobManager.initializePrinterJobManager();
PrinterJob printerJob = PrinterJob.getPrinterJob();
// Sets the print job and print request attributes. Notice that OrientationRequested also implements PrintRequestAttribute
PrintRequestAttributeSet requestAttributes = new HashPrintRequestAttributeSet();
requestAttributes.add(MediaSizeName.NA_LETTER);
requestAttributes.add(OrientationRequested.LANDSCAPE);
// Open the PDF document
InputStream pdfDoc = new FileInputStream("My_Document.pdf");
// Wubiq can convert PDF to Pageable. This document must be closed after printing.
Pageable pageable = PdfUtils.INSTANCE.pdfToPageable(pdfDoc);
try {
printerJob.setPageable(pageable);
printerJob.print(requestAttributes);
} finally {
PdfUtils.INSTANCE.closePageable(pageable);
}
As you can see Pageable and Printable interface are treated as DocFlavor.SERVICE_FORMATTED, and this means that the service formats or interprets the document sent to it. So you will see that the same document might look different when printed from different operating system platform or printers (PrintService).
When printing try to use a PDF renderer if your printer supports PDF documents. Converting PDF to images does not produce good quality output. Converting PDF to Pageable produces the most accepted output, but each implementation of the print service handle it differently thus producing different visible output from the same report and data.
Most dot matrix printers can receive print command with Epson ESCP/2 compatible commands. Non-windows drivers outputs the document with acceptable resolution. However is never closed to direct printing as Windows driver does. So if your network includes printing to Dot Matrix connected to non-windows computers, you might want to take a look at wubiq's API to print using TextPageable, which enables clearer and better quality printing.