0

I am having trouble with printing labels on a Zebra label printer using JavaFX.

If I request zero margins, or request Printer.MarginType.HARDWARE_MINIMUM, I would expect the margins to be set to zero (or close to it). From this question, it seems that when setting the margins, the printer hardware is still taken into account:

If the printer cannot support the layout as specified, it will adjust the returned layout to a supported configuration

However, in this case, it is a Zebra Label printer, and the driver has set zero margins, so the supported configuration should allow zero margins, but it doesn't seem to.

The text output from the printInfo function in the code below shows the following for the print job before and after modification:

---------------------------------------------
Resolution = 203
ScaleX = 1.0
TranslateX = 0.0
Printer: Printer ZDesigner ZD420-203dpi ZPL
PageLayout: Paper=Paper: Letter size=8.5x11.0 INCH Orient=PORTRAIT leftMargin=54.0 rightMargin=54.0 topMargin=54.0 bottomMargin=54.0
LayoutX = 0.0points  0.0mm
Layout Bounds MinX = 0.0points  0.0mm
BoundsInLocal MinX = 0.0points  0.0mm
BoundsInParent MinX = 0.0points  0.0mm
printable Width = 504.0points  177.8000000112mm
printable Height = 684.0points  241.30000001520003mm
---------------------------------------------
Resolution = 203
ScaleX = 0.28346438836889
TranslateX = -189.13754272460938
Printer: Printer ZDesigner ZD420-203dpi ZPL
PageLayout: Paper=Paper: Custom size=50.0x32.0 MM Orient=PORTRAIT leftMargin=17.75 rightMargin=17.749999999999993 topMargin=11.375 bottomMargin=11.375000000000004
LayoutX = 0.0points  0.0mm
Layout Bounds MinX = 0.0points  0.0mm
BoundsInLocal MinX = 0.0points  0.0mm
BoundsInParent MinX = 2.7526337476047047E-7points  9.710680165772739E-8mm
printable Width = 106.5points  37.5708333357mm
printable Height = 68.25points  24.077083334850002mm

Interestingly the paper selection when the print job starts is Letter size. The print driver nowhere has a Letter size specified, so I am not sure where it is getting that info from, and why it doesn't get 50mm x 32mm from the driver.

After setting the printer margins to zero, they are reported back at 17.75 (points I am guessing) left and right, and the printout coincides with that... I have a much smaller print area to print things in, anything over gets clipped.

The printer settings look like the following: enter image description here

Printing from other applications, e.g. LibreOffice, I can print right to the edge of the label... but not in Java... Started off trying in Linux, but the printer drivers might not be the most up-to-date, so switched to Windows, but still having troubles.

Code:

PrinterJob job = PrinterJob.createPrinterJob();
if (job != null) {
    if (job.showPrintDialog(window)) {
        printInfo(job, node);

        // Have tried not setting anything...
        val paper = PrintHelper.createPaper("Custom", 50, 32, Units.MM);
        val orientation = PageOrientation.PORTRAIT; // Have tried Landscape
        // val pageLayout = job.getPrinter().createPageLayout(paper, orientation, Printer.MarginType.HARDWARE_MINIMUM);
        val pageLayout = job.getPrinter().createPageLayout(paper, orientation, 0, 0, 0, 0);

        job.getJobSettings().setPageLayout(pageLayout);

        // Units seem to be in points, so scale for 0.1mm
        node.setScaleX(0.28346438836889);
        node.setScaleY(0.28346438836889);

        node.setTranslateX(-node.getBoundsInParent().getMinX());
        node.setTranslateY(-node.getBoundsInParent().getMinY());

        printInfo(job, node);

        boolean printed = job.printPage(node);
        if (printed) {
            job.endJob();
        } else {
            System.out.println("Printing failed");
        }
    }
} else {
    System.out.println("Could not create printer job");
}

The print info function just outputs some info:

void printInfo(PrinterJob job, Node node) {
    System.out.println("---------------------------------------------");
    val resolution = job.getJobSettings().getPrintResolution().getFeedResolution();
    System.out.println("Resolution = " + resolution);
    System.out.println("ScaleX = " + node.getScaleX());
    System.out.println("TranslateX = " + node.getTranslateX());

    System.out.println("Printer: " + job.getPrinter());
    System.out.println("PageLayout: " + job.getJobSettings().getPageLayout().toString());

    System.out.println("LayoutX = " + pointsAndMm(node.getLayoutX()));
    System.out.println("Layout Bounds MinX = " + pointsAndMm(node.getLayoutBounds().getMinX()));
    System.out.println("BoundsInLocal MinX = " + pointsAndMm(node.getBoundsInLocal().getMinX()));
    System.out.println("BoundsInParent MinX = " + pointsAndMm(node.getBoundsInParent().getMinX()));
    System.out.println("printable Width = " + pointsAndMm(job.getJobSettings().getPageLayout().getPrintableWidth()));
    System.out.println("printable Height = " + pointsAndMm(job.getJobSettings().getPageLayout().getPrintableHeight()));
}

Have come across a few question/answers in my googling, but they either are for AWT or don't solve the problem.

cfnz
  • 174
  • 2
  • 14

0 Answers0