Ok, here is my 50 cents. In addition to @ALabrosik and @ReneEnriquez answers.
Download pdf.js dist and place it under src/main/resources
├── pom.xml
├── src
│ └── main
│ ├── java
│ │ └── me
│ │ └── example
│ │ ├── JSLogListener.java
│ │ ├── Launcher.java
│ │ └── WebController.java
│ └── resources
│ ├── build
│ │ ├── pdf.js
│ │ └── pdf.worker.js
│ ├── main.fxml
│ ├── web
│ │ ├── cmaps
│ │ ├── compatibility.js
│ │ ├── debugger.js
│ │ ├── images
│ │ ├── l10n.js
│ │ ├── locale
│ │ ├── viewer.css
│ │ ├── viewer.html
│ │ └── viewer.js
Create the following fxml file (you should wrap WebView in TabPane or similar container to avoid problems with scrolling support)
To prevent pdf.js from opening demo pdf file on startup, open web/viewer.js
and clear DEFAULT_URL
value.
var DEFAULT_URL = '';
Open web/viewer.html
and add script block:
Now the controller (see code comments for explanation).
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("/web/viewer.html").toExternalForm();
// connect CSS styles to customize pdf.js appearance
engine.setUserStyleSheetLocation(getClass().getResource("/web.css").toExternalForm());
engine.setJavaScriptEnabled(true);
engine.load(url);
engine.getLoadWorker()
.stateProperty()
.addListener((observable, oldValue, newValue) -> {
// 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("/path/to/file"));
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 -> {
try {
byte[] data = FileUtils.readFileToByteArray(new File("/path/to/another/file"));
String base64 = Base64.getEncoder().encodeToString(data);
engine.executeScript("openFileFromBase64('" + base64 + "')");
} catch (Exception e) {
e.printStackTrace();
}
});
}
}
Some of pdf.js functions will not work: open file (cause pdf.js have no access to URL outside JAR), printing etc. To hide corresponding toolbar buttons you can add the following lines to web.css:
#toolbarViewerRight {
display:none;
}
That's all. The rest of the code is trivial.
public class JSLogListener {
public void log(String text) {
System.out.println(text);
}
}
public class Launcher extends Application {
public static void main(String[] args) {
Application.launch();
}
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("/main.fxml"));
primaryStage.setTitle("PDF test app");
primaryStage.setScene(new Scene(root, 1280, 576));
primaryStage.show();
}
}
Hope this helps to someone.