Automattic / Automattic/node-canvas

Nodejs crash when processing PDF file with pdfjs

Open
#2,349 5 comments 0 reactions 0 assignees View on GitHub
Images & Formats: SVG
Dominant language
JavaScript
Stars
10.7k
Forks
1.2k
Avg merge
4d 8h
Merged PRs (30d)
1

Description

## Issue

nodejs crashes when processing a PDF file containing an image. Enclosed the PDF file too.

The crash is as follows:

```
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: 0x7ffba0dd5d4d Context2d::CreateImageData(Nan::FunctionCallbackInfo const&) [/home/sridhar/devl/node-canvas/node_modules/canvas/build/Release/canvas.node]
5: 0x7ffba0dcc3d3 [/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]
```

## Steps to Reproduce
The following sample is taken from pdfjs example [here:](https://github.com/mozilla/pdf.js/blob/master/examples/node/pdf2png/pdf2png.mjs) customized a bit to output an SVG file.

```js
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));

// 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;
const page = await pdfDocument.getPage(1);
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);
}
```

## Your Environment
* From package.json - `"canvas": "^2.11.2" and `"pdfjs-dist": "^4.0.379"`
* node -v shows: `v18.18.2`
[sample.pdf](https://github.com/Automattic/node-canvas/files/14280865/sample.pdf)

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.