Slient/Direct Printing In Servoy

Questions and answers on designing your Servoy solutions, database modelling and other 'how do I do this' that don't fit in any of the other categories

Slient/Direct Printing In Servoy

Postby ryan1680098789 » Mon Feb 05, 2024 7:03 pm

I'm looking to add direct printing capability within our web application. I've found this previous forum post about it: https://forum.servoy.com/viewtopic.php?t=21298 and still seem to run into some issues. Just wondering if anyone else can provide some insight if they have added this to their application and if so how it was done. Thanks.
ryan1680098789
 
Posts: 13
Joined: Wed Mar 29, 2023 4:06 pm

Re: Slient/Direct Printing In Servoy

Postby sbutler » Tue Feb 06, 2024 8:01 am

The way I've done it in the past is have a queue where the clients print job goes. This queue is usually a table of some sort. The server then has a batch processor which watches the queue and sends the job directly to the printer via command line. On Mac, thats with LPR. Windows can do it with Adobe reader command line.
Mac example here: https://servoyguy.com/code-repository/s ... a-printer/
Scott Butler
iTech Professionals, Inc.
SAN Partner

Servoy Consulting & Development
Servoy University- Training Videos
Servoy Components- Plugins, Beans, and Web Components
Servoy Guy- Tips & Resources
ServoyForge- Open Source Components
User avatar
sbutler
Servoy Expert
 
Posts: 759
Joined: Sun Jan 08, 2006 7:15 am
Location: Cincinnati, OH

Re: Slient/Direct Printing In Servoy

Postby Richard1521662995 » Fri Feb 09, 2024 12:05 am

The following works for me

I use Jasper Reports to create a PDF from the report, then pass the folder and file name as the filePath

Code: Select all
/**
* Print PDF file in background. This Prints the pdf file in the preferred printer attached to server machine
*
* @param {String|plugins.file.JSFile} filePath The path to file
* @param {String} [preferredPrinter] The Printer to be used for printing. By default uses the default printer
* @param {Number} iCopies
*
* @return {Boolean} Status
* @author pradipta
* @since 10/15/2014
*
* @properties={typeid:24,uuid:"7FDD2CE9-C65F-484C-A8A9-DCA8AEB737E2"}
*/
function silentPrint(filePath, preferredPrinter, iCopies) {

   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().toUpperCase() == preferredPrinter.toUpperCase()) {
            printerJob.setPrintService(_printServices[_i]);
            break;
         }
      }

      // Validate of the desired printer found
      if (_i == _printServices.length) {
         plugins.dialogs.showErrorDialog('Print', preferredPrinter + ' cannot find in installed printers')
         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) {
      plugins.dialogs.showErrorDialog('Print Report', preferredPrinter + ' found, but unable to set the Printer Service');
      return false;
   }

   try {

      // Get Document and Print
      var file = new Packages.java.io.File(filePath);
      var printDocument = Packages.org.apache.pdfbox.pdmodel.PDDocument.load(file);
      if (!printDocument) {
         plugins.dialogs.showErrorDialog('Print Report', 'Unable to start Print Job');
         return false;
      }

      // Print Document
      printerJob.setPageable(new Packages.org.apache.pdfbox.printing.PDFPageable(printDocument));
      printerJob.setJobName("Aquarius PDF Print Job");

      if (arguments.length == 3) {
         printerJob.setCopies(iCopies);
      } else {
         printerJob.setCopies(1);
      }

      // Set the attributes
      var printAttributes = new Packages.javax.print.attribute.HashPrintRequestAttributeSet;
      var pageRange = new Packages.javax.print.attribute.standard.PageRanges(1, printDocument.getNumberOfPages());

      printAttributes.add(pageRange);

      printerJob.print();

   } catch (exception) {
      application.output(exception.message);
   } finally {
      if (printDocument) {
         printDocument.close();
      }
   }

   return true;
}
Richard Clarke
acss.co.uk
Richard1521662995
 
Posts: 40
Joined: Wed Mar 21, 2018 10:09 pm


Return to Programming with Servoy

Who is online

Users browsing this forum: No registered users and 28 guests

cron