Skip to content

Commit aaea218

Browse files
committed
[api-minor] Replace the canvas package with @napi-rs/canvas
The `@napi-rs/canvas` package has fewer dependencies, which should *hopefully* make installing and using it easier for `pdfjs-dist` end-users. (Over the years we've seen, repeatedly, that `canvas` can be difficult to install successfully.) Furthermore, this package includes more functionality (such as `Path2D`) which reduces the overall number of dependencies in the PDF.js project. One point to note is that `@napi-rs/canvas` is a fair bit newer than `canvas`, and has a lot fewer users, however looking at the commit history it does seem to be actively maintained. Note that I've successfully tested the [Node.js examples](https://github.com/mozilla/pdf.js/tree/master/examples/node), in particular the `pdf2png` one, with this patch applied and things appear to work fine. Please see: - https://www.npmjs.com/package/@napi-rs/canvas - https://github.com/Brooooooklyn/canvas
1 parent bff6738 commit aaea218

File tree

6 files changed

+186
-100
lines changed

6 files changed

+186
-100
lines changed

README.md

-3
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ all dependencies for PDF.js:
5757

5858
$ npm install
5959

60-
> [!NOTE]
61-
> On MacOS M1/M2 you may see some `node-gyp`-related errors when running `npm install`. This is because one of our dependencies, `"canvas"`, does not provide pre-built binaries for this platform and instead `npm` will try to build it from source. Please make sure to first install the necessary native dependencies using `brew`: https://github.com/Automattic/node-canvas#compiling.
62-
6360
Finally, you need to start a local web server as some browsers do not allow opening
6461
PDF files using a `file://` URL. Run:
6562

examples/node/pdf2png/pdf2png.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ try {
5757
const renderTask = page.render(renderContext);
5858
await renderTask.promise;
5959
// Convert the canvas to an image buffer.
60-
const image = canvasAndContext.canvas.toBuffer();
60+
const image = canvasAndContext.canvas.toBuffer("image/png");
6161
fs.writeFile("output.png", image, function (error) {
6262
if (error) {
6363
console.error("Error: " + error);

gulpfile.mjs

+1-2
Original file line numberDiff line numberDiff line change
@@ -2256,8 +2256,7 @@ function packageJson() {
22562256
bugs: DIST_BUGS_URL,
22572257
license: DIST_LICENSE,
22582258
optionalDependencies: {
2259-
canvas: "^3.0.0-rc2",
2260-
path2d: "^0.2.1",
2259+
"@napi-rs/canvas": "^0.1.60",
22612260
},
22622261
browser: {
22632262
canvas: false,

package-lock.json

+175-63
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
"@jazzer.js/core": "^2.1.0",
1111
"@metalsmith/layouts": "^2.7.0",
1212
"@metalsmith/markdown": "^1.10.0",
13+
"@napi-rs/canvas": "^0.1.60",
1314
"@types/node": "^22.8.7",
1415
"autoprefixer": "^10.4.20",
1516
"babel-loader": "^9.2.1",
1617
"caniuse-lite": "^1.0.30001677",
17-
"canvas": "^3.0.0-rc2",
1818
"core-js": "^3.39.0",
1919
"eslint": "^8.57.1",
2020
"eslint-plugin-import": "^2.31.0",
@@ -37,7 +37,6 @@
3737
"metalsmith": "^2.6.3",
3838
"metalsmith-html-relative": "^2.0.5",
3939
"ordered-read-streams": "^2.0.0",
40-
"path2d": "^0.2.1",
4140
"pngjs": "^7.0.0",
4241
"postcss": "^8.4.47",
4342
"postcss-dark-theme-class": "^1.3.0",

0 commit comments

Comments
 (0)