title: Printing on a thermal printer Java
body: <p>I'm using the following code to print some text on a thermal printer with 80mm roll paper:</p>
  
  <pre><code>public class printnow {
  
      public static void printCard(final String bill) {
          final PrinterJob job = PrinterJob.getPrinterJob();
  
          Printable contentToPrint = new Printable() {
              @Override
              public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws  PrinterException {
                  Graphics2D g2d = (Graphics2D) graphics;
                  g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
                  g2d.setFont(new Font("Monospaced", Font.BOLD, 7));
                  pageFormat.setOrientation(PageFormat.PORTRAIT);
  
                  Paper pPaper = pageFormat.getPaper();
                  pPaper.setImageableArea(0, 0, pPaper.getWidth() , pPaper.getHeight() -2);
                  pageFormat.setPaper(pPaper);
  
                  if (pageIndex &gt; 0) 
                      return NO_SUCH_PAGE; //Only one page
  
                  String Bill [] = bill.split(";");
                  int y = 0;
                  for (int i = 0; i &lt; Bill.length; i++) {
                      g2d.drawString(Bill[i], 0, y);
                      y = y + 15;
                  }
  
                  return PAGE_EXISTS;
              }
          };  
  
          boolean don = job.printDialog();
  
          job.setPrintable(contentToPrint);
  
          try {
              job.print();
          } catch (PrinterException e) {
              System.err.println(e.getMessage());
          }
      }
  }
  </code></pre>
  
  <p>This is printing extremely fine and is exactly what I want. But when I remove the following line to disable the print dialog box and automate the process of printing, my print messes up and the printer automatically takes some margin in the left.</p>
  
  <pre><code>boolean don = job.printDialog();
  </code></pre>
  
  <p>Any idea on why this is happening and how can it be solved?</p>