简体   繁体   中英

PDF doesn't load at all in javafx webview

The purpose of this code is to display a pdf file through webview.
The pdf is read on button click, but the screen just remains white and loads forever. There are no errors as the pdf viewer loads in the web view once i click the button, and nothing happens.

    public class customFXMLController implements Initializable {

            @FXML
            private TabPane tabPane;
            @FXML
            private WebView web;

            @FXML
            private Button btn;

             WebEngine engine;

            @Override
            public void initialize(URL location, ResourceBundle rb) {
                 engine = web.getEngine();
                String url = getClass().getResource("/web/viewer.html").toExternalForm();
                engine.setUserStyleSheetLocation(getClass().getResource("/web/viewer.css").toExternalForm());
                engine.setJavaScriptEnabled(true);
                engine.load(url);    
             }

            public void getWebView() {
                startPDF();
                tabPane.getSelectionModel().select(1);

            }

//this is the button click event
             public void startPDF() {
                try {
                     byte[] data = Files.readAllBytes(new File("C:/Users/dan1223/Desktop/TelevisionMan.pdf").toPath());
                    String base64 = Base64.getEncoder().encodeToString(data);
                    web.getEngine().executeScript("openFileFromBase64('" + base64 + "')");

               } catch (Exception e) {
                    e.printStackTrace();
               }

            }

        }

Javafx is not part of JDK from 11(LTS) and above and it is available as a separate standalone.

OpenJavaFX : https://gluonhq.com/products/javafx/

A fix for the PDF loading issue is included in Javafx 13.

I don't know which Java version do you have, but there is a known bug with the WebView of JavaFX and pdf.js using java 1.8_131 and higher: https://bugs.openjdk.java.net/browse/JDK-8180825 . It is a font problem.

As pointed out in the bug ticket, a fix has been included with Java 9. I just tried it, and it works.

Java 9 JDK : http://www.oracle.com/technetwork/java/javase/downloads/jdk9-downloads-3848520.html

EDIT: The fix also has been included in java 1.8_152.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM