I'm trying to print a HTML page within a JavaFX WebView (JavaFX 8_25) on A4 paper in Landscape orientation but it prints out with Portrait orientation with a tiny font size
printer.createPageLayout(Paper.A4, PageOrientation.LANDSCAPE, Printer.MarginType.DEFAULT);
PrinterJob job = PrinterJob.createPrinterJob(printer);
if (job != null) {
System.out.println(job.getJobSettings().getPageLayout());
webEngine.print(job);
job.endJob();
}
The System.out shows the portrait orientation
Paper=Paper: A4 (210 x 297mm) size=594.90087890625x841.3598022460938 MM Orient=PORTRAIT leftMargin=54.0 rightMargin=54.0 topMargin=54.0 bottomMargin=54.0
I'm finding the only way I can print the HTML page in Landscape mode is by calling the print job's showPageSetupDialog method just prior to printing.
printer.createPageLayout(Paper.A4, PageOrientation.LANDSCAPE, Printer.MarginType.DEFAULT);
PrinterJob job = PrinterJob.createPrinterJob(printer);
if (job != null) {
if(job.showPageSetupDialog(null)) {
System.out.println(job.getJobSettings().getPageLayout());
webEngine.print(job);
job.endJob();
}
}
If I select Landscape in the Page Setup dialog, the System.out shows
Paper=Paper: A4 size=210.0x297.0 MM Orient=LANDSCAPE leftMargin=54.0 rightMargin=53.11810302734375 topMargin=51.995269775390625 bottomMargin=54.0
I have a problem with the dialog in that I have to set A4 and Landscape every time.
I have 3 questions: -
Why is the webEngine.print not using the PageLayout I have passed in to the printer?
Is it possible to get Landscape orientation on a webEngine.print(job); without having to set it via the Page Setup Dialog?
If I have to use the Page Setup Dialog, is there any way JavaFX can remember what I set for the page setup for the next print job i.e. A4 and Landscape?
While typing this I have also tried
print{@page {size: landscape}}
within a style tag on HTML page but that didn't work either.
Thanks
You need to set the PageLayout
to the JobSettings
of your PrinterJob
, using the method job.getJobSettings().setPageLayout(PageLayout)
Try this code:
PageLayout pageLayout = printer.createPageLayout(Paper.A4,
PageOrientation.LANDSCAPE, Printer.MarginType.DEFAULT);
PrinterJob job = PrinterJob.createPrinterJob(printer);
job.getJobSettings().setPageLayout(pageLayout);
if (job != null) {
System.out.println(job.getJobSettings().getPageLayout());
webEngine.print(job);
job.endJob();
}