Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8235831

First printed page blank after resizing WebView


    • Type: Bug
    • Status: Open
    • Priority: P4
    • Resolution: Unresolved
    • Affects Version/s: 8, openjfx11
    • Fix Version/s: tbd
    • Component/s: javafx
    • Labels:
    • Subcomponent:


      JavaFX allows to print any Node through the Print API, even when the application is running in headless mode.

      However, in headless mode if the `PrinterJob#printPage` method is called on a WebView after it has been resized, the job doesn't print anything. This only happens for the first page. Subsequent prints after the first print are correct.

      The issue only happens in headless mode for the first print after the WebView is resized.

      Can be reproduced on MacOS Catalina, Ubuntu 18.04 and Windows 10 Using Oracle Java 8 (64-bit) and AdoptOpenJDK 11

      Can always be reproduced

      1 Set the default printer to a "document printer"
      2 Run the attached application
      3 The application tries to print 4 pages and shows a dialog to save each one of them
      4 First and fourth print-jobs prints the content of the WebView. However, second and third doesn't

      Print job should always print the content of the WebView
      ACTUAL -
      The first print job after WebView resize doesn't print anything

      ---------- BEGIN SOURCE ----------
      import javafx.animation.PauseTransition;
      import javafx.application.Application;
      import javafx.application.Platform;
      import javafx.beans.value.ChangeListener;
      import javafx.concurrent.Worker;
      import javafx.geometry.Dimension2D;
      import javafx.print.Printer;
      import javafx.print.PrinterJob;
      import javafx.scene.Scene;
      import javafx.scene.web.WebView;
      import javafx.stage.Stage;
      import javafx.util.Duration;

      import java.util.concurrent.CountDownLatch;

      public class WebViewPrintBug extends Application {

          private static final String HTML_TEMPLATE = "<html><body>%s</body></html>";

          private static final CountDownLatch startup = new CountDownLatch(1);

          private static WebView webView;
          private static Stage stage;
          private static Dimension2D dimensions;

          private static PauseTransition transition;
          private static CountDownLatch printed;

          public static void main(String[] args) throws Exception {
              // start JFX
              new Thread(() -> Application.launch(WebViewPrintBug.class)).start();

              //try some prints
              Printer printer = Printer.getDefaultPrinter();

              //prints fine
              printed = new CountDownLatch(1);
              dimensions = new Dimension2D(720, 960);
              print(printer, String.format(HTML_TEMPLATE, "<h2>Test Page 1</h2><div>Content goes here</div>"));

              //prints blank
              printed = new CountDownLatch(1);
              dimensions = new Dimension2D(640, 960);
              print(printer, String.format(HTML_TEMPLATE, "<h2>Test Page 2</h2><div>Can you see me?</div>"));

              //also prints blank
              printed = new CountDownLatch(1);
              dimensions = new Dimension2D(640, 800);
              print(printer, String.format(HTML_TEMPLATE, "<h2>Test Page 3</h2><div>Spooky ghost image</div>"));

              //same dimensions > prints fine
              printed = new CountDownLatch(1);
              print(printer, String.format(HTML_TEMPLATE, "<h2>Test Page 4</h2><div>Visible content</div>"));


          /** Called by jfx thread on startup */
          public void start(Stage st) {
              webView = new WebView();
              st.setScene(new Scene(webView));
              stage = st;

              //setup load listeners
              Worker<Void> worker = webView.getEngine().getLoadWorker();
              worker.exceptionProperty().addListener((obs, oldEx, newEx) -> {
                  if (newEx != null) { newEx.printStackTrace(); }

              //prevents JavaFX from shutting down when hiding window

              System.out.println("Started JFX");

          /** Called by jfx thread on content load */
          private static ChangeListener<Worker.State> stateListener = (ov, oldState, newState) -> {
              if (newState == Worker.State.SUCCEEDED) {
                  //resize the view
                  webView.setMinSize(dimensions.getWidth(), dimensions.getHeight());
                  webView.setPrefSize(dimensions.getWidth(), dimensions.getHeight());
                  webView.setMaxSize(dimensions.getWidth(), dimensions.getHeight());

                  //hand off to print lambda

                  //showing stage at this point prevents blank pages, but breaks on headless environments

          /** Our call to load pages and finalize printing */
          private static void print(Printer printer, String content) {
              //note - extending duration doesn't seem to have an effect on blank pages
              transition = new PauseTransition(Duration.millis(100));
              transition.setOnFinished(evt -> {
                  PrinterJob job = PrinterJob.createPrinterJob(printer);

                  Platform.runLater(() -> {

                      System.out.println("Starting print");
                      System.out.println((String) webView.getEngine().executeScript("document.documentElement.outerHTML"));


              //reset webview and start loading
              Platform.runLater(() -> {
                  webView.getEngine().loadContent(content, "text/html");
      ---------- END SOURCE ----------




            • Assignee:
              bchoudhary Bhawesh Choudhary
              jvos Johan Vos
            • Votes:
              0 Vote for this issue
              4 Start watching this issue


              • Created: