Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Java

Printing PDF Silently in Servoy

0.00/5 (No votes)
21 Oct 2014CPOL 9.1K  
How to print 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.

Java
/**
 * 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;
}

Let's go through the code a little bit.

Java
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.

Java
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.

Java
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.

Java
printDocument.silentPrint(printerJob);

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)