title: Java code print PDF file from client machine
body: <p>I am developing Billing module in my application in Java (Struts2 framework). After successful submission of Bill form, my code generates PDF of bill. What I want to implement is, on the successful bill form submission and on successful PDF generation, the generated PDF should print from printer. I wrote the code for printing PDF, but the problem here is, by that code, it is only possible to print the PDF file from the same machine where application is deployed and printer is connected. But that is not going to be the architecture after completion of application. There is going to be client-server architecture, where my application will be deployed on one server and many client will be using that application  from their respective machines. </p>
  
  <p>So the problem here is, every other machine is connected to different printers and by my current code it is only possible to print the PDF from where the application is deployed i.e, in this case, server. </p>
  
  <p>Now my question here is:</p>
  
  <ol>
  <li><p>Is it possible to achieve PDF printing, in such scenario, using
  Java, I mean, printing PDF from printer which is connected to the respective client machine? </p></li>
  <li><p>If yes, how would I do that?</p></li>
  </ol>
  
  <p>Here is my code snippet:</p>
  
  <pre><code>import java.io.FileInputStream;
  import javax.print.Doc;
  import javax.print.DocFlavor;
  import javax.print.DocPrintJob;
  import javax.print.PrintService;
  import javax.print.PrintServiceLookup;
  import javax.print.SimpleDoc;
  import javax.print.attribute.HashPrintRequestAttributeSet;
  import javax.print.attribute.PrintRequestAttributeSet;
  import javax.print.attribute.standard.Copies;
  import javax.print.attribute.standard.Sides;
  
  public class PDFPrintDemo {
  
      public static void main(String[] args) {
          try {
  
              System.out.println("Printing PDF demo using JAVA.");
  
              DocFlavor docFlavor = DocFlavor.SERVICE_FORMATTED.PAGEABLE;
  
          PrintRequestAttributeSet attributeSet = new HashPrintRequestAttributeSet();
  
          attributeSet.add(Sides.ONE_SIDED);
          attributeSet.add(new Copies(2));
  
          PrintService[] printServices = PrintServiceLookup
                  .lookupPrintServices(docFlavor, attributeSet);
  
          if (printServices.length == 0) {
              System.out.println("No printer found...");
          }
  
          PrintService myService = null;
  
          for (PrintService service : printServices) {
              System.out.println("Connected printer name is :: "
                      + service.getName());
              if (service.getName().equals(
                      "Hewlett-Packard-HP-LaserJet-Pro-MFP-M126nw")) {
  
                  myService = service;
  
                  break;
  
              }
          }
  
          FileInputStream inputStream = new FileInputStream(
                  "/home/roshan/Downloads/rudhiraBillDemo.pdf");
  
          Doc pdfDoc = new SimpleDoc(inputStream,
                  DocFlavor.INPUT_STREAM.AUTOSENSE, null);
  
          DocPrintJob printJob = myService.createPrintJob();
  
          printJob.print(pdfDoc, attributeSet);
  
          inputStream.close();
  
          System.out.println("PDF printed successfully..");
  
      } catch (Exception exception) {
          exception.printStackTrace();
      }
  }
  
  }
  </code></pre>