0
此代碼的目的是通過webview顯示PDF文件。
pdf是在點擊按鈕上閱讀的,但屏幕只是保持白色並永久加載。一旦我點擊按鈕,PDF查看器加載到Web視圖中就沒有錯誤,並且什麼也沒有發生。PDF在javafx中無法加載webview
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();
}
}
}