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

Update PDFJS to v4 (IN-11761) #9

Merged
merged 3 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
72 changes: 30 additions & 42 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
24 changes: 8 additions & 16 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -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();

Expand Down Expand Up @@ -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);
}
Expand Down
27 changes: 6 additions & 21 deletions src/lib.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -75,7 +63,4 @@ async function getThumbnail(
});
}

module.exports = {
NodeCanvasFactory,
getThumbnail,
};
export { NodeCanvasFactory, getThumbnail };
18 changes: 12 additions & 6 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
Loading