title: Java auto length printing on Brother QL580n label printer (Windows only)
body: <p>I am desperately trying to print a random length label on my Brother QL580n from Java on Windows.</p>
  
  <p>I've found a way to do it with CUPS but it is not working when executing it from Windows with a Windows printer.</p>
  
  <p>Here is a sample of code using Apache PDFBox:</p>
  
  <pre><code> PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
  
    PrintService service = null;
    for (PrintService printer : printServices) {
        if (printer.getName().equals("Brother QL-580N")) {
          service = printer;
        }
    }
    PDDocument document = PDDocument.load(new File("C:\\label.pdf"));
  
    Paper paperP = new Paper();
  
    double width = document.getPage(0).getMediaBox().getWidth();
    double height = document.getPage(0).getMediaBox().getHeight();
    paperP.setSize(width, height);
    paperP.setImageableArea(0, 0, paperP.getWidth(), paperP.getHeight()); // no margins
  
    // custom page format
    PageFormat pageFormat = new PageFormat();
    pageFormat.setPaper(paperP);
    PrinterJob jobP = PrinterJob.getPrinterJob();
    jobP.setPrintService(service);
  
    Book book = new Book();
    book.append(new PDFPrintable(document), pageFormat, document.getNumberOfPages());
  
    jobP.setPageable(book);
  
    jobP.print();
  </code></pre>
  
  <p>The real length of my pdf label is 40mm but only 29mm are printed which is one the windows default label lengths (even if I change the windows printer preferences to something else than 62 mm x 29 mm)</p>
  
  <p>By the way, I am not able to do it with Adobe Reader too, but I am able to print a custom length label with P-Touch Editor from Brother</p>
  
  <p>EDIT</p>
  
  <p>I've just tried to look at <code>jobP.getPageFormat( new HashPrintRequestAttributeSet()).getWidth()</code> and <code>jobP.getPageFormat( new HashPrintRequestAttributeSet()).getHeight()</code> and I am getting 62mm and 29 mm so it seems my try to pass a custom PageFormat is not working</p>