title: Java Applet PDF printing
body: <p>I am trying to build a java applet which prints a PDF file and sends it to a label printer rather than the default. I explored desktop.print but couldn't work out how to specify the printer.</p>
  
  <p>This is the code I have, i've tried to look for solutions but have ended stuck. I have signed the applet and the error it gives me it just says application error 0</p>
  
  <pre><code>import java.io.*;
  import java.net.*;
  import javax.swing.*;
  import java.awt.print.*;
  import javax.print.*;  
  import javax.print.attribute.*;   
  import javax.print.attribute.standard.*; 
  
  
  public class printPDF extends JApplet {
  
      public void init(){ 
              String uri = System.getProperty("user.home") + "\\jobbase\\print.pdf"; 
  
              DocFlavor flavor = DocFlavor.INPUT_STREAM.PDF;
  
  
              PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
              aset.add(new PrinterName("label", null));
              aset.add(new Copies(1));
  
  
              PrintService[] pservices =
                      PrintServiceLookup.lookupPrintServices(flavor, aset);
  
              if (pservices.length &gt; 0) {
  
                      DocPrintJob printJob = pservices[0].createPrintJob();
  
                      try{
                          FileInputStream fis = new FileInputStream(uri);
                          Doc doc = new SimpleDoc(fis, flavor, null);
  
  
                          try {
                              printJob.print(doc, aset);
                          } catch (PrintException e) { 
                              System.err.println(e);
                          }
  
                          } catch(IOException ioe){
                              ioe.printStackTrace(System.out);
                      }
              } else {
                      System.err.println("No suitable printers");
              }
      }
  }
  </code></pre>