好的,经过一番测试,解决方法很简单:
使用 Ajax 调用函数获取 PDF 数据,该函数可以确定要查看的实际文件。在那个 PHP 文件中... 正常使用 fopen 和 fread 将文件读入内存。使用 base64_encode 转换为 base64 将该字符串传递回调用 Javascript。
在原始调用函数中,使用以下内容将字符串转换为 Uint 数组,然后将其传递给 PDFJS 库...
## The function that turns the base64 string into whatever a Uint8 array is...
function base64ToUint8Array(base64) {
var raw = atob(base64);
var uint8Array = new Uint8Array(raw.length);
for (var i = 0; i < raw.length; i++) {
uint8Array[i] = raw.charCodeAt(i);
}
return uint8Array;
}
## the guts that gets the file data, calls the above function to convert it, and then calls PDF.JS to display it
$.ajax({
type: "GET",
data: {file: <a file id or whatever distinguishes this PDF>},
url: 'getFilePDFdata.php', (the PHP file that reads the data and returns it encoded)
success: function(base64Data){
var pdfData = base64ToUint8Array(base64Data);
## Loading document.
PDFJS.getDocument(pdfData).then(function (pdfDocument) {
## Document loaded, specifying document for the viewer and
## the (optional) linkService.
pdfViewer.setDocument(pdfDocument);
pdfLinkService.setDocument(pdfDocument, null);
});
}
});