unpdf 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +65 -18
- package/dist/index.cjs +212 -0
- package/dist/{shared/unpdf.3991886a.d.cts → index.d.cts} +23 -9
- package/dist/{shared/unpdf.3991886a.d.mts → index.d.mts} +23 -9
- package/dist/{shared/unpdf.3991886a.d.ts → index.d.ts} +23 -9
- package/dist/index.mjs +202 -0
- package/dist/pdfjs.mjs +1 -1
- package/package.json +26 -22
- package/dist/index.node.cjs +0 -24
- package/dist/index.node.d.cts +0 -10
- package/dist/index.node.d.mts +0 -10
- package/dist/index.node.d.ts +0 -10
- package/dist/index.node.mjs +0 -17
- package/dist/index.web.cjs +0 -24
- package/dist/index.web.d.cts +0 -10
- package/dist/index.web.d.mts +0 -10
- package/dist/index.web.d.ts +0 -10
- package/dist/index.web.mjs +0 -17
- package/dist/index.worker.cjs +0 -24
- package/dist/index.worker.d.cts +0 -10
- package/dist/index.worker.d.mts +0 -10
- package/dist/index.worker.d.ts +0 -10
- package/dist/index.worker.mjs +0 -17
- package/dist/shared/unpdf.6c0ca5b5.cjs +0 -107
- package/dist/shared/unpdf.ce219a40.mjs +0 -99
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
let resolvedModule;
|
|
2
|
+
const isNode = globalThis.process?.release?.name === "node";
|
|
3
|
+
const isBrowser = typeof window !== "undefined";
|
|
4
|
+
async function getDocumentProxy(data, options = {}) {
|
|
5
|
+
const { getDocument } = await getResolvedPDFJS();
|
|
6
|
+
const pdf = await getDocument({
|
|
7
|
+
data,
|
|
8
|
+
isEvalSupported: false,
|
|
9
|
+
...options
|
|
10
|
+
}).promise;
|
|
11
|
+
return pdf;
|
|
12
|
+
}
|
|
13
|
+
async function getResolvedPDFJS() {
|
|
14
|
+
if (!resolvedModule) {
|
|
15
|
+
await resolvePDFJSImports();
|
|
16
|
+
}
|
|
17
|
+
return resolvedModule;
|
|
18
|
+
}
|
|
19
|
+
async function resolvePDFJSImports(pdfjsResolver, { force = false } = {}) {
|
|
20
|
+
if (resolvedModule && !force) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
if (pdfjsResolver) {
|
|
24
|
+
try {
|
|
25
|
+
const _import = await pdfjsResolver();
|
|
26
|
+
resolvedModule = _import.default || _import;
|
|
27
|
+
return;
|
|
28
|
+
} catch {
|
|
29
|
+
throw new Error(
|
|
30
|
+
"Resolving failed. Please check the provided configuration."
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
try {
|
|
35
|
+
resolvedModule = await import('unpdf/pdfjs');
|
|
36
|
+
} catch {
|
|
37
|
+
throw new Error(
|
|
38
|
+
"PDF.js is not available. Please add the package as a dependency."
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
function isPDFDocumentProxy(data) {
|
|
43
|
+
return typeof data === "object" && data !== null && "_pdfInfo" in data;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async function getMeta$1(data) {
|
|
47
|
+
const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
|
|
48
|
+
const meta = await pdf.getMetadata();
|
|
49
|
+
return {
|
|
50
|
+
info: meta?.info ?? {},
|
|
51
|
+
metadata: meta?.metadata?.getAll() ?? {}
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async function extractText$1(data, options = {}) {
|
|
56
|
+
const { mergePages = false } = { ...options };
|
|
57
|
+
const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
|
|
58
|
+
const texts = await Promise.all(
|
|
59
|
+
Array.from({ length: pdf.numPages }, (_, i) => getPageText(pdf, i + 1))
|
|
60
|
+
);
|
|
61
|
+
const filteredTexts = texts.filter(Boolean);
|
|
62
|
+
return {
|
|
63
|
+
totalPages: pdf.numPages,
|
|
64
|
+
text: mergePages ? filteredTexts.join("\n\n") : filteredTexts
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
async function getPageText(pdf, pageNumber) {
|
|
68
|
+
const page = await pdf.getPage(pageNumber);
|
|
69
|
+
const content = await page.getTextContent();
|
|
70
|
+
const items = content.items;
|
|
71
|
+
if (items.length === 0) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
return items.map(({ str }) => str).join("\n");
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async function extractImages$1(data, pageNumber) {
|
|
78
|
+
const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
|
|
79
|
+
const page = await pdf.getPage(pageNumber);
|
|
80
|
+
if (pageNumber < 1 || pageNumber > pdf.numPages) {
|
|
81
|
+
throw new Error(
|
|
82
|
+
`Invalid page number. Must be between 1 and ${pdf.numPages}.`
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
const operatorList = await page.getOperatorList();
|
|
86
|
+
const { OPS } = await getResolvedPDFJS();
|
|
87
|
+
const images = [];
|
|
88
|
+
for (let i = 0; i < operatorList.fnArray.length; i++) {
|
|
89
|
+
const op = operatorList.fnArray[i];
|
|
90
|
+
if (op !== OPS.paintImageXObject) {
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
const imageKey = operatorList.argsArray[i][0];
|
|
94
|
+
const image = await page.objs.get(imageKey);
|
|
95
|
+
images.push(image.data);
|
|
96
|
+
}
|
|
97
|
+
return images;
|
|
98
|
+
}
|
|
99
|
+
async function renderPageAsImage$1(data, pageNumber, options = {}) {
|
|
100
|
+
const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
|
|
101
|
+
const page = await pdf.getPage(pageNumber);
|
|
102
|
+
if (pageNumber < 1 || pageNumber > pdf.numPages) {
|
|
103
|
+
throw new Error(
|
|
104
|
+
`Invalid page number. Must be between 1 and ${pdf.numPages}.`
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
let outputScale = options.scale || 1;
|
|
108
|
+
let viewport = page.getViewport({ scale: outputScale });
|
|
109
|
+
if (options.width) {
|
|
110
|
+
outputScale = options.width / viewport.width;
|
|
111
|
+
} else if (options.height) {
|
|
112
|
+
outputScale = options.height / viewport.height;
|
|
113
|
+
}
|
|
114
|
+
if (outputScale !== 1 && outputScale > 0) {
|
|
115
|
+
viewport = page.getViewport({ scale: outputScale });
|
|
116
|
+
}
|
|
117
|
+
const canvasFactory = await createIsomorphicCanvasFactory();
|
|
118
|
+
const ctx = canvasFactory.create(viewport.width, viewport.height);
|
|
119
|
+
await page.render({
|
|
120
|
+
canvasContext: ctx.context,
|
|
121
|
+
viewport
|
|
122
|
+
}).promise;
|
|
123
|
+
const dataUrl = isBrowser ? ctx.canvas.toDataURL() : ctx.canvas.toDataURL();
|
|
124
|
+
const response = await fetch(dataUrl);
|
|
125
|
+
return await response.arrayBuffer();
|
|
126
|
+
}
|
|
127
|
+
async function createIsomorphicCanvasFactory() {
|
|
128
|
+
let _canvas;
|
|
129
|
+
if (isNode) {
|
|
130
|
+
try {
|
|
131
|
+
_canvas = await import('canvas');
|
|
132
|
+
} catch {
|
|
133
|
+
throw new Error(
|
|
134
|
+
'The "canvas" package is required when running in Node.js.'
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return {
|
|
139
|
+
_createCanvas(width, height) {
|
|
140
|
+
if (isBrowser) {
|
|
141
|
+
const canvas = document.createElement("canvas");
|
|
142
|
+
canvas.width = width;
|
|
143
|
+
canvas.height = height;
|
|
144
|
+
return canvas;
|
|
145
|
+
} else if (isNode) {
|
|
146
|
+
return _canvas.createCanvas(width, height);
|
|
147
|
+
}
|
|
148
|
+
throw new Error("Unsupported environment for canvas creation.");
|
|
149
|
+
},
|
|
150
|
+
create(width, height) {
|
|
151
|
+
const _canvas2 = this._createCanvas(width, height);
|
|
152
|
+
const context = _canvas2.getContext(
|
|
153
|
+
"2d"
|
|
154
|
+
);
|
|
155
|
+
return {
|
|
156
|
+
canvas: _canvas2,
|
|
157
|
+
context
|
|
158
|
+
};
|
|
159
|
+
},
|
|
160
|
+
reset(ctx, width, height) {
|
|
161
|
+
if (ctx.canvas) {
|
|
162
|
+
ctx.canvas.width = width;
|
|
163
|
+
ctx.canvas.height = height;
|
|
164
|
+
}
|
|
165
|
+
},
|
|
166
|
+
destroy(ctx) {
|
|
167
|
+
if (ctx.canvas) {
|
|
168
|
+
ctx.canvas.width = 0;
|
|
169
|
+
ctx.canvas.height = 0;
|
|
170
|
+
}
|
|
171
|
+
ctx.canvas = void 0;
|
|
172
|
+
ctx.context = void 0;
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
async function defineUnPDFConfig(options) {
|
|
178
|
+
const { pdfjs } = { ...options };
|
|
179
|
+
if (pdfjs) {
|
|
180
|
+
await resolvePDFJSImports(pdfjs, { force: true });
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const getMeta = async (...args) => {
|
|
185
|
+
await resolvePDFJSImports();
|
|
186
|
+
return await getMeta$1(...args);
|
|
187
|
+
};
|
|
188
|
+
const extractText = async (...args) => {
|
|
189
|
+
await resolvePDFJSImports();
|
|
190
|
+
return await extractText$1(...args);
|
|
191
|
+
};
|
|
192
|
+
const extractPDFText = extractText;
|
|
193
|
+
const extractImages = async (...args) => {
|
|
194
|
+
await resolvePDFJSImports();
|
|
195
|
+
return await extractImages$1(...args);
|
|
196
|
+
};
|
|
197
|
+
const renderPageAsImage = async (...args) => {
|
|
198
|
+
await resolvePDFJSImports();
|
|
199
|
+
return await renderPageAsImage$1(...args);
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
export { defineUnPDFConfig, extractImages, extractPDFText, extractText, getDocumentProxy, getMeta, getResolvedPDFJS, renderPageAsImage, resolvePDFJSImports };
|