简体   繁体   English

使用Webview中的PDFJS库在Javafx中显示Pdf

[英]Displaying Pdf in Javafx using PDFJS library in Webview

My goal is to display pdf documents in my JavaFX application. 我的目标是在JavaFX应用程序中显示pdf文档。 After researching I came across Mozilla's PDFJS library and found it pretty useful. 经过研究,我发现Mozilla的PDFJS库非常有用。 What I'm doing is reading PDF file as a byte array from JAVA and calling the js code in Web view. 我正在做的是从JAVA中读取PDF文件作为字节数组,然后在Web视图中调用js代码。 Here is the code. 这是代码。

JAVA CODE JAVA代码

byte[] data = Files.readAllBytes(Paths.get("D:\\test\\test.pdf"));
    String base64 = Base64.getEncoder().encodeToString(data);   


    btn.setOnMouseClicked(e -> {
        String js = "openFileFromBase64('" + base64 + "')";
        engine.executeScript(js);
    });

Javascript Code JavaScript代码

<script>
var openFileFromBase64 = function(data) {
    var arr = base64ToArrayBuffer(data);
    PDFViewerApplication.open(arr);
}

function base64ToArrayBuffer(base64) {
  var binary_string = window.atob(base64);
  var len = binary_string.length;
  var bytes = new Uint8Array(new ArrayBuffer(len));
  alert(len);
  for (var i = 0; i < len; i++)        {
      bytes[i] = binary_string.charCodeAt(i);
  }
  return bytes.buffer;
}

Problem 问题

I'm able to render some pdf files just fine but I've some pdf files that won't open correctly. 我可以很好地渲染一些pdf文件,但有些pdf文件无法正确打开。 So I did some analysis and found following:- 所以我做了一些分析,发现以下内容:

  1. The problematic pdf files open correctly in adobe reader, firefox and edge browser. 有问题的pdf文件可以在Adobe Reader,firefox和Edge浏览器中正确打开。

  2. I thought the problem might be PDFJS library. 我以为问题可能是PDFJS库。 So I tried placing my pdf file in WEB directory and opened viewer.html. 因此,我尝试将pdf文件放置在WEB目录中,然后打开viewer.html。 To my surprise the pdf displayed correctly. 令我惊讶的是pdf正确显示。

  3. I thought maybe some bytes are lost when I send data from Java to Javascript.So I printed number of bytes on each end and they match. 我以为从Java向Javascript发送数据时可能丢失了一些字节,所以我在两端打印了字节数并且它们匹配。

  4. Finally, I thought that java might be messing up the encoding. 最后,我认为java可能会弄乱编码。 So I read the file from java and wrote the bytes to separate file and the file generated is correct. 因此,我从java中读取了文件,并将字节写入了单独的文件,并且生成的文件是正确的。

I'm trying to understand if I overlooked something. 我想了解我是否忽略了某些内容。 Any suggestions are appreciated. 任何建议表示赞赏。 Thanks in advance. 提前致谢。

Here is how my pdf looks like after it is rendered:- 这是我的pdf呈现后的样子:- 在此处输入图片说明

I had the same problem: No text would render properly in the JavaFX WebView using the current stable release of PDF.js as of today ( v2.0.943 ). 我遇到了同样的问题:截止到今天( v2.0.943 ),使用当前稳定的PDF.js版本,JavaFX WebView中无法正确呈现文本 Image based PDFs render properly. 基于图像的PDF可以正确呈现。

Having a look at the PDF.js release notes, I found that v2.0.943 introduced lots of changes related to fonts and seem to have broken the font rendering in JavaFX. 查看PDF.js发行说明,我发现v2.0.943引入了许多与字体有关的更改,并且似乎破坏了JavaFX中的字体呈现。

The good news is that the current pre-release , v2.1.266 has some bugfixes regarding the handling of fonts and it fixes the text rendering problem in the JavaFX WebView. 好消息是, 当前的预发行版本 v2.1.266中一些有关字体处理的错误修正 ,它修复了JavaFX WebView中的文本呈现问题

If you don't feel confortable using a pre-release, you can use v1.10.100 , text rendering works with this version too, although I recommend using the latest version, because it seems to render the different fonts much better. 如果您不满意使用预发行版,可以使用v1.10.100 ,此版本也可以使用文本渲染,尽管我建议使用最新版本,因为它似乎可以更好地渲染不同的字体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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