78

我正在尝试使用 pdf.js 从 pdf 呈现页面

通常,使用 url,我可以这样做:

PDFJS.getDocument("http://www.server.com/file.pdf").then(function getPdfHelloWorld(pdf) {
  //
  // Fetch the first page
  //
  pdf.getPage(1).then(function getPageHelloWorld(page) {
    var scale = 1.5;
    var viewport = page.getViewport(scale);

    //
    // Prepare canvas using PDF page dimensions
    //
    var canvas = document.getElementById('the-canvas');
    var context = canvas.getContext('2d');
    canvas.height = viewport.height;
    canvas.width = viewport.width;

    //
    // Render PDF page into canvas context
    //
    page.render({canvasContext: context, viewport: viewport});
  });
});

但在这种情况下,我有 base64 中的文件而不是 url:

data:application/pdf;base64,JVBERi0xLjUKJdDUxdgKNSAwIG9iaiA8PAovTGVuZ3RoIDE2NjUgICAgICAKL0ZpbHRlciAvRmxhdGVEZWNvZGUKPj4Kc3RyZWFtCnjarVhLc9s2...

如何做到这一点?

4

3 回答 3

106

来自 http://mozilla.github.com/pdf.js/build/pdf.js的源代码

/**
 * This is the main entry point for loading a PDF and interacting with it.
 * NOTE: If a URL is used to fetch the PDF data a standard XMLHttpRequest(XHR)
 * is used, which means it must follow the same origin rules that any XHR does
 * e.g. No cross domain requests without CORS.
 *
 * @param {string|TypedAray|object} source Can be an url to where a PDF is
 * located, a typed array (Uint8Array) already populated with data or
 * and parameter object with the following possible fields:
 *  - url   - The URL of the PDF.
 *  - data  - A typed array with PDF data.
 *  - httpHeaders - Basic authentication headers.
 *  - password - For decrypting password-protected PDFs.
 *
 * @return {Promise} A promise that is resolved with {PDFDocumentProxy} object.
 */

因此,使用标准 XMLHttpRequest(XHR) 来检索文档。问题在于 XMLHttpRequests 不支持 data: uris(例如 data:application/pdf;base64,JVBERi0xLjUK ...)。

但是有可能将类型化的 Javascript 数组传递给函数。您唯一需要做的就是将 base64 字符串转换为 Uint8Array。您可以使用https://gist.github.com/1032746上的此功能

var BASE64_MARKER = ';base64,';

function convertDataURIToBinary(dataURI) {
  var base64Index = dataURI.indexOf(BASE64_MARKER) + BASE64_MARKER.length;
  var base64 = dataURI.substring(base64Index);
  var raw = window.atob(base64);
  var rawLength = raw.length;
  var array = new Uint8Array(new ArrayBuffer(rawLength));

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

tl;博士

var pdfAsDataUri = "data:application/pdf;base64,JVBERi0xLjUK..."; // shortened
var pdfAsArray = convertDataURIToBinary(pdfAsDataUri);
PDFJS.getDocument(pdfAsArray)
于 2012-08-23T15:18:02.337 回答
5

根据示例,直接支持 base64 编码,尽管我自己没有测试过。获取您的 base64 字符串(从文件派生或使用任何其他方法加载,POST/GET、websockets 等),使用 atob 将其转换为二进制文件,然后将其解析为 PDFJS API 上的 getDocument,如PDFJS.getDocument({data: base64PdfData});Codetoffel 答案确实适用于我虽然。

于 2018-03-01T17:24:24.983 回答
0

使用 Accepted Answer 对 IE 进行检查并将 dataURI 转换为 UInt8Array;PDFJS 接受的表格

        Ext.isIE ? pdfAsDataUri = me.convertDataURIToBinary(pdfAsDataUri): '';

        convertDataURIToBinary: function(dataURI) {
          var BASE64_MARKER = ';base64,',
            base64Index = dataURI.indexOf(BASE64_MARKER) + BASE64_MARKER.length,
            base64 = dataURI.substring(base64Index),
            raw = window.atob(base64),
            rawLength = raw.length,
            array = new Uint8Array(new ArrayBuffer(rawLength));

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

于 2016-03-17T17:50:44.533 回答