-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Vitalii Savchuk
committed
Jun 25, 2022
0 parents
commit 23d0684
Showing
4 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }] | ||
] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |