Skip to content
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

Crash when processing PDF containing an image using nodejs and node-canvas #17677

Closed
jhpage opened this issue Feb 16, 2024 · 1 comment
Closed

Comments

@jhpage
Copy link

jhpage commented Feb 16, 2024

Crash when running the example with a PDF file containing an image

FATAL ERROR: v8::ToLocalChecked Empty MaybeLocal.
 1: 0xb85bc0 node::Abort() [node]
 2: 0xa94749 node::OOMErrorHandler(char const*, bool) [node]
 3: 0xd66a2a v8::Utils::ReportApiFailure(char const*, char const*) [node]
 4: 0x7fa014251d4d Context2d::CreateImageData(Nan::FunctionCallbackInfo<v8::Value> const&) [/home/sridhar/devl/node-canvas/node_modules/canvas/build/Release/canvas.node]
 5: 0x7fa0142483d3  [/home/sridhar/devl/node-canvas/node_modules/canvas/build/Release/canvas.node]
 6: 0xdc4e00  [node]
 7: 0xdc633f v8::internal::Builtin_HandleApiCall(int, unsigned long*, v8::internal::Isolate*) [node]
 8: 0x1705c39  [node]

Attach (recommended) or Link to PDF file here:
sample.pdf

Configuration:

  • node v18.18.2
  • Ubuntu Linux 5.4.0-171-generic #189-Ubuntu SMP Fri Jan 5 14:23:02 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
  • pdfjs-dist version is 4.0.379
  • No, this is not a browser extension.

Steps to reproduce the problem:
The code for reproducing the above bug. Example code taken from here:

import { strict as assert } from "assert";
import Canvas from "canvas";
import fs from "fs";
import { getDocument } from "pdfjs-dist/legacy/build/pdf.mjs";

class NodeCanvasFactory {
    create(width, height) {
	const canvas = Canvas.createCanvas(width, height, 'svg');
	const context = canvas.getContext("2d");
	return {
	    canvas,
	    context,
	};
    }
    reset(canvasAndContext, width, height) {
	canvasAndContext.canvas.width = width;
	canvasAndContext.canvas.height = height;
    }
    destroy(canvasAndContext) {
	canvasAndContext.canvas.width = 0;
	canvasAndContext.canvas.height = 0;
	canvasAndContext.canvas = null;
	canvasAndContext.context = null;
    }
}

// Some PDFs need external cmaps.
const CMAP_URL = "./node_modules/pdfjs-dist/cmaps/";
const CMAP_PACKED = true;

// Where the standard fonts are located.
const STANDARD_FONT_DATA_URL = "./node_modules/pdfjs-dist/standard_fonts/";

const canvasFactory = new NodeCanvasFactory();

// Loading file from file system into typed array.
const pdfPath = "./sample.pdf"
const data = new Uint8Array(fs.readFileSync(pdfPath));
console.log('read pdf file')

// Load the PDF file.
const loadingTask = getDocument({
    data,
    cMapUrl: CMAP_URL,
    cMapPacked: CMAP_PACKED,
    standardFontDataUrl: STANDARD_FONT_DATA_URL,
    canvasFactory,
});

try {
    const pdfDocument = await loadingTask.promise;
    console.log('pdf document read')
    const page = await pdfDocument.getPage(1);
    console.log('page 1 fetched')
    const viewport = page.getViewport({ scale: 1.0 });
    const canvasAndContext = canvasFactory.create(
	viewport.width,
	viewport.height
    );
    const renderContext = {
	canvasContext: canvasAndContext.context,
	viewport,
    };
    const renderTask = page.render(renderContext);
    await renderTask.promise;
    const image = canvasAndContext.canvas.toBuffer();
    fs.writeFile("output.svg", image, function (error) {
	if (error) {
	    console.error("Error: " + error);
	} else {
	    console.log("Finished converting first page of PDF to output.svg");
	}
    });
    page.cleanup();
} catch (reason) {
    console.log(reason);
}

What is the expected behavior? (add screenshot)
The code should output an SVG file. It crashes at the await renderTask.promise line.

What went wrong? (add screenshot)
Code crashes.

Link to a viewer (if hosted on a site other than mozilla.github.io/pdf.js or as Firefox/Chrome extension):
No viewer. Just running this code from command line.

@Snuffleupagus
Copy link
Collaborator

Given Automattic/node-canvas#2349 (comment) this really seems like a node-canvas bug.

@Snuffleupagus Snuffleupagus closed this as not planned Won't fix, can't repro, duplicate, stale Feb 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants