unpdf 1.0.5 → 1.1.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 CHANGED
@@ -10,7 +10,7 @@ This library is also intended as a modern alternative to the unmaintained but st
10
10
 
11
11
  - 🏗️ Made for Node.js, browser and serverless environments
12
12
  - 🪭 Includes serverless build of PDF.js ([`unpdf/pdfjs`](./package.json#L34))
13
- - 💬 Extract [text](#extract-text-from-pdf) and [images](#extractimages) from PDF files
13
+ - 💬 Extract [text](#extract-text-from-pdf), [links](#extractlinks), and [images](#extractimages) from PDF files
14
14
  - 🧠 Perfect for AI applications and PDF summarization
15
15
  - 🧱 Opt-in to legacy PDF.js build
16
16
  - 💨 Zero dependencies
@@ -169,6 +169,39 @@ function extractText(
169
169
  }>
170
170
  ```
171
171
 
172
+ ### `extractLinks`
173
+
174
+ Extracts all links from a PDF document, including hyperlinks and external URLs.
175
+
176
+ **Type Declaration**
177
+
178
+ ```ts
179
+ function extractLinks(
180
+ data: DocumentInitParameters['data'] | PDFDocumentProxy,
181
+ ): Promise<{
182
+ totalPages: number
183
+ links: string[]
184
+ }>
185
+ ```
186
+
187
+ **Example**
188
+
189
+ ```ts
190
+ import { readFile } from 'node:fs/promises'
191
+ import { extractLinks, getDocumentProxy } from 'unpdf'
192
+
193
+ // Load a PDF file
194
+ const buffer = await readFile('./document.pdf')
195
+ const pdf = await getDocumentProxy(new Uint8Array(buffer))
196
+
197
+ // Extract all links from the PDF
198
+ const { totalPages, links } = await extractLinks(pdf)
199
+
200
+ console.log(`Total pages: ${totalPages}`)
201
+ console.log(`Found ${links.length} links:`)
202
+ for (const link of links) console.log(link)
203
+ ```
204
+
172
205
  ### `extractImages`
173
206
 
174
207
  Extracts images from a specific page of a PDF document, including necessary metadata such as width, height, and calculated color channels.
package/dist/index.cjs CHANGED
@@ -145,7 +145,7 @@ async function extractImages$1(data, pageNumber) {
145
145
  continue;
146
146
  }
147
147
  const imageKey = operatorList.argsArray[i][0];
148
- const image = imageKey.startsWith("g_") ? await page.commonObjs.get(imageKey) : await page.objs.get(imageKey);
148
+ const image = imageKey.startsWith("g_") ? await new Promise((resolve) => page.commonObjs.get(imageKey, (resolvedImage) => resolve(resolvedImage))) : await new Promise((resolve) => page.objs.get(imageKey, (resolvedImage) => resolve(resolvedImage)));
149
149
  if (!image || !image.data || !image.width || !image.height) {
150
150
  continue;
151
151
  }
@@ -203,6 +203,28 @@ async function createIsomorphicCanvasFactory(canvasImport) {
203
203
  throw new Error("Unsupported environment for canvas creation.");
204
204
  }
205
205
 
206
+ async function extractLinks$1(data) {
207
+ const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
208
+ const pageLinks = await Promise.all(
209
+ Array.from({ length: pdf.numPages }, (_, i) => getPageLinks(pdf, i + 1))
210
+ );
211
+ return {
212
+ totalPages: pdf.numPages,
213
+ links: pageLinks.flat()
214
+ };
215
+ }
216
+ async function getPageLinks(document, pageNumber) {
217
+ const page = await document.getPage(pageNumber);
218
+ const annotations = await page.getAnnotations();
219
+ const links = [];
220
+ for (const annotation of annotations) {
221
+ if (annotation.subtype === "Link" && annotation.url) {
222
+ links.push(annotation.url);
223
+ }
224
+ }
225
+ return links;
226
+ }
227
+
206
228
  async function getMeta$1(data) {
207
229
  const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
208
230
  const meta = await pdf.getMetadata();
@@ -252,10 +274,15 @@ const renderPageAsImage = async (...args) => {
252
274
  await resolvePDFJSImport();
253
275
  return await renderPageAsImage$1(...args);
254
276
  };
277
+ const extractLinks = async (...args) => {
278
+ await resolvePDFJSImport();
279
+ return await extractLinks$1(...args);
280
+ };
255
281
 
256
282
  exports.configureUnPDF = configureUnPDF;
257
283
  exports.definePDFJSModule = definePDFJSModule;
258
284
  exports.extractImages = extractImages;
285
+ exports.extractLinks = extractLinks;
259
286
  exports.extractText = extractText;
260
287
  exports.getDocumentProxy = getDocumentProxy;
261
288
  exports.getMeta = getMeta;
package/dist/index.d.cts CHANGED
@@ -34,6 +34,11 @@ declare function renderPageAsImage$1(data: DocumentInitParameters['data'] | PDFD
34
34
  height?: number;
35
35
  }): Promise<ArrayBuffer>;
36
36
 
37
+ declare function extractLinks$1(data: DocumentInitParameters['data'] | PDFDocumentProxy): Promise<{
38
+ totalPages: number;
39
+ links: string[];
40
+ }>;
41
+
37
42
  declare function getMeta$1(data: DocumentInitParameters['data'] | PDFDocumentProxy): Promise<{
38
43
  info: Record<string, any>;
39
44
  metadata: Record<string, any>;
@@ -86,5 +91,6 @@ declare const getMeta: typeof getMeta$1;
86
91
  declare const extractText: typeof extractText$1;
87
92
  declare const extractImages: typeof extractImages$1;
88
93
  declare const renderPageAsImage: typeof renderPageAsImage$1;
94
+ declare const extractLinks: typeof extractLinks$1;
89
95
 
90
- export { configureUnPDF, definePDFJSModule, extractImages, extractText, getDocumentProxy, getMeta, getResolvedPDFJS, renderPageAsImage, resolvePDFJSImport };
96
+ export { configureUnPDF, definePDFJSModule, extractImages, extractLinks, extractText, getDocumentProxy, getMeta, getResolvedPDFJS, renderPageAsImage, resolvePDFJSImport };
package/dist/index.d.mts CHANGED
@@ -34,6 +34,11 @@ declare function renderPageAsImage$1(data: DocumentInitParameters['data'] | PDFD
34
34
  height?: number;
35
35
  }): Promise<ArrayBuffer>;
36
36
 
37
+ declare function extractLinks$1(data: DocumentInitParameters['data'] | PDFDocumentProxy): Promise<{
38
+ totalPages: number;
39
+ links: string[];
40
+ }>;
41
+
37
42
  declare function getMeta$1(data: DocumentInitParameters['data'] | PDFDocumentProxy): Promise<{
38
43
  info: Record<string, any>;
39
44
  metadata: Record<string, any>;
@@ -86,5 +91,6 @@ declare const getMeta: typeof getMeta$1;
86
91
  declare const extractText: typeof extractText$1;
87
92
  declare const extractImages: typeof extractImages$1;
88
93
  declare const renderPageAsImage: typeof renderPageAsImage$1;
94
+ declare const extractLinks: typeof extractLinks$1;
89
95
 
90
- export { configureUnPDF, definePDFJSModule, extractImages, extractText, getDocumentProxy, getMeta, getResolvedPDFJS, renderPageAsImage, resolvePDFJSImport };
96
+ export { configureUnPDF, definePDFJSModule, extractImages, extractLinks, extractText, getDocumentProxy, getMeta, getResolvedPDFJS, renderPageAsImage, resolvePDFJSImport };
package/dist/index.d.ts CHANGED
@@ -34,6 +34,11 @@ declare function renderPageAsImage$1(data: DocumentInitParameters['data'] | PDFD
34
34
  height?: number;
35
35
  }): Promise<ArrayBuffer>;
36
36
 
37
+ declare function extractLinks$1(data: DocumentInitParameters['data'] | PDFDocumentProxy): Promise<{
38
+ totalPages: number;
39
+ links: string[];
40
+ }>;
41
+
37
42
  declare function getMeta$1(data: DocumentInitParameters['data'] | PDFDocumentProxy): Promise<{
38
43
  info: Record<string, any>;
39
44
  metadata: Record<string, any>;
@@ -86,5 +91,6 @@ declare const getMeta: typeof getMeta$1;
86
91
  declare const extractText: typeof extractText$1;
87
92
  declare const extractImages: typeof extractImages$1;
88
93
  declare const renderPageAsImage: typeof renderPageAsImage$1;
94
+ declare const extractLinks: typeof extractLinks$1;
89
95
 
90
- export { configureUnPDF, definePDFJSModule, extractImages, extractText, getDocumentProxy, getMeta, getResolvedPDFJS, renderPageAsImage, resolvePDFJSImport };
96
+ export { configureUnPDF, definePDFJSModule, extractImages, extractLinks, extractText, getDocumentProxy, getMeta, getResolvedPDFJS, renderPageAsImage, resolvePDFJSImport };
package/dist/index.mjs CHANGED
@@ -143,7 +143,7 @@ async function extractImages$1(data, pageNumber) {
143
143
  continue;
144
144
  }
145
145
  const imageKey = operatorList.argsArray[i][0];
146
- const image = imageKey.startsWith("g_") ? await page.commonObjs.get(imageKey) : await page.objs.get(imageKey);
146
+ const image = imageKey.startsWith("g_") ? await new Promise((resolve) => page.commonObjs.get(imageKey, (resolvedImage) => resolve(resolvedImage))) : await new Promise((resolve) => page.objs.get(imageKey, (resolvedImage) => resolve(resolvedImage)));
147
147
  if (!image || !image.data || !image.width || !image.height) {
148
148
  continue;
149
149
  }
@@ -201,6 +201,28 @@ async function createIsomorphicCanvasFactory(canvasImport) {
201
201
  throw new Error("Unsupported environment for canvas creation.");
202
202
  }
203
203
 
204
+ async function extractLinks$1(data) {
205
+ const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
206
+ const pageLinks = await Promise.all(
207
+ Array.from({ length: pdf.numPages }, (_, i) => getPageLinks(pdf, i + 1))
208
+ );
209
+ return {
210
+ totalPages: pdf.numPages,
211
+ links: pageLinks.flat()
212
+ };
213
+ }
214
+ async function getPageLinks(document, pageNumber) {
215
+ const page = await document.getPage(pageNumber);
216
+ const annotations = await page.getAnnotations();
217
+ const links = [];
218
+ for (const annotation of annotations) {
219
+ if (annotation.subtype === "Link" && annotation.url) {
220
+ links.push(annotation.url);
221
+ }
222
+ }
223
+ return links;
224
+ }
225
+
204
226
  async function getMeta$1(data) {
205
227
  const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
206
228
  const meta = await pdf.getMetadata();
@@ -250,5 +272,9 @@ const renderPageAsImage = async (...args) => {
250
272
  await resolvePDFJSImport();
251
273
  return await renderPageAsImage$1(...args);
252
274
  };
275
+ const extractLinks = async (...args) => {
276
+ await resolvePDFJSImport();
277
+ return await extractLinks$1(...args);
278
+ };
253
279
 
254
- export { configureUnPDF, definePDFJSModule, extractImages, extractText, getDocumentProxy, getMeta, getResolvedPDFJS, renderPageAsImage, resolvePDFJSImport };
280
+ export { configureUnPDF, definePDFJSModule, extractImages, extractLinks, extractText, getDocumentProxy, getMeta, getResolvedPDFJS, renderPageAsImage, resolvePDFJSImport };