PDFJS Chrome 扩展

PDFJS Chrome Extension

我正在尝试编写一个 chrome 扩展来实现 PDFJS 以从 PDF 中提取数据。我可以在读取本地文件时实现这一点,但我希望解析当前在浏览器中查看的 PDF。

我收到大量以下错误:

'Warning: Ignoring invalid character "33" in hex string'
'Warning: Ignoring invalid character "79" in hex string'...

然后

"Error
    at InvalidPDFExceptionClosure (chrome-extension://nhbmpffpjhndjdpmlahapejappacnkcc/pdfjs/pdf.js:658:35)
    at Object.<anonymous> (chrome-extension://nhbmpffpjhndjdpmlahapejappacnkcc/pdfjs/pdf.js:661:2)
    at __w_pdfjs_require__ (chrome-extension://nhbmpffpjhndjdpmlahapejappacnkcc/pdfjs/pdf.js:52:30)
    at Object.<anonymous> (chrome-extension://nhbmpffpjhndjdpmlahapejappacnkcc/pdfjs/pdf.js:129:23)
    at __w_pdfjs_require__ (chrome-extension://nhbmpffpjhndjdpmlahapejappacnkcc/pdfjs/pdf.js:52:30)
    at chrome-extension://nhbmpffpjhndjdpmlahapejappacnkcc/pdfjs/pdf.js:116:18
    at chrome-extension://nhbmpffpjhndjdpmlahapejappacnkcc/pdfjs/pdf.js:119:10
    at webpackUniversalModuleDefinition (chrome-extension://nhbmpffpjhndjdpmlahapejappacnkcc/pdfjs/pdf.js:31:50)
    at chrome-extension://nhbmpffpjhndjdpmlahapejappacnkcc/pdfjs/pdf.js:32:3"

我不知道是什么导致了错误。

这是我用来打开 PDF 文档的代码

var url = decodeURIComponent(location.href.split('?url=')[1]);
//http://www.pdf995.com/samples/pdf.pdf

var params = {
  cMapPacked: true,
  cMapUrl: "pdfjs/cmaps/",
  disableAutoFetch: false,
  disableCreateObjectURL: false,
  disableFontFace: false,
  disableRange: false,
  disableStream: false,
  docBaseUrl: url,
  isEvalSupported: true,
  maxImageSize: -1,
  pdfBug: false,
  postMessageTransfers: true,
  url: url,
  verbosity: 1
};

pdfjsLib.GlobalWorkerOptions.workerSrc = 'pdfjs/pdf.worker.js';
var loadingTask = (0, pdfjsLib.getDocument)(params);

loadingTask.promise.then(...

我的 background.js 文件正在捕获请求并重定向到扩展,正在使用

chrome.webRequest.onBeforeRequest

改为

chrome.webRequest.onHeadersReceived

已解决问题。

我还更改了加载 PDF 的代码:

var loadingTask = pdfjsLib.getDocument({
  url: url,
  cMapUrl: "pdfjs/cmaps/",
  cMapPacked: true
});

loadingTask.promise.then(...