New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

examples/node/pdf2png example fails when using any other file other than the helloworld.pdf example file #8489

Closed
nicholaswmin opened this Issue Jun 6, 2017 · 9 comments

Comments

Projects
None yet
@nicholaswmin
Copy link

nicholaswmin commented Jun 6, 2017

Test file: gal-trace.pdf

Configuration:

  • Node.js v7.6.0
  • OSX Sierra
  • pdf.js v1.8.188

Steps to reproduce the problem:

  1. Add attached gal-trace.pdf file in examples/helloworld/ folder
  2. Change pdfURL in examples/node/pdf2png/pdf2png to point to examples/helloworld/gal-trace.pdf
  3. Run the pdf2png.js example

What is the expected behavior?

  • It creates 14 images - one image for each page in gal-trace.pdf

What went wrong? (add screenshot)

  • No images are created
  • Terminal logs the following error:
# PDF document loaded.
(node:16114) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): ReferenceError: document is not defined
(node:16114) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:16114) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): ReferenceError: document is not defined
(node:16114) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 3): ReferenceError: document is not defined
(node:16114) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 4): ReferenceError: document is not defined
(node:16114) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 5): ReferenceError: document is not defined
(node:16114) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 6): ReferenceError: document is not defined
(node:16114) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 7): ReferenceError: document is not defined
(node:16114) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 8): ReferenceError: document is not defined
(node:16114) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 9): ReferenceError: document is not defined
(node:16114) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 10): ReferenceError: document is not defined
(node:16114) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 11): ReferenceError: document is not defined
(node:16114) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 12): ReferenceError: document is not defined
(node:16114) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 13): ReferenceError: document is not defined

@yurydelendik yurydelendik added the 1-other label Jun 6, 2017

@ua1905

This comment has been minimized.

Copy link

ua1905 commented Jun 7, 2017

Same problem for me

@jlfabi

This comment has been minimized.

Copy link

jlfabi commented Jun 27, 2017

I am having a similar issue. Does the PDF need be generated by GNUpdf only?

@taxilian

This comment has been minimized.

Copy link

taxilian commented Jul 14, 2017

The issue appears to be related in some way to the fontLoader; I would guess it's in some way connected to having a custom embedded font in the PDF, but I'm still investigating. The actual location of the exception is:

Unhandled rejection ReferenceError: document is not defined
    at Object.fontLoaderInsertRule [as insertRule] (/Users/richard/code/gc/gcformservice/node_modules/pdfjs-dist/build/webpack:/src/display/font_loader.js:36:42)
    at Object.fontLoaderBind [as bind] (/Users/richard/code/gc/gcformservice/node_modules/pdfjs-dist/build/webpack:/src/display/font_loader.js:136:16)
    at Object.transportObj (/Users/richard/code/gc/gcformservice/node_modules/pdfjs-dist/build/webpack:/src/display/api.js:1727:29)
    at LoopbackPort.MessageHandler._onComObjOnMessage (/Users/richard/code/gc/gcformservice/node_modules/pdfjs-dist/build/webpack:/src/shared/util.js:1300:19)
    at LoopbackPort.<anonymous> (/Users/richard/code/gc/gcformservice/node_modules/pdfjs-dist/build/webpack:/src/display/api.js:1177:18)
    at Array.forEach (native)
    at /Users/richard/code/gc/gcformservice/node_modules/pdfjs-dist/build/webpack:/src/display/api.js:1176:23

You can find this yourself by using Bluebird, setting globals.Promise = require("bluebird");. Unlike native Promise bluebird logs unhandled rejections with a stack trace by default, which helps in this case.

@lafeuil

This comment has been minimized.

Copy link

lafeuil commented Sep 4, 2017

I find a workaround in #7929 :
try using PDFJS.disableFontFace=true;

It's not fixing issue, it just using lines to draw letters instead of fonts and fillText.

@raliste

This comment has been minimized.

Copy link

raliste commented Nov 14, 2017

What about documents with images?

