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.