Java - how to print the PDF to a specific printer?
<p>I have 5 printers in Windows 8.1 and the PDF file is not in local system its generated in PHP server.</p>

<p>Question. how can i get the PDF file from the server and print to a specific printer? </p>

<p>I am trying with Apache PDFBox 2.0.0</p>

<p><a href="https://i.stack.imgur.com/PUOSJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PUOSJ.png" alt="enter image description here"></a></p>

<p><strong>EDIT:</strong></p>

<pre><code>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 org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.printing.PDFPageable;

public class JPrint {

  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)) &gt; -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:/Users/tpt/Downloads/pdf.pdf";
    String download_pdf_from = "https://github.com/msysgit/msysgit/releases/download/Git-1.9.2-preview20140411/Git-1.9.2-preview20140411.exe";
    String downloaded_filename_open_as_pdf = "C:\\Users\\tpt\\Downloads\\pdf.pdf";
    String printerNameDesired = "DYMO LabelWriter 450"; // Brother HL-6180DW series

    // Get printers
    PrintService[] services = PrinterJob.lookupPrintServices();
    DocPrintJob docPrintJob = null;


    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 &lt; services.length; i++) {
           if (services[i].getName().equalsIgnoreCase(printerNameDesired)) {
             docPrintJob = services[i].createPrintJob();
           }
          }

          job.setPrintService(docPrintJob.getPrintService());
          job.setPageable(new PDFPageable(pdf));
          //docPrintJob = service[i].createPrintJob();
          job.print();

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


  }
}
</code></pre>