Search code examples
javascriptgoogle-chrome-extensionpdf.js

PDFJS Chrome Extension


I'm trying to write a chrome extension that implements PDFJS to pull data out of PDF's. I can achieve this when reading a local file, but I wish to parse the current PDF being viewed in the browser.

I'm getting a load of the following errors:

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

And then

"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"

I can't figure out what's causing the error.

This is code I'm using to open the PDF document

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(...

Solution

  • My background.js file that was capturing requests and redirecting to the extension, was using

    chrome.webRequest.onBeforeRequest
    

    Changing to

    chrome.webRequest.onHeadersReceived
    

    Fixed the problem.

    I also changed the code that loads the PDF:

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