Skip to content

Commit

Permalink
an example for mozilla/pdf.js#15102
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitalii Savchuk committed Jun 25, 2022
0 parents commit 23d0684
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
17 changes: 17 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = {
presets: [
[
'@babel/preset-env',
{
targets: {
node: 'current'
}
}
]
],
plugins: [
['@babel/proposal-decorators', { legacy: true }],
['@babel/proposal-class-properties', { loose: false }],
['@babel/plugin-proposal-private-methods', { loose: false }]
]
};
29 changes: 29 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "pdf-test",
"version": "1.0.0",
"description": "",
"author": "Vitalii Savchuk <[email protected]>",
"private": true,
"engines": {
"node": ">=10"
},
"scripts": {
"start": "npx babel-node test.js"
},
"dependencies": {
"canvas": "^2.9.3",
"pdfjs-dist": "^2.13.216"
},
"devDependencies": {
"@babel/cli": "^7.14.8",
"@babel/core": "^7.15.0",
"@babel/node": "^7.14.9",
"@babel/plugin-proposal-class-properties": "^7.14.5",
"@babel/plugin-proposal-decorators": "^7.14.5",
"@babel/plugin-transform-runtime": "^7.15.0",
"@babel/polyfill": "^7.12.1",
"@babel/preset-env": "^7.15.0",
"@babel/runtime": "^7.15.3",
"babel-eslint": "^10.1.0"
}
}
Binary file added page.pdf
Binary file not shown.
52 changes: 52 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import fs from 'fs';
import { getDocument } from 'pdfjs-dist/legacy/build/pdf.js';
import { createCanvas } from 'canvas';

const filename = 'page.pdf';

class NodeCanvasFactory
{
create(width, height) {
const canvas = createCanvas(width, height);
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;
}
}

async function bootstrap() {
const doc = await getDocument({
disableFontFace: true,
fontExtraProperties: true,
useSystemFonts: true,
data: fs.readFileSync(filename)
}).promise;
const page = await doc.getPage(1);

const canvasFactory = new NodeCanvasFactory();

const viewport = page.getViewport({ scale: 1 });
const ctx = canvasFactory.create(viewport.width, viewport.height);

await page.render({
canvasContext: ctx.context,
viewport,
canvasFactory
});
}

bootstrap();

0 comments on commit 23d0684

Please sign in to comment.