unpdf 0.6.1 → 0.7.1

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/dist/index.mjs CHANGED
@@ -1,4 +1,6 @@
1
1
  let resolvedModule;
2
+ const isNode = globalThis.process?.release?.name === "node";
3
+ const isBrowser = typeof window !== "undefined";
2
4
  async function getDocumentProxy(data, options = {}) {
3
5
  const { getDocument } = await getResolvedPDFJS();
4
6
  const pdf = await getDocument({
@@ -8,12 +10,6 @@ async function getDocumentProxy(data, options = {}) {
8
10
  }).promise;
9
11
  return pdf;
10
12
  }
11
- async function defineUnPDFConfig(options) {
12
- const { pdfjs } = { ...options };
13
- if (pdfjs) {
14
- await resolvePDFJSImports(pdfjs, { force: true });
15
- }
16
- }
17
13
  async function getResolvedPDFJS() {
18
14
  if (!resolvedModule) {
19
15
  await resolvePDFJSImports();
@@ -47,7 +43,7 @@ function isPDFDocumentProxy(data) {
47
43
  return typeof data === "object" && data !== null && "_pdfInfo" in data;
48
44
  }
49
45
 
50
- async function getPDFMeta$1(data) {
46
+ async function getMeta$1(data) {
51
47
  const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
52
48
  const meta = await pdf.getMetadata();
53
49
  return {
@@ -56,7 +52,7 @@ async function getPDFMeta$1(data) {
56
52
  };
57
53
  }
58
54
 
59
- async function extractPDFText$1(data, options = {}) {
55
+ async function extractText$1(data, options = {}) {
60
56
  const { mergePages = false } = { ...options };
61
57
  const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
62
58
  const texts = await Promise.all(
@@ -78,35 +74,123 @@ async function getPageText(pdf, pageNumber) {
78
74
  return items.map(({ str }) => str).join("\n");
79
75
  }
80
76
 
81
- async function getImagesFromPage$1(data, pageNumber) {
77
+ async function extractImages$1(data, pageNumber) {
82
78
  const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
83
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
+ }
84
85
  const operatorList = await page.getOperatorList();
85
86
  const { OPS } = await getResolvedPDFJS();
86
87
  const images = [];
87
- for (const op of operatorList.fnArray) {
88
+ for (let i = 0; i < operatorList.fnArray.length; i++) {
89
+ const op = operatorList.fnArray[i];
88
90
  if (op !== OPS.paintImageXObject) {
89
91
  continue;
90
92
  }
91
- const image = await page.objs.get(operatorList.argsArray[op][0]);
92
- if (image.data) {
93
- images.push(image.data.buffer);
94
- }
93
+ const imageKey = operatorList.argsArray[i][0];
94
+ const image = await page.objs.get(imageKey);
95
+ images.push(image.data);
95
96
  }
96
97
  return images;
97
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(options.canvas);
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(canvas) {
128
+ const _canvas = await canvas?.();
129
+ return {
130
+ _createCanvas(width, height) {
131
+ if (isBrowser) {
132
+ const canvas2 = document.createElement("canvas");
133
+ canvas2.width = width;
134
+ canvas2.height = height;
135
+ return canvas2;
136
+ } else if (isNode) {
137
+ if (!_canvas) {
138
+ throw new Error('Failed to resolve "canvas" package.');
139
+ }
140
+ return _canvas.createCanvas(width, height);
141
+ }
142
+ throw new Error("Unsupported environment for canvas creation.");
143
+ },
144
+ create(width, height) {
145
+ const _canvas2 = this._createCanvas(width, height);
146
+ const context = _canvas2.getContext(
147
+ "2d"
148
+ );
149
+ return {
150
+ canvas: _canvas2,
151
+ context
152
+ };
153
+ },
154
+ reset(ctx, width, height) {
155
+ if (ctx.canvas) {
156
+ ctx.canvas.width = width;
157
+ ctx.canvas.height = height;
158
+ }
159
+ },
160
+ destroy(ctx) {
161
+ if (ctx.canvas) {
162
+ ctx.canvas.width = 0;
163
+ ctx.canvas.height = 0;
164
+ }
165
+ ctx.canvas = void 0;
166
+ ctx.context = void 0;
167
+ }
168
+ };
169
+ }
170
+
171
+ async function defineUnPDFConfig(options) {
172
+ const { pdfjs } = { ...options };
173
+ if (pdfjs) {
174
+ await resolvePDFJSImports(pdfjs, { force: true });
175
+ }
176
+ }
98
177
 
99
- const getPDFMeta = async (...args) => {
178
+ const getMeta = async (...args) => {
179
+ await resolvePDFJSImports();
180
+ return await getMeta$1(...args);
181
+ };
182
+ const extractText = async (...args) => {
100
183
  await resolvePDFJSImports();
101
- return await getPDFMeta$1(...args);
184
+ return await extractText$1(...args);
102
185
  };
103
- const extractPDFText = async (...args) => {
186
+ const extractPDFText = extractText;
187
+ const extractImages = async (...args) => {
104
188
  await resolvePDFJSImports();
105
- return await extractPDFText$1(...args);
189
+ return await extractImages$1(...args);
106
190
  };
107
- const getImagesFromPage = async (...args) => {
191
+ const renderPageAsImage = async (...args) => {
108
192
  await resolvePDFJSImports();
109
- return await getImagesFromPage$1(...args);
193
+ return await renderPageAsImage$1(...args);
110
194
  };
111
195
 
112
- export { defineUnPDFConfig, extractPDFText, getDocumentProxy, getImagesFromPage, getPDFMeta, getResolvedPDFJS, resolvePDFJSImports };
196
+ export { defineUnPDFConfig, extractImages, extractPDFText, extractText, getDocumentProxy, getMeta, getResolvedPDFJS, renderPageAsImage, resolvePDFJSImports };