在javafx webview中根本不加载PDF [英] PDF doesn't load at all in javafx webview

查看:710
本文介绍了在javafx webview中根本不加载PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码的目的是通过webview显示pdf文件。

按钮单击时读取pdf,但屏幕保持白色并永久加载。单击按钮后,pdf查看器在Web视图中加载没有错误,没有任何反应。

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

            }

        }


推荐答案

我不知道你有哪个Java版本,但是使用java 1.8_131及更高版本的JavaFX和pdf.js的WebView存在已知错误: https://bugs.openjdk.java.net/browse/JDK-8180825 。这是一个字体问题。

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.

正如错误故障单中所指出的,Java 9中已经包含了一个修复程序。我只是尝试了它,它可以工作。

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

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

编辑:修复程序也包含在java 1.8_152中。

The fix also has been included in java 1.8_152.

这篇关于在javafx webview中根本不加载PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