title: How can I display a PDF file in a WebView (JavaFX) panel using PDF.js?
body: <p>I got this (<a href="https://stackoverflow.com/a/42040344/3789572">https://stackoverflow.com/a/42040344/3789572</a>) solution, which code is below, but it isn't working. When I press the button, nothing is shown.
  You can change the file paths and try it for yourself.</p>
  
  <p>Can you help me?</p>
  
  <p>Controller code:</p>
  
  <pre><code>package sample.principal;
  
  import javafx.concurrent.Worker;
  import javafx.fxml.FXML;
  import javafx.fxml.Initializable;
  import javafx.scene.control.Button;
  import javafx.scene.web.WebEngine;
  import javafx.scene.web.WebView;
  import netscape.javascript.JSObject;
  
  import java.io.File;  
  import java.net.URL;
  import java.util.Base64;
  import java.util.ResourceBundle;
  
  import org.apache.commons.io.FileUtils;
  
  public class WebController implements Initializable {
  
  @FXML
  private WebView web;
  
  @FXML
  private Button btn;
  
  public void initialize(URL location, ResourceBundle resources) {
      WebEngine engine = web.getEngine();
      String url = getClass().getResource("..\\resources\\web\\viewer.html").toExternalForm();
  
      // connect CSS styles to customize pdf.js appearance
      engine.setUserStyleSheetLocation(getClass().getResource("..\\resources\\web\\viewer.css").toExternalForm());
  
      engine.setJavaScriptEnabled(true);
      engine.load(url);
  
      engine.getLoadWorker().stateProperty().addListener((observable, oldValue, newValue) -&gt; {
                  // to debug JS code by showing console.log() calls in IDE console
                  JSObject window = (JSObject) engine.executeScript("window");
                  window.setMember("java", new JSLogListener());
                  engine.executeScript("console.log = function(message){ java.log(message); };");
  
                  // this pdf file will be opened on application startup
                  if (newValue == Worker.State.SUCCEEDED) {
                      try {
                          // readFileToByteArray() comes from commons-io library
                          byte[] data = FileUtils.readFileToByteArray(new File("C:\\Users\\Felipe\\Documents\\" +
                                  "Programação\\Java\\" +
                                  "IdeaProjects\\PDFviewerStackOverFlow\\src\\sample\\principal\\teste.pdf"));
                          String base64 = Base64.getEncoder().encodeToString(data);
                          // call JS function from Java code
                          engine.executeScript("openFileFromBase64('" + base64 + "')");
                      } catch (Exception e) {
                          e.printStackTrace();
                      }
                  }
              });
  
      // this file will be opened on button click
      btn.setOnAction(actionEvent -&gt; {
          try {
              byte[] data = FileUtils.readFileToByteArray(new File("teste.pdf"));
              String base64 = Base64.getEncoder().encodeToString(data);
              engine.executeScript("openFileFromBase64('" + base64 + "')");
          } catch (Exception e) {
              e.printStackTrace();
          }
      });
  }
  }
  </code></pre>
  
  <p>The Main code:</p>
  
  <pre><code>package sample.principal;
  
  import javafx.application.Application;
  import javafx.fxml.FXMLLoader;
  import javafx.scene.Parent;
  import javafx.scene.Scene;
  import javafx.stage.Stage;
  
  public class Main extends Application {
  
  public static void main(String[] args) {
      Application.launch();
  }
  
  public void start(Stage primaryStage) throws Exception {
      Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
      primaryStage.setTitle("PDF test app");
      primaryStage.setScene(new Scene(root, 1280, 576));
      primaryStage.show();
  }
  }
  </code></pre>
  
  <p>and the other one:</p>
  
  <pre><code>package sample.principal;
  
  public class JSLogListener {
  public void log(String text) {
      System.out.println(text);
  }
  }
  </code></pre>
  
  <p>I would be very grateful for your help.</p>
  
  <p>Thanks.</p>