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

pdf.js doesn't work inside WebView component

    Details

    • Subcomponent:
      web
    • CPU:
      x86_64
    • OS:
      windows_7

      Description

      yeFULL PRODUCT VERSION :
      java version "1.8.0_66"
      Java(TM) SE Runtime Environment (build 1.8.0_66-b18)
      Java HotSpot(TM) 64-Bit Server VM (build 25.66-b18, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      MS windows7 6.1 Build 7601 SP1

      A DESCRIPTION OF THE PROBLEM :
      WebKit browser isn't able to launch pdf.js document viewer any more after upgrage from jdk 1.8 51 -> 66. No easy way to find out the reason. The only js debugger which may work with WebView is firebug lite, but it has no breakpoint/step in/out support. Console log doen't show any crashes. PDF document arrives to the js virtual machine, but rendeing fails.

      REGRESSION. Last worked in version 8u51

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Install netbeans with JavaFX support
      2. install jdk 1.8 u66
      3. open sample https://docs.oracle.com/javase/8/javafx/sample-apps/WebViewSample.zip in netbeans
      4. replace url in the source of the sample at line 228 from oracle website to https://mozilla.github.io/pdf.js/web/viewer.html
      5. launch the application

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      viewer should show pdf
      ACTUAL -
      viewer doesn't show pdf file

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      No crashes available. js console doesn't have any messages.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      /*
       * Copyright (c) 2011, 2014, Oracle and/or its affiliates.
       * All rights reserved. Use is subject to license terms.
       *
       * This file is available and licensed under the following license:
       *
       * Redistribution and use in source and binary forms, with or without
       * modification, are permitted provided that the following conditions
       * are met:
       *
       * - Redistributions of source code must retain the above copyright
       * notice, this list of conditions and the following disclaimer.
       * - Redistributions in binary form must reproduce the above copyright
       * notice, this list of conditions and the following disclaimer in
       * the documentation and/or other materials provided with the distribution.
       * - Neither the name of Oracle nor the names of its
       * contributors may be used to endorse or promote products derived
       * from this software without specific prior written permission.
       *
       * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
       * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
       * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
       * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
       * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
       * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
       * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
       * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
       * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       */

      package webviewsample;

      import javafx.application.Application;
      import javafx.application.Platform;
      import javafx.beans.value.ObservableValue;
      import javafx.collections.ListChangeListener.Change;
      import javafx.concurrent.Worker.State;
      import javafx.event.ActionEvent;
      import javafx.event.Event;
      import javafx.geometry.HPos;
      import javafx.geometry.Pos;
      import javafx.geometry.VPos;
      import javafx.print.PrinterJob;
      import javafx.scene.Node;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.control.ComboBox;
      import javafx.scene.control.ContextMenu;
      import javafx.scene.control.Hyperlink;
      import javafx.scene.control.MenuItem;
      import javafx.scene.image.Image;
      import javafx.scene.image.ImageView;
      import javafx.scene.input.MouseButton;
      import javafx.scene.input.MouseEvent;
      import javafx.scene.layout.HBox;
      import javafx.scene.layout.Priority;
      import javafx.scene.layout.Region;
      import javafx.scene.paint.Color;
      import javafx.scene.web.PopupFeatures;
      import javafx.scene.web.WebEngine;
      import javafx.scene.web.WebHistory;
      import javafx.scene.web.WebHistory.Entry;
      import javafx.scene.web.WebView;
      import javafx.stage.Stage;
      import netscape.javascript.JSObject;

      public class WebViewSample extends Application {

          private Scene scene;

          @Override
          public void start(Stage stage) {
              // create scene
              stage.setTitle("Web View Sample");
              scene = new Scene(new Browser(stage), 900, 600, Color.web("#666970"));
              stage.setScene(scene);
              // apply CSS style
              scene.getStylesheets().add("webviewsample/BrowserToolbar.css");
              // show stage
              stage.show();
          }

          public static void main(String[] args) {
              launch(args);
          }
      }

      class Browser extends Region {

          private final HBox toolBar;
          final private static String[] imageFiles = new String[]{
              "product.png",
              "blog.png",
              "documentation.png",
              "partners.png",
              "help.png"
          };
          final private static String[] captions = new String[]{
              "Products",
              "Blogs",
              "Documentation",
              "Partners",
              "Help"
          };
          final private static String[] urls = new String[]{
              "http://www.oracle.com/products/index.html",
              "http://blogs.oracle.com/",
              "http://docs.oracle.com/javase/index.html",
              "http://www.oracle.com/partners/index.html",
              WebViewSample.class.getResource("help.html").toExternalForm()
          };
          final ImageView selectedImage = new ImageView();
          final Hyperlink[] hpls = new Hyperlink[captions.length];
          final Image[] images = new Image[imageFiles.length];
          final WebView browser = new WebView();
          final WebEngine webEngine = browser.getEngine();
          final Button toggleHelpTopics = new Button("Toggle Help Topics");
          final WebView smallView = new WebView();
          final ComboBox comboBox = new ComboBox();
          private boolean needDocumentationButton = false;
          
          
          public Browser(final Stage stage) {
              //apply the styles
              getStyleClass().add("browser");
                      
              for (int i = 0; i < captions.length; i++) {
                  // create hyperlinks
                  Hyperlink hpl = hpls[i] = new Hyperlink(captions[i]);
                  Image image = images[i]
                          = new Image(getClass().getResourceAsStream(imageFiles[i]));
                  hpl.setGraphic(new ImageView(image));
                  final String url = urls[i];
                  final boolean addButton = (hpl.getText().equals("Help"));
                  
                  // process event
                  hpl.setOnAction((ActionEvent e) -> {
                      needDocumentationButton = addButton;
                      webEngine.load(url);
                  });
                          
              }


              comboBox.setPrefWidth(60);

              // create the toolbar
              toolBar = new HBox();
              toolBar.setAlignment(Pos.CENTER);
              toolBar.getStyleClass().add("browser-toolbar");
              toolBar.getChildren().add(comboBox);
              toolBar.getChildren().addAll(hpls);
              toolBar.getChildren().add(createSpacer());

              //set action for the button
              toggleHelpTopics.setOnAction((ActionEvent t) -> {
                  webEngine.executeScript("toggle_visibility('help_topics')");
              });

              smallView.setPrefSize(120, 80);

              //handle popup windows
              webEngine.setCreatePopupHandler(
                      (PopupFeatures config) -> {
                          smallView.setFontScale(0.8);
                          if (!toolBar.getChildren().contains(smallView)) {
                              toolBar.getChildren().add(smallView);
                          }
                          return smallView.getEngine();
              });

              //process history
              final WebHistory history = webEngine.getHistory();
              history.getEntries().addListener(
                  (Change<? extends Entry> c) -> {
                      c.next();
                      c.getRemoved().stream().forEach((e) -> {
                      comboBox.getItems().remove(e.getUrl());
                  });
                      c.getAddedSubList().stream().forEach((e) -> {
                      comboBox.getItems().add(e.getUrl());
                  });
              });

              //set the behavior for the history combobox
              comboBox.setOnAction((Event ev) -> {
                  int offset
                          = comboBox.getSelectionModel().getSelectedIndex()
                          - history.getCurrentIndex();
                  history.go(offset);
              });

              // process page loading
              webEngine.getLoadWorker().stateProperty().addListener(
                  (ObservableValue<? extends State> ov, State oldState,
                      State newState) -> {
                          toolBar.getChildren().remove(toggleHelpTopics);
                          if (newState == State.SUCCEEDED) {
                              JSObject win
                                      = (JSObject) webEngine.executeScript("window");
                              win.setMember("app", new JavaApp());
                              if (needDocumentationButton) {
                                  toolBar.getChildren().add(toggleHelpTopics);
                              }
                          }
              });
              //adding context menu
              final ContextMenu cm = new ContextMenu();
              MenuItem cmItem1 = new MenuItem("Print");
              cm.getItems().add(cmItem1);
              toolBar.addEventHandler(MouseEvent.MOUSE_CLICKED, (MouseEvent e) -> {
                  if (e.getButton() == MouseButton.SECONDARY) {
                      cm.show(toolBar, e.getScreenX(), e.getScreenY());
                  }
              });

              //processing print job
              cmItem1.setOnAction((ActionEvent e) -> {
                  PrinterJob job = PrinterJob.createPrinterJob();
                  if (job != null) {
                      webEngine.print(job);
                      job.endJob();
                  }
              });

              // load the home page
              webEngine.load("https://mozilla.github.io/pdf.js/web/viewer.html");

              //add components
              getChildren().add(toolBar);
              getChildren().add(browser);
          }

          // JavaScript interface object
          public class JavaApp {

              public void exit() {
                  Platform.exit();
              }
          }

          private Node createSpacer() {
              Region spacer = new Region();
              HBox.setHgrow(spacer, Priority.ALWAYS);
              return spacer;
          }

          @Override
          protected void layoutChildren() {
              double w = getWidth();
              double h = getHeight();
              double tbHeight = toolBar.prefHeight(w);
              layoutInArea(browser,0,0,w,h-tbHeight,0,HPos.CENTER,VPos.CENTER);
              layoutInArea(toolBar,0,h-tbHeight,w,tbHeight,0,HPos.CENTER,VPos.CENTER);
          }

          @Override
          protected double computePrefWidth(double height) {
              return 900;
          }

          @Override
          protected double computePrefHeight(double width) {
              return 600;
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Rollback to jdk 1.8 51u

        Issue Links

          Activity

          Hide
          mbilla Murali Billa added a comment -
          I Tested with 8u40, 66, 72, 9 and issue is not reproducible(able to see pdf in viewer).

          Hence closing this bug as not reproducible.
          Show
          mbilla Murali Billa added a comment - I Tested with 8u40, 66, 72, 9 and issue is not reproducible(able to see pdf in viewer). Hence closing this bug as not reproducible.
          Hide
          pmangal Priyanka Mangal added a comment -
          Issue reproducible in jdk-9+170 and 8u131 . Reproducer available in JDK-8180825.
          Show
          pmangal Priyanka Mangal added a comment - Issue reproducible in jdk-9+170 and 8u131 . Reproducer available in JDK-8180825 .

            People

            • Assignee:
              mbilla Murali Billa
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: