title: Thermal Printing in Java
body: <p>I have a problem in thermal printer. every time I print, there's some code/text that would be added like this.</p>
  
  <pre><code>%!PS-Adobe-3.0
  %%BoundingBox: 17 19 595 773 
  %%Pages: 1
  ..... 
  %%BegingFeature: *InputSlot Upper 
  2 dict up /PageSize [612 792] put dup /ImagingBox null put setpagedevice 
  %%EndFeature 
  .... 
  %%EndSetup 
  %%Page: 1 1 
  %%BeginPageSetup 
  %%EndPageSetup 
  %%BeginDocument: nondsc 
  Hello World 
  %%EndDocument 
  %%EOF 
  </code></pre>
  
  <p>I just wanted to print the "Hello World", how will I eliminate the unwanted text in the output? is there something in my code? </p>
  
  <p>My printer is ESC/POS 
  Model P06-P 
  My platform: Mac OS 10.8.2</p>
  
  <p>my code is </p>
  
  <pre><code>String defaultPrinter = PrintServiceLookup.lookupDefaultPrintService().getName();
          System.out.println("Default printer: " + defaultPrinter);
          PrintService service = PrintServiceLookup.lookupDefaultPrintService();
          // prints the famous hello world! plus a form feed
          InputStream is = new ByteArrayInputStream("hello world!\f".getBytes("UTF8"));
  
          PrintRequestAttributeSet  pras = new HashPrintRequestAttributeSet();
  
          pras.add(new Copies(1));
  
          DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
          Doc doc = new SimpleDoc(is, flavor, null);
          DocPrintJob job = service.createPrintJob();
  
          job.print(doc, pras);
          is.close();
  </code></pre>
  
  <p>any help would be much appreciated</p>
  
  <p>=======================================</p>
  
  <p>When I try to print it on the CLI it prints, but when I made this code in java, it didn't print. waitFor() always return 1. I am sure the file is there, and the directory it is executing. But when I only issue "ls" it will have an output.</p>
  
  <pre><code> List&lt;String&gt; cmd = new ArrayList&lt;String&gt;();
      cmd.add("lpr");
      cmd.add("-P");
      cmd.add("Prolific_Technology_Inc__IEEE_1284_Controller");
      cmd.add("-oraw");
      cmd.add("lalala.txt");
  
      ProcessBuilder pb = new ProcessBuilder(cmd);
      pb.directory(new File("src/org/cups4j/test"));
      Process p = pb.start();
      int res = p.waitFor();
      String line = null;
      try { 
          BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream())); 
          while( (line = input.readLine()) != null){
              System.out.println(line);
          }
       } catch (IOException e1) { 
           e1.printStackTrace();
           System.exit(0); 
       }    
      System.out.println("Result: " + res);
  </code></pre>