Skip to content

Run PDF.js canvas rendering in a web-worker #10319

Closed
@brianmhunt

Description

@brianmhunt

Configuration:

  • Chrome 71
  • Operating system and its version: any
  • PDF.js version: 2.0.943
  • Is a browser extension: no

Steps to reproduce the problem:

  1. Write a web worker like this:
function onmessage () {
  const pdfDocument = await pdfjs.getDocument('/some-hard-coded.pdf')
  const page = await pdfDocument.getPage(1)
  const viewport = pdfPage.getViewport(1)
  const width = 250
  const height = viewport.height * (width / viewport.width)
  const canvas = new OffscreenCanvas(width, height)
  const canvasContext = canvas.getContext('2d', { alpha: false })
  return page.render({ canvasContext, viewport })
})
  1. Call this with e.g. w = new Worker('sample-pdf.js');

What is the expected behavior? (add screenshot)

PDF would render to the OffscreenCanvas

What went wrong? (add screenshot)

ReferenceError: document is not defined
at GenericFontLoader.insertRule (pdf.js:10154)
at GenericFontLoader.bind (pdf.js:10205)
at WorkerTransport. (pdf.js:9177)
at LoopbackPort.MessageHandler._onComObjOnMessage (pdf.js:12833)
at LoopbackPort. (pdf.js:8681)
at Array.forEach ()

i.e. this is failing:

styleElement = this.styleElement = document.createElement('style');

The problem is the absence of a DOM. The issue might be one of documentation, as PDF.js has a Node example, or there may be issues lurking in the deep. In any case I wanted to report it in case there were some obvious guidelines here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions