John Smith
John Smith

Reputation: 21

How to print Pane in JavaFX?

How can I print my Pane that has Label inside of it? I want to print it using my POS Printer 55mm. I have this kind of code to print, but it prints nothing to me:

void print2(Node node){
    PrinterJob job = PrinterJob.createPrinterJob();
    Printer printer = Printer.getDefaultPrinter().getDefaultPrinter();
    PageLayout pageLayout = printer.createPageLayout(Paper.A6, PageOrientation.PORTRAIT, Printer.MarginType.DEFAULT);
    JobSettings jobSettings = job.getJobSettings();
    jobSettings.setPageLayout(pageLayout);
    boolean printed = job.printPage(node);
    if (printed) {
        job.endJob();
    }
}

Upvotes: 0

Views: 1933

Answers (1)

Robert Rohm
Robert Rohm

Reputation: 341

The code does not look bad. There are a few points I would recommend to check out:

  • I ran into problems with page margins. If I need absolute control over the whole printing area, I set the printer margins all to 0 and define the page layout exactly to the printer format.
  • I would try do define a page layout/paper size that suites the POS 55m printer. Maybe Paper.A6 gives you trouble because it is "oversized". I experienced this not when working with JavaFX and printing, but with Windows GDI printing in Delphi. Since the driver layer is the same, it might be a similar problem.
  • It was a good starting point to define layout node size keeping printer points, i.e., 72ppi, in mind. Simply think pixel size as printer point size.

In fact, I had good experiences with a slightly more simple code like this (sorry, it is DIN A5 paper size example, but worked):

PrinterJob printerJob = PrinterJob.createPrinterJob();
if (printerJob != null) {
  PageLayout pageLayout = printerJob.getPrinter().createPageLayout(Paper.A5, PageOrientation.LANDSCAPE, 0, 0, 0, 0);

  boolean success = printerJob.printPage(pageLayout, root);
  if (success) {
    printerJob.endJob();
  }
}

Hope that helps!

Upvotes: 1

Related Questions