使用 mozilla pdf.js 在 Android WebView 中显示 PDF 文件 Android API 级别低于 19 [英] Show PDF file in Android WebView using mozilla pdf.js Android API Level below 19

查看:22
本文介绍了使用 mozilla pdf.js 在 Android WebView 中显示 PDF 文件 Android API 级别低于 19的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Android WebView 中使用 mozilla pdf.js 来显示 PDF 文件.

I'm using mozilla pdf.js in Android WebView to Show PDF files.

代码在 Android API 级别 19 中运行良好.

Code is working fine in Android API Level 19.

Uri path = Uri.parse(Environment.getExternalStorageDirectory().toString() + "/test.pdf");
webView.loadUrl("file:///android_asset/pdfviewer/index.html?file=" + path); 

但它不适用于 Android API 级别 16 及以下.

设备上显示白色空白屏幕.

A White blank Screen displays on Device.

有什么办法可以解决这个问题吗?

Is there any way to resolve this issue?

推荐答案

我遇到了同样的问题.首先,如果不包含编译文件(pdf.js 和 pdf.worker.js),而是像 helloworld 示例中那样单独包含所有 js 文件,那么使用 2013 年 12 月的旧版本 pdf.js 似乎可行.

I've had the same problem. First it seemed that using an older version of pdf.js from December 2013 would work, if instead of including the compiled files (pdf.js and pdf.worker.js) you'd include all the js files separately like in the helloworld example.

然后我发现了这个SO post.有人为 Android 编写了一个 pdf.js 示例(Butelo).我试过了,项目资产文件夹中的 .js 文件甚至可以在 Android 2.3 上运行,但前提是通过 http 访问.在帖子中还有关于如何让它与 file://urls 一起工作的建议.我找到了另一个解决方案:

Then I discovered this SO post. Someone there has put together a pdf.js example for Android (Butelo). I've tried it, the .js files in the asset folder of the project even work on Android 2.3, but only if accessed over http. In the post there are also suggestions on how to get this to work with file:// urls. I found yet another solution:

您使用 Java 阅读 PDF 文件并将其转换为 Base64.通过这种方式,您可以绕过 file://限制.

You read and convert the PDF file to Base64 with Java. In this way you're circumventing the file:// restriction.

ByteArrayOutputStream ous = null;
InputStream ios = null;
String imageData = null;
try {
    byte[] buffer = new byte[4096];
    ous = new ByteArrayOutputStream();
    ios = new FileInputStream(pdfFile);
    int read = 0;
    while ( (read = ios.read(buffer)) != -1 ) {
        ous.write(buffer, 0, read);
    }                                  
    imageData = Base64.encodeToString(ous.toByteArray(), Base64.DEFAULT).replace("
", "").replace("
", "");                                       
} catch (Exception e) {
    e.printStackTrace();
}           

然后通过将 imageData 传递给它来加载 WebView.使用 JavaScript,您需要将 Base64 转换为 UInt8 数组.可能您可以直接在 Java 中执行此操作并避免 Base64 绕路,但我不知道如何并且不确定如何将该数组放入您的 WebView.

You then load the WebView by somehow passing the imageData to it. With JavaScript you'll need to convert the Base64 to a UInt8 array. Probably you could do this directly in Java and avoid the Base64 detour, but I didn't figure out how and I'm not sure how you'd get that array into your WebView.

function fromBase64(dataURI) {           
    var raw = window.atob(dataURI);
    var rawLength = raw.length;
    var array = new Uint8Array(new ArrayBuffer(rawLength));

    for(i = 0; i < rawLength; i++) {
         array[i] = raw.charCodeAt(i);
    }
    return array;
}

pdf.js 将采用 UInt8 数组,就像它是一个 URL.所以你可以使用:

pdf.js will take the UInt8 array like if it was a URL. So you can use:

PDFJS.getDocument(fromBase64(base64DataFromJava));

此方法肯定适用于 Android 4.0 及更高版本.出于某种奇怪的原因,它目前对我来说不适用于 Android 2.3,但这可能与我的项目中的其他一些问题有关.当然,在较旧的设备上,渲染需要一些时间(有时需要半分钟),但无论如何都是如此.

This method works for sure on Android 4.0 and above. For some strange reason it currently doesn't work on Android 2.3 for me, but that might be related to some other problem in my project. Of course, on older devices, the rendering will take some time (sometimes half a minute), but that'll be the case anyways.

这篇关于使用 mozilla pdf.js 在 Android WebView 中显示 PDF 文件 Android API 级别低于 19的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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