title: how to print pdf using javax printing?
body: <p>why is the following code <strong>not printing anything</strong>? Please help. I tried changing doc flavour, print request attributes. its a simple java file that print a pdf file. if you want to see the pdf, here is a <a href="https://bitbucket.org/basithkunimal/java_printing" rel="nofollow noreferrer">link to the repo</a>. I've installed all drivers and can print the same pdf file using other programs like reader and firefox.</p>
  
  <pre><code>import javax.print.*;
  import javax.print.attribute.*;
  import javax.print.attribute.standard.*;
  import java.io.*;
  import java.util.*;
  
  class PrintPdf {
  
    static String label = "HP DeskJet 5810 series"; // change this to your printer name
  
    public static void main (String [] args) throws Exception
    {
      // DocFlavor flavor = DocFlavor.INPUT_STREAM.PDF; // not working
      // DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE; // not working
      // DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PAGEABLE; // not working
      DocFlavor flavor = new DocFlavor.INPUT_STREAM("application/octet-stream");
      PrintRequestAttributeSet attr = new HashPrintRequestAttributeSet();
      // attr.add(Sides.DUPLEX); // not working
      // attr.add(MediaSize.ISO.A4); // not working
      PrintService[] ps = PrintServiceLookup.lookupPrintServices(flavor, attr);
      if (ps.length == 0) {
        throw new IllegalStateException("No Printer found");
      }
      System.out.println("Available printers: " + Arrays.asList(ps));
  
      PrintService myService = null;
      for (PrintService printService : ps) {
        if (printService.getName().equals(label)) {
            myService = printService;
          break;
        }
      }
  
      if (myService == null) {
          throw new IllegalStateException("Printer not found");
      }
  
      FileInputStream fis = new FileInputStream("./example.pdf");
      Doc pdfDoc = new SimpleDoc(fis, flavor, null);
      DocPrintJob printJob = myService.createPrintJob();
      printJob.print(pdfDoc, new HashPrintRequestAttributeSet());
      fis.close();
    }
  }
  </code></pre>
  
  <p>here is what happens, after a few seconds of waiting the <code>Java Printing</code> job disappears from the queue without printing anything</p>
  
  <p><a href="https://i.stack.imgur.com/1g3t2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1g3t2.png" alt="enter image description here"></a></p>