JavaFX Scaled Printing Messed up My Printer

943 Views Asked by At

I was using a very simple piece of code to print a WebView:

public void print(final Node node) {

  PrinterJob job = PrinterJob.createPrinterJob();

  if (job != null) {
    if(job.showPrintDialog(node.getScene().getWindow())){
      _quoteView.getEngine().print(job);
      job.endJob();
    }
  }
}

It did not work well, but it did work, printing out the webview on about two pages of paper, but looking like the WebView.

Then, I tried adapting the following code snippet that I got from: http://java.dzone.com/articles/introduction-example-javafx-8

Printer printer = Printer.getDefaultPrinter();
PageLayout pageLayout = printer.createPageLayout(Paper.NA_LETTER,PageOrientation.PORTRAIT, Printer.MarginType.DEFAULT);
double scaleX = pageLayout.getPrintableWidth() / node.getBoundsInParent().getWidth();
double scaleY = pageLayout.getPrintableHeight() / node.getBoundsInParent().getHeight();
node.getTransforms().add(new Scale(scaleX, scaleY));

This did not work and it messed up the printing very badly. Ok, no foul, I just put the code back to the way it was.

The Problem

However, now, whenever I run the app with the restored code, the scaling seems to be permanently changed. Does not matter which printer I use, or even if I simply print to PDF. I have restarted my computer (recent Mac with up to date OS). I have restarted my printer. Same result.

I have tried printing from other apps, and so far, they all appear to print just fine.

I am guessing that the code snippet above somehow changed the scaling and associated it with just this app somehow in a cache.

Questions

  1. Does anyone know what is going on?
  2. If it is cached values
    1. Where are they?
    2. How do I get rid of them?

Also, not sure if this is related to the problem, but I started getting this in the console running the java app doing the printing:

Outstanding resource locks detected:
J2D Texture Pool: 1,105,920 used (0.4%), 1,105,920 managed (0.4%), 268,435,456 total
ES2 Vram Pool: 39,665,348 used (14.8%), 39,665,348 managed (14.8%), 268,435,456 total
47 total resources being managed
average resource age is 7.0 frames
0 resources at maximum supported age (0.0%)
5 resources marked permanent (10.6%)
3 resources have had mismatched locks (6.4%)
3 resources locked (6.4%)
23 resources contain interesting data (48.9%)
0 resources disappeared (0.0%)

I have no idea what this is referring to, but it definitely comes up in response to printing.

Thank you in advance for any help you can provide.

.... quite a bit after the initial posting of this question.

I inspected the created job:

I don't have enough reputation points to post the image of the stack trace. So here is a link to it.

It becomes clear that the printer associated with the job has a defPageLayout whose Paper is set to "Paper:Japanese Postcard size=100.0x148.0..."

This is clearly what is messing up the printing. However, I can't seem to reset it.

I used job.showPageSetupDialog(node.getScene().getWindow()) to bring up the page layout panel. Then I was able to see the Paper set to "Postcard", at which point I changed it to Standard US Letter and then selected the top pull down to Save as Default menu item. This however, did not fix the problem and each time I run it, the default is still Postcard.

I did successfully prove that the problem can be corrected, if I first create a new page layout:

Printer printer = Printer.getDefaultPrinter();
PageLayout pageLayout = printer.createPageLayout(Paper.NA_LETTER, PageOrientation.PORTRAIT, Printer.MarginType.DEFAULT);

Then, if I set the new page layout on the job settings:

JobSettings jobSettings = job.getJobSettings();
jobSettings.setPageLayout(pageLayout);

Then the WebView prints correctly. But I can't seem to reset the default paper size for this app. The default paper size for all other apps seems to be fine.

Where is this thing cached?

1

There are 1 best solutions below

0
On

Solution

On a Mac, go to settings, delete the printer and re-add it to the list of printers. Admittedly, only after re-adding the printer did I notice the pull-down labeled "Default paper size" at the bottom of the Printers & Scanners System preferences pane. I suspect I could have fixed it there. :(