PDF doesn't load at all in javafx webview

本秂侑毒 提交于 2019-12-10 12:15:32

问题


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();
               }

            }

        }

回答1:


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.



来源:https://stackoverflow.com/questions/45666551/pdf-doesnt-load-at-all-in-javafx-webview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!