0

I read this and this but I can't print to my Brother QL-700 Label Printer. I'm always getting the following Exception:

java.lang.IllegalArgumentException: Bad margins

The Labels I would like to print on are 38mm X 30mm. Here is my Code:

Printer printer = Printer.getDefaultPrinter();
Paper label = PrintHelper.createPaper("38x30", 38, 30, Units.MM);
PageLayout pageLayout = printer.createPageLayout(label, PageOrientation.LANDSCAPE, Printer.MarginType.EQUAL);

PrinterJob job = PrinterJob.createPrinterJob();
double scaleX = pageLayout.getPrintableWidth() / node.getBoundsInParent().getWidth();
double scaleY = pageLayout.getPrintableHeight() / node.getBoundsInParent().getHeight();
node.getTransforms().add(new Scale(scaleX, scaleY));
if (job != null  && job.showPrintDialog(node.getScene().getWindow()) ) {
boolean success = job.printPage(node);
    if (success) {
        job.endJob();
    }
}

Update 1:

I get the exception here:

job.showPrintDialog(node.getScene().getWindow())

Update 2:

printer.getPrinterAttributes().getSupportedPapers()

gives me that:

[Paper: 12mm size=12.0x89.8 MM, Paper: 12mm Dia size=12.0x12.0 MM, Paper: 12mm x 2 size=21.0x89.8 MM, Paper: 12mm x 3 size=30.0x89.8 MM, Paper: 12mm x 4 size=39.0x89.8 MM, Paper: 17mm x 54mm size=17.0x53.9 MM, Paper: 17mm x 87mm size=17.0x86.9 MM, Paper: 23mm x 23mm size=23.0x23.0 MM, Paper: 24mm Dia size=24.0x24.0 MM, Paper: 29mm size=29.0x89.8 MM, Paper: 29mm x 2 size=54.8x89.8 MM, Paper: 29mm x 3 size=80.7x89.8 MM, Paper: 29mm x 42mm size=29.0x41.9 MM, Paper: 29mm x 90mm size=29.0x89.8 MM, Paper: 38mm size=38.0x89.8 MM, Paper: 38mm x 2 size=73.0x89.8 MM, Paper: 38mm x 90mm size=38.0x89.8 MM, Paper: 39mm x 48mm size=39.0x47.8 MM, Paper: 50mm size=50.0x89.8 MM, Paper: 54mm size=53.8x89.8 MM, Paper: 58mm Dia size=58.3x58.3 MM, Paper: 60mm x 86mm size=60.0x86.8 MM, Paper: 62mm size=62.0x89.8 MM, Paper: 62mm x 100mm size=62.0x99.8 MM, Paper: Absender-Etikett size=17.0x53.9 MM, Paper: Adress-Etikett (Standard) size=29.0x89.8 MM, Paper: Adress-Etikett (groß) size=38.0x89.8 MM, Paper: Benutzerdefinierte Größe size=29.0x89.8 MM, Paper: CD/DVD-Etikett size=58.3x58.3 MM, Paper: Hängeregister-Etikett size=17.0x53.9 MM, Paper: Mappe 3 cm - Rücken size=29.0x209.1 MM, Paper: Mappe 5 cm - Rücken size=62.0x209.1 MM, Paper: Namensschild size=62.0x99.8 MM, Paper: Ordnerregister-Etikett size=17.0x86.9 MM, Paper: Porto-Etikett 62mmx184mm size=62.0x183.9 MM, Paper: Porto-Etikett 62mmx209mm size=62.0x209.3 MM, Paper: Quadratisches Etikett size=23.0x23.0 MM, Paper: Rundes Etikett ø 12 mm size=12.0x12.0 MM, Paper: Rundes Etikett ø 24 mm size=24.0x24.0 MM, Paper: Versand-Etikett (Päckchen) size=62.0x99.8 MM]

Update 3

I found out that the function public PageLayout createPageLayout(Paper paper, PageOrientation orient, MarginType mType) in Printer class multiplies the margin with 72 (I think that are the dpi). Due to that I think I get the bad margins Exception. But I have no idea why. I also set all margins to zero with the same result. PageLayout pageLayout = printer.createPageLayout(tmp, PageOrientation.PORTRAIT,0f,0f,0f,0f);

I tried this and it works. but only in the given size double width = fromCMToPPI(3.5); double height = fromCMToPPI(8.8);. If I set the height to 3.3 it's not working

Update 4

I can print with this code:

DocFlavor flavor = DocFlavor.INPUT_STREAM.JPEG;
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();

PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
Media[] res = (Media[]) printService.getSupportedAttributeValues(Media.class, null, null);

aset.add(OrientationRequested.PORTRAIT);
aset.add(res[14]);

DocPrintJob pj = printService.createPrintJob();

FileInputStream fis = new FileInputStream("test.jpg");
Doc doc = new SimpleDoc(fis, flavor, null);
pj.print(doc, aset);

But now the problem is the height. I have to much space. How can I set the height of the paper? Btw it's endless paper and res[14] means 38mm endless.

Community
  • 1
  • 1
neobatman
  • 99
  • 1
  • 13
  • 1
    on which line do you get the exception? – user7291698 Dec 16 '16 at 13:17
  • `PrintHelper` is not a public API class, so you should probably avoid using it. What does `printer.getPrinterAttributes().getSupportedPapers()` give you? – James_D Dec 16 '16 at 13:18
  • Also, note you are not actually using the page layout you create. Not sure if it makes any difference, because you don't say which line throws the exception, but you should use `boolean success = job.printPage(pageLayout, node);`. – James_D Dec 16 '16 at 13:29
  • I updated the Question – neobatman Dec 16 '16 at 14:30
  • So none of the supported paper sizes match the dimensions you are giving: the 29mm x 42mm appears to be closest. Try using that actual paper from the set returned by `printer.getPrinterAttributes().getSupportedPapers()`. – James_D Dec 16 '16 at 14:46
  • I did but getting the same Exception: Used Printer: Printer Brother QL-700 Used Paper + size in px (I think): 29mm x 42mm : 119.0(h) X 82.0(w) Node size in px: 100(h) : 60(w) //tested with fixed values – neobatman Dec 16 '16 at 15:07
  • The exception says `Bad margins` but I don't see any information on the actual values of the margins you set. The code says `Printer.MarginType.EQUAL` but maybe you could try `Printer.MarginType.HARDWARE_MINIMUM`? – Rupert Dec 17 '16 at 14:34
  • The Margins of `Printer.MarginType.HAWDWARE_MINIMUM` are Bottom: 8.601891636848443, Top: 8.399055361747742, Left: 4.3199999034404755, Right: 4.240001469850537 If I set it to EQUAL every side is 8.601891636848443 – neobatman Dec 19 '16 at 09:27
  • I updated the Question. – neobatman Dec 19 '16 at 13:57
  • From the printer specs not containing any 38mm x 30mm labels I deduce that you are using endless labels, correct? Maybe [this](http://stackoverflow.com/questions/11778014/label-printing-from-c-sharp-document-size-is-too-large-for-printer) contains a hint? Is there any configuration you can change to endless labels? Actually the printer should recognize that fact from the labels you inserted but who knows. – Rupert Dec 20 '16 at 13:15
  • I updated again the question. The printer knows that it's endless labels. It shows the correct width. – neobatman Jan 17 '17 at 10:01
  • If on Windows, have you tried "Print Server Properties" and add a custom 38x30mm form? You may also consider setting this form as default. Then Java will see it. – tresf Aug 25 '18 at 14:01

0 Answers0