我已经基于https://blog.samirhadzic.com/2017/02/09/show-pdf-in-your-application/在我的javaFX应用程序中实现了pdfviewer。但是,它不会加载任何pdf。 pdf查看器显示一个错误:
PDF.js v2.0.943(内部版本:dc98bf76) 消息:输入错误
我尝试了多个pdf,但它们没有损坏。
engine = web.getEngine();
String url = getClass().getResource("resources/web/viewer.html").toExternalForm();
engine.setUserStyleSheetLocation(getClass().getResource("web.css").toExternalForm());
engine.setJavaScriptEnabled(true);
engine.load(url);
engine.getLoadWorker()
.stateProperty()
.addListener((observable, oldValue, newValue) -> {
JSObject window = (JSObject) engine.executeScript("window");
window.setMember("java", new JSLogListener());
engine.executeScript("console.log = function(message){ java.log(message); };");
if (newValue == Worker.State.SUCCEEDED) {
try {
byte[] data = FileUtils.readFileToByteArray(new File("C:/werkruimte/123.pdf"));
String base64 = Base64.getEncoder().encodeToString(data);
engine.executeScript("openFileFromBase64('" + base64 + "')");
} catch (Exception e) {
e.printStackTrace();
}
}
});