2
\$\begingroup\$

I have following Java code, which is to print PDF, but in a busy location this code is taking about 20 to 30 second delay before submitting to the hardware.

  1. get the PDF file via the network
  2. download and save locally
  3. then submit local file, to the print job to printer
  4. printer hardware warm up
  5. printer hardware prints

Whole job takes about 1 to 2 minute (depending on the hardware mode, and networking mode).

I am getting lot of complaints and I can't seem to find any issue in my version. Is there any improvement I can make in the following code which might boost the speed?

While testing, it seems like the delay is causing when the file is saved to the disk and read from the disk.

import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.MediaTray;
import javax.print.attribute.standard.Slides;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.printing.PDFPageable;

public class IPrint {

  public static boolean saveFile(URL url, String file) throws IOException {
    boolean download_status = false;

    System.out.println("[OK] - open");
    InputStream in = url.openStream();
    FileOutputStream fos = new FileOutputStream(new File(file));
    System.out.println("[OK] - reading file...");
    int length = -1;
    byte[] buffer = new byte[1024];

    while ((length = in.read(buffer)) > -1) {
        fos.write(buffer, 0, length);
    }
    fos.close();
    in.close();

    download_status = true;
    System.out.println("[OK] - downloaded");
    return download_status;
  }

  public static void main(String[] args) throws IOException, PrinterException {  
    String downloaded_filename = "C:/Python27/pdf.pdf";
    String download_pdf_from = "http://www.example.com/" + args[0];
    String downloaded_filename_open_as_pdf = "C:\\Python27\\pdf.pdf";

    String printerNameDesired = "Brother HL-6180DW series";

    PrintService[] services = PrinterJob.lookupPrintServices();
    DocPrintJob docPrintJob = null;
    for (int i = 0; i < services.length; i++) {
      System.out.println(services[i]);
    }   

    try{
      URL url = new URL(download_pdf_from);

      if(saveFile(url, downloaded_filename)) {
        try {
          PDDocument pdf = PDDocument.load(new File(downloaded_filename_open_as_pdf));
          PrinterJob job = PrinterJob.getPrinterJob();
          for (int i = 0; i < services.length; i++) {
           if (services[i].getName().equalsIgnoreCase(printerNameDesired)) {
             docPrintJob = services[i].createPrintJob();
           }
          }

          job.setPrintService(docPrintJob.getPrintService());
          job.setPageable(new PDFPageable(pdf));

          PrintRequestAttributeSet pset = new HashPrintRequestAttributeSet();

          //'top': The top input tray in the printer.
          //'middle': The middle input tray in the printer.
          //'bottom': The bottom input tray in the printer.
          //'envelope': The envelope input tray in the printer.
          //'manual': The manual feed input tray in the printer.
          //'large-capacity': The large capacity input tray in the printer.
          //'main': The main input tray
          //'side': The side input tray

          //pset.add(MediaTray.TOP);// From top to bottom, first tray1
          pset.add(MediaTray.BOTTOM); // from top to bottom, the last tray2
          pset.add(Sides.DUPLEX);
          job.print(pset);

        } catch (Exception e) {
          System.out.println("[FAIL]" + e);
        }      
      } else {
        System.out.println("[FAIL] - download fail");
      }      
    } catch (Exception ae) {
      System.out.println("[FAIL]" + ae);
    }


  }

}
\$\endgroup\$
  • \$\begingroup\$ You should run the profiler on it (VisualVM is with the JDK, so try that). Then you can determine what exactly is slow \$\endgroup\$ – Justin Nov 11 '16 at 13:23
  • \$\begingroup\$ All is very good time is 2 seconds \$\endgroup\$ – Carlos Mollapaza Sep 17 '18 at 17:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.