Unhandled rejection ReferenceError: HTMLElement is not defined
    at CanvasGraphics_paintInlineImageXObject [as paintInlineImageXObject] (/Users/raliste/Work-Unsafe/preview/node_modules/pdfjs-dist/build/pdf.js:15331:30)
    at CanvasGraphics_paintImageMaskXObject [as paintImageMaskXObject] (/Users/raliste/Work-Unsafe/preview/node_modules/pdfjs-dist/build/pdf.js:15242:12)
@noauthoritybuturself

This comment has been minimized.

Copy link

noauthoritybuturself commented Nov 28, 2017

any idea how to solve it for documents with images?

@cemerick

This comment has been minimized.

Copy link
Contributor

cemerick commented Nov 28, 2017

I'm seeing the same error as @nicholaswmin with a different (very simple/small) document. A couple of data points:

  1. I'm only calling .getOperatorList(); that's where my interactions with pdf.js end (not using its rasterization/display stuff in this context). Calling .getTextContent() (for example) does not yield any error.
  2. PDFJS.disableFontFace=true also is effective at eliminating the error as mentioned by @lafeuil. I haven't dug into how that setting will or won't affect what I'm doing, so I don't know if it's actually a workaround (yet).
@swftvsn

This comment has been minimized.

Copy link
Contributor

swftvsn commented Mar 20, 2018

So I chased these problems a bit today, and here's what I've found so far:

First of all, you don't need Bluebird to get the full stack of unhandled exceptions, just add this at the top of your script:

process.on('unhandledRejection', r => console.log(r))

The examples are written in typescript andI drive this in Google Cloud Functions, so I require

import { PDFJSStatic, PDFDocumentProxy, PDFSource, PDFPageProxy } from 'pdfjs-dist'
const Canvas = require('canvas-prebuilt')
const pdfjs = require('pdfjs-dist') as PDFJSStatic

OK, let's start..

  1. disableFontFace=true; works to combat the font loader issue. Please do note, that it must be fed as a parameter to pdfjs.getDocument today instead of global setting like this:
const source = {
  data: pdfBuffer,
  nativeImageDecoderSupport: 'none',
  disableFontFace: true
} as PDFSource
pdfjs.getDocument(source).then(result => {
  1. nativeImageDecoderSupport must be set to 'none' as shown in 1. to fix the "Image is not defined" issue explained in #9373

  2. To fix "Unhandled rejection ReferenceError: HTMLElement is not defined" in PDF's that contain images, one must introduce a global as follows:

global.HTMLElement = class {}

The stub can be empty, as the only point where it is used is this instance of: (the comment even states that this does not work in node..)

pdf.js/src/display/canvas.js

Lines 2115 to 2116 in 25bbff4

// instanceof HTMLElement does not work in jsdom node.js module
if (imgData instanceof HTMLElement || !imgData.data) {

This could actually be fixed quite easily just by checking if HTMLElement exists like this:

 // typeof check is needed in node.js, see issue #8489
 if ((typeof HTMLElement === 'function' && imgData instanceof HTMLElement) || !imgData.data) {

swftvsn added a commit to swftvsn/pdf.js that referenced this issue Mar 20, 2018

Improve node.js support
This change fixes "Unhandled rejection ReferenceError: HTMLElement is not defined" issue that is discussed in more detail in mozilla#8489.
@swftvsn

This comment has been minimized.

Copy link
Contributor

swftvsn commented Mar 20, 2018

Opened PR to address the HTMLElement issue, see #9588

swftvsn added a commit to swftvsn/pdf.js that referenced this issue Mar 21, 2018

Improve node.js support
This change fixes "Unhandled rejection ReferenceError: HTMLElement is not defined" issue that is discussed in more detail in mozilla#8489.

IsaacSchemm added a commit to IsaacSchemm/pdf.js-seamonkey that referenced this issue Apr 3, 2018

Improve node.js support
This change fixes "Unhandled rejection ReferenceError: HTMLElement is not defined" issue that is discussed in more detail in mozilla#8489.

(cherry picked from commit c20426e)

movsb pushed a commit to movsb/pdf.js that referenced this issue Jul 14, 2018

Improve node.js support
This change fixes "Unhandled rejection ReferenceError: HTMLElement is not defined" issue that is discussed in more detail in mozilla#8489.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment