From 96e5243cafad6190f3f3a1815416bc24fcc99f44 Mon Sep 17 00:00:00 2001 From: Arfat Salman Date: Thu, 3 Oct 2024 14:26:19 +0200 Subject: [PATCH] Update PDFJS to v4 (IN-11761) (#9) * init * test * test --- .github/workflows/main.yml | 2 +- package-lock.json | 72 ++++++++++++++++---------------------- package.json | 3 +- src/app.js | 24 +++++-------- src/lib.js | 27 ++++---------- test/test.js | 18 ++++++---- 6 files changed, 59 insertions(+), 87 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 55067c8..c4630e5 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -15,7 +15,7 @@ jobs: - name: Setup Node uses: actions/setup-node@v1 with: - node-version: '14' + node-version: '21' - name: Cache Node.js modules uses: actions/cache@v2 with: diff --git a/package-lock.json b/package-lock.json index e7cfc8a..cc7b8b7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,7 @@ "dependencies": { "canvas": "^2.11.0", "commander": "~10.0.0", - "pdfjs-dist": "~3.3.122" + "pdfjs-dist": "^4.6.82" }, "bin": { "pdfthumbnail": "src/app.js" @@ -315,9 +315,9 @@ } }, "node_modules/canvas": { - "version": "2.11.0", - "resolved": "https://registry.npmjs.org/canvas/-/canvas-2.11.0.tgz", - "integrity": "sha512-bdTjFexjKJEwtIo0oRx8eD4G2yWoUOXP9lj279jmQ2zMnTQhT8C3512OKz3s+ZOaQlLbE7TuVvRDYDB3Llyy5g==", + "version": "2.11.2", + "resolved": "https://registry.npmjs.org/canvas/-/canvas-2.11.2.tgz", + "integrity": "sha512-ItanGBMrmRV7Py2Z+Xhs7cT+FNt5K0vPL4p9EZ/UX/Mu7hFbkxSjKF2KVtPwX7UYWp7dRKnrTvReflgrItJbdw==", "hasInstallScript": true, "dependencies": { "@mapbox/node-pre-gyp": "^1.0.0", @@ -1559,24 +1559,25 @@ "node": ">=8" } }, - "node_modules/path2d-polyfill": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/path2d-polyfill/-/path2d-polyfill-2.0.1.tgz", - "integrity": "sha512-ad/3bsalbbWhmBo0D6FZ4RNMwsLsPpL6gnvhuSaU5Vm7b06Kr5ubSltQQ0T7YKsiJQO+g22zJ4dJKNTXIyOXtA==", + "node_modules/path2d": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/path2d/-/path2d-0.2.1.tgz", + "integrity": "sha512-Fl2z/BHvkTNvkuBzYTpTuirHZg6wW9z8+4SND/3mDTEcYbbNKWAy21dz9D3ePNNwrrK8pqZO5vLPZ1hLF6T7XA==", + "optional": true, "engines": { - "node": ">=8" + "node": ">=6" } }, "node_modules/pdfjs-dist": { - "version": "3.3.122", - "resolved": "https://registry.npmjs.org/pdfjs-dist/-/pdfjs-dist-3.3.122.tgz", - "integrity": "sha512-98WC09jOq3OuqrmF5+LZfcyzTlGA0sY9ocMBbWZ/H6Pwni7deptxwkNZVLieOz+4nSoTEW25PsfnfOj3ELrHdA==", - "dependencies": { - "path2d-polyfill": "^2.0.1", - "web-streams-polyfill": "^3.2.1" + "version": "4.6.82", + "resolved": "https://registry.npmjs.org/pdfjs-dist/-/pdfjs-dist-4.6.82.tgz", + "integrity": "sha512-BUOryeRFwvbLe0lOU6NhkJNuVQUp06WxlJVVCsxdmJ4y5cU3O3s3/0DunVdK1PMm7v2MUw52qKYaidhDH1Z9+w==", + "engines": { + "node": ">=18" }, "optionalDependencies": { - "canvas": "^2.11.0" + "canvas": "^2.11.2", + "path2d": "^0.2.1" } }, "node_modules/picomatch": { @@ -1975,14 +1976,6 @@ "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==" }, - "node_modules/web-streams-polyfill": { - "version": "3.2.1", - "resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.2.1.tgz", - "integrity": "sha512-e0MO3wdXWKrLbL0DgGnUV7WHVuw9OUvL4hjgnPkIeEvESk74gAITi5G606JtZPp39cd8HA9VQzCIvA49LpPN5Q==", - "engines": { - "node": ">= 8" - } - }, "node_modules/webidl-conversions": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", @@ -2345,9 +2338,9 @@ "dev": true }, "canvas": { - "version": "2.11.0", - "resolved": "https://registry.npmjs.org/canvas/-/canvas-2.11.0.tgz", - "integrity": "sha512-bdTjFexjKJEwtIo0oRx8eD4G2yWoUOXP9lj279jmQ2zMnTQhT8C3512OKz3s+ZOaQlLbE7TuVvRDYDB3Llyy5g==", + "version": "2.11.2", + "resolved": "https://registry.npmjs.org/canvas/-/canvas-2.11.2.tgz", + "integrity": "sha512-ItanGBMrmRV7Py2Z+Xhs7cT+FNt5K0vPL4p9EZ/UX/Mu7hFbkxSjKF2KVtPwX7UYWp7dRKnrTvReflgrItJbdw==", "requires": { "@mapbox/node-pre-gyp": "^1.0.0", "nan": "^2.17.0", @@ -3248,19 +3241,19 @@ "integrity": "sha512-ojmeN0qd+y0jszEtoY48r0Peq5dwMEkIlCOu6Q5f41lfkswXuKtYrhgoTpLnyIcHm24Uhqx+5Tqm2InSwLhE6Q==", "dev": true }, - "path2d-polyfill": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/path2d-polyfill/-/path2d-polyfill-2.0.1.tgz", - "integrity": "sha512-ad/3bsalbbWhmBo0D6FZ4RNMwsLsPpL6gnvhuSaU5Vm7b06Kr5ubSltQQ0T7YKsiJQO+g22zJ4dJKNTXIyOXtA==" + "path2d": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/path2d/-/path2d-0.2.1.tgz", + "integrity": "sha512-Fl2z/BHvkTNvkuBzYTpTuirHZg6wW9z8+4SND/3mDTEcYbbNKWAy21dz9D3ePNNwrrK8pqZO5vLPZ1hLF6T7XA==", + "optional": true }, "pdfjs-dist": { - "version": "3.3.122", - "resolved": "https://registry.npmjs.org/pdfjs-dist/-/pdfjs-dist-3.3.122.tgz", - "integrity": "sha512-98WC09jOq3OuqrmF5+LZfcyzTlGA0sY9ocMBbWZ/H6Pwni7deptxwkNZVLieOz+4nSoTEW25PsfnfOj3ELrHdA==", + "version": "4.6.82", + "resolved": "https://registry.npmjs.org/pdfjs-dist/-/pdfjs-dist-4.6.82.tgz", + "integrity": "sha512-BUOryeRFwvbLe0lOU6NhkJNuVQUp06WxlJVVCsxdmJ4y5cU3O3s3/0DunVdK1PMm7v2MUw52qKYaidhDH1Z9+w==", "requires": { - "canvas": "^2.11.0", - "path2d-polyfill": "^2.0.1", - "web-streams-polyfill": "^3.2.1" + "canvas": "^2.11.2", + "path2d": "^0.2.1" } }, "picomatch": { @@ -3521,11 +3514,6 @@ "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==" }, - "web-streams-polyfill": { - "version": "3.2.1", - "resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.2.1.tgz", - "integrity": "sha512-e0MO3wdXWKrLbL0DgGnUV7WHVuw9OUvL4hjgnPkIeEvESk74gAITi5G606JtZPp39cd8HA9VQzCIvA49LpPN5Q==" - }, "webidl-conversions": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", diff --git a/package.json b/package.json index 583d6d5..401c52d 100644 --- a/package.json +++ b/package.json @@ -7,12 +7,13 @@ "dependencies": { "canvas": "^2.11.0", "commander": "~10.0.0", - "pdfjs-dist": "~3.3.122" + "pdfjs-dist": "^4.6.82" }, "devDependencies": { "eslint": "^8.33.0", "mocha": "^10.2.0" }, + "type": "module", "scripts": { "test": "mocha" }, diff --git a/src/app.js b/src/app.js index d264cc4..592db75 100755 --- a/src/app.js +++ b/src/app.js @@ -1,10 +1,12 @@ #!/usr/bin/env node -const { readFile, writeFile } = require("fs").promises; -const { basename, join } = require("path"); -const { getThumbnail, NodeCanvasFactory } = require("./lib"); +import { readFile, writeFile } from "fs/promises"; -const { Command, InvalidOptionArgumentError } = require("commander"); +import { basename, join } from "path"; + +import { getThumbnail, NodeCanvasFactory } from "./lib.js"; + +import { Command, InvalidOptionArgumentError } from "commander"; const program = new Command(); @@ -37,19 +39,9 @@ program const canvasFactory = new NodeCanvasFactory(); try { for (const file of files) { - const outputPath = join( - options.output, - basename(file, ".pdf") + ".jpg" - ); + const outputPath = join(options.output, basename(file, ".pdf") + ".jpg"); const contents = await readFile(file); - const jpegBuffer = await getThumbnail( - contents, - options.pagenum, - options.width, - options.quality, - options.standardFonts, - canvasFactory - ); + const jpegBuffer = await getThumbnail(new Uint8Array(contents), options.pagenum, options.width, options.quality, options.standardFonts, canvasFactory); await writeFile(outputPath, jpegBuffer); } diff --git a/src/lib.js b/src/lib.js index f5aa54e..bade297 100644 --- a/src/lib.js +++ b/src/lib.js @@ -1,10 +1,8 @@ -const assert = require("assert").strict; +import assert from "assert"; -const Canvas = require("canvas"); -const pdfjsLib = require("pdfjs-dist/legacy/build/pdf.js"); +import Canvas from "canvas"; -// todo: Use this once Node.js is upgraded to 16 -// const pdfjsLib = require('pdfjs-dist') +import * as pdfjsLib from "pdfjs-dist/legacy/build/pdf.mjs"; /* Largely copied from https://github.com/mozilla/pdf.js/blob/55f55f58594b9a6947fecaabf8ef4e3b02002023/examples/node/pdf2png/pdf2png.js#L20 */ class NodeCanvasFactory { @@ -37,14 +35,7 @@ class NodeCanvasFactory { } } -async function getThumbnail( - data, - pageNum = 1, - width = 300, - quality = 1.0, - standardFonts = "/tmp/fonts/", - canvasFactory = new NodeCanvasFactory() -) { +async function getThumbnail(data, pageNum = 1, width = 300, quality = 1.0, standardFonts = "/tmp/fonts/", canvasFactory = new NodeCanvasFactory()) { const loadingTask = pdfjsLib.getDocument({ data: data, standardFontDataUrl: standardFonts, @@ -56,10 +47,7 @@ async function getThumbnail( const scale = width / viewport.width; viewport = page.getViewport({ scale: scale }); - const canvasAndContext = canvasFactory.create( - viewport.width, - viewport.height - ); + const canvasAndContext = canvasFactory.create(viewport.width, viewport.height); const renderContext = { canvasContext: canvasAndContext.context, viewport: viewport, @@ -75,7 +63,4 @@ async function getThumbnail( }); } -module.exports = { - NodeCanvasFactory, - getThumbnail, -}; +export { NodeCanvasFactory, getThumbnail }; diff --git a/test/test.js b/test/test.js index b1ba8c7..f8fd4f1 100644 --- a/test/test.js +++ b/test/test.js @@ -1,17 +1,23 @@ -const assert = require("assert"); -const path = require("path"); +import assert from "assert"; -const { readFile, writeFile } = require("fs").promises; +import path from "path"; +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; -const { getThumbnail } = require("../src/lib"); +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + +import { readFile, writeFile } from "fs/promises"; + +import { getThumbnail } from "../src/lib.js"; describe("ProcessFeaturedArticle", function () { it("should successfully generate from wikipedia featured article", async function () { const data = await readFile("test/Herja.pdf"); - const jpegBuffer = await getThumbnail(data); + const jpegBuffer = await getThumbnail(new Uint8Array(data)); await writeFile(path.join(__dirname, "thumbnail.jpg"), jpegBuffer); - assert.strictEqual(jpegBuffer.length, 51013); + assert.strictEqual(jpegBuffer.length, 51133); }); });