forked from Moebits/waifu2x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdf-images.ts
96 lines (85 loc) · 3.31 KB
/
pdf-images.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import {createCanvas, loadImage} from "canvas"
import fs from "fs"
const pdfjs = require("pdfjs-dist/build/pdf.js")
const renderPage = async (pdfDocument: any, pageNumber: number, options?: any) => {
const page = await pdfDocument.getPage(pageNumber)
let viewport = page.getViewport({scale: 1.0})
let newScale = 1.0
if (options.width) {
newScale = options.width / viewport.width
} else if (options.height) {
newScale = options.height / viewport.height
}
if (newScale != 1 && newScale > 0) {
viewport = page.getViewport({scale: newScale})
}
const canvas = createCanvas(viewport.width, viewport.height)
const ctx = canvas.getContext("2d")
const canvasFactory = {
create: (width: number, height: number) => {
const canvas = createCanvas(width, height)
const context = canvas.getContext("2d")
return {canvas, context}
},
reset: (ctx: any, width: number, height: number) => {
ctx.canvas.width = width
ctx.canvas.height = height
},
destroy: (ctx: any) => {
ctx.canvas.width = 0
ctx.canvas.height = 0
ctx.canvas = null
ctx.context = null
}
}
await page.render({canvasContext: ctx, viewport, canvasFactory}).promise
let mime = "image/jpeg" as any
if (options.type === "png") mime = "image/png"
if (options.type === "webp") mime = "image/webp"
if (options.type === "avif") mime = "image/avif"
return ctx.canvas.toBuffer(mime)
}
export const pdfImages = async (pdf: string | Buffer | Uint8Array, options?: {width?: number, height?: number, base64?: boolean, pageNumbers?: number[], type?: "jpg" | "png" | "webp" | "avif"}) => {
if (!options) options = {}
let pdfData = null as any
if (typeof pdf === "string") {
if (pdf.startsWith("http") || pdf.startsWith("file://")) {
const arrayBuffer = await fetch(pdf).then((r) => r.arrayBuffer())
pdfData = new Uint8Array(arrayBuffer)
} else if (pdf.includes("base64")) {
pdfData = new Uint8Array(Buffer.from(pdf.split(",")[1], "base64"));
} else {
pdfData = new Uint8Array(fs.readFileSync(pdf))
}
} else if (Buffer.isBuffer(pdf)) {
pdfData = new Uint8Array(pdf)
} else {
pdfData = pdf
}
const pdfDocument = await pdfjs.getDocument({data: pdfData, disableFontFace: true, verbosity: 0}).promise
let outPages = []
if (options.pageNumbers) {
for (let i = 0; i < options.pageNumbers.length; i++) {
let currentPage = await renderPage(pdfDocument, options.pageNumbers[i], options)
if (currentPage) {
if (options.base64) {
outPages.push(currentPage.toString("base64"))
} else {
outPages.push(new Uint8Array(currentPage))
}
}
}
} else {
for (let i = 1; i <= pdfDocument.numPages; i++) {
let currentPage = await renderPage(pdfDocument, i, options)
if (currentPage) {
if (options.base64) {
outPages.push(currentPage.toString("base64"))
} else {
outPages.push(new Uint8Array(currentPage))
}
}
}
}
return outPages
}