4

I need to display a PDF inside the default WebView of JavaFX. I assumed, that i would easily be able to do that like this.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class ShowPdfTest extends Application {
    public static void main(String[] args) {
        launch();
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        WebView webView = new WebView();
        WebEngine engine = webView.getEngine();
        Scene scene = new Scene(webView);
        primaryStage.setScene(scene);
        primaryStage.show();

//        engine.load("https://www.google.com");
        engine.load("http://www.orimi.com/pdf-test.pdf");
    }
}

I was wrong. Nothing happens. It seems like the WebEngine has no built-in PDF-Renderer. I tried JxBrowser which worked fine, but is a rather costly alternative.

So is there any way to display a PDF directly inside the default WebView component?

Anton Horst
  • 263
  • 1
  • 4
  • 11
  • 2
    You can use [Mozilla Pdf.js](https://github.com/mozilla/pdf.js/) in your WebView. – Cem Ikta Jul 13 '17 at 01:20
  • OK, but what if i have something like this? https://www.cs.tut.fi/~jkorpela/html/iframe-pdf.html – Anton Horst Jul 13 '17 at 07:23
  • You don't need iframe for Pdf.js! You can call your pdf file with Pdf.js in WebView. – Cem Ikta Jul 13 '17 at 14:21
  • No, i get that i can use Pdf.js to display a PDF inside a WebView. But my goal is to render PDFs dynamically like any other browser. So if a webpage has ebedded a PDF inside iframe or JavaScript i want to be able to show the whole page with the embedded PDF. – Anton Horst Jul 13 '17 at 15:26

1 Answers1

1

JxBrowser which worked fine, but is a rather costly alternative.

If you your application is an open-source project, you can obtain the JxBrowser Open-Source license here.

Nikita Shvinagir
  • 438
  • 3
  • 10