Printing PDF Silently in Servoy

While developing a project, we come across some requirement to print PDF files silently. The challenge was to push the file to a selected or desired printer. It shouldn’t always print in the default printer. Also it should work in Servoy Web client. Here let me clarify that this code looks for the printers connected in Servoy Server machine for web client.

After quite some investigation we come across some java code that runs fine for the requirement. This code uses Apache PDF Box java Library.

/**
 * Print PDF file in background. This Prints the pdf file in the preferred printer attached to server machine
 * 
 * @param {String} filePath The path to file
 * @param {String} [preferredPrinter] The Printer to be used for printing. By default uses the default printer
 * @return {Boolean} Status
 * @author pradipta
 * @since 10/15/2014
 * 
 * @properties={typeid:24,uuid:"7FDD2CE9-C65F-484C-A8A9-DCA8AEB737E2"}
 */
function silentPrint(filePath, preferredPrinter) {
 
 var printerJob = Packages.java.awt.print.PrinterJob.getPrinterJob();
 
 // Find the Preferred Printer
 if (preferredPrinter) {
 /** @type {Array<javax.print.PrintService>} */
 var _printServices = Packages.javax.print.PrintServiceLookup.lookupPrintServices(null, null)
 for (var _i = 0; _i<_printServices.length; _i++) {
 if (_printServices[_i].getName() == preferredPrinter) {
 printerJob.setPrintService(_printServices[_i]);
 break;
 }
 }
 
 // Validate of the desired printer found
 if (_i == _printServices.length) return false;
 } else {
 
 // Find the Default printer;
 /** @type {javax.print.PrintService} */
 var _printService = new Packages.javax.print.PrintServiceLookup.lookupDefaultPrintService();
 if (!_printService) return false;
 printerJob.setPrintService(_printService);
 }
 
 // Check Printer Job
 if (!printerJob) return false;
 
 try {
 
 // Get Document and Print
 var printDocument = Packages.org.apache.pdfbox.pdmodel.PDDocument.load(filePath);
 if (!printDocument) return false;
 
 // Print Document
 printDocument.silentPrint(printerJob);
 }
 catch (exception) { }
 finally {
 printDocument.close();
 }

 return true;
}

Lets go through the code little bit.

var _printServices = Packages.javax.print.PrintServiceLookup.lookupPrintServices(null, null)
for (var _i = 0; _i<_printServices.length; _i++) {
if (_printServices[_i].getName() == preferredPrinter) {
printerJob.setPrintService(_printServices[_i]);
break;
}
}

This is basically looping through the available Printer Services and associates the Printer Job object with the Printer Service matching our selected Printer name.

var _printService = new Packages.javax.print.PrintServiceLookup.lookupDefaultPrintService();

When no Preferred printer name is provided, it looks for the default printer available and uses that printer for printing.

var printDocument = Packages.org.apache.pdfbox.pdmodel.PDDocument.load(filePath);

We are using Apache PDF Box library to read the pdf file. This jar need to be copied to Beans folder in application_server. You can Download it from here.

printDocument.silentPrint(printerJob);

This sends the loaded file into the printer’s job Queue silently.