pdfjs-reader-core 0.2.15 → 0.2.16

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.
Files changed (2) hide show
  1. package/package.json +4 -2
  2. package/runkit-example.js +91 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pdfjs-reader-core",
3
- "version": "0.2.15",
3
+ "version": "0.2.16",
4
4
  "description": "A Next.js-compatible PDF renderer with canvas rendering, annotations, and search",
5
5
  "author": "suhas <suhasrdev@gmail.com>",
6
6
  "license": "MIT",
@@ -12,6 +12,7 @@
12
12
  "bugs": {
13
13
  "url": "https://github.com/suhasTeju/pdf-reader-js/issues"
14
14
  },
15
+ "runkitExampleFilename": "runkit-example.js",
15
16
  "type": "module",
16
17
  "main": "./dist/index.cjs",
17
18
  "module": "./dist/index.js",
@@ -30,7 +31,8 @@
30
31
  "./styles.css": "./dist/styles.css"
31
32
  },
32
33
  "files": [
33
- "dist"
34
+ "dist",
35
+ "runkit-example.js"
34
36
  ],
35
37
  "sideEffects": [
36
38
  "**/*.css"
@@ -0,0 +1,91 @@
1
+ /**
2
+ * pdfjs-reader-core - RunKit Example
3
+ *
4
+ * Note: This package is primarily a React component library for rendering PDFs.
5
+ * The full viewer requires a browser environment with React.
6
+ *
7
+ * This example demonstrates the headless/utility APIs that work in Node.js:
8
+ * - Loading PDF documents
9
+ * - Extracting text content
10
+ * - Getting document metadata
11
+ * - Getting document outline (table of contents)
12
+ *
13
+ * For the full React viewer, see the documentation:
14
+ * https://github.com/suhasTeju/pdf-reader-js
15
+ */
16
+
17
+ const { getDocument } = require('pdfjs-dist');
18
+
19
+ // Sample PDF URL (a simple public PDF)
20
+ const PDF_URL = 'https://raw.githubusercontent.com/niconiahi/puppeteer-pdf/main/sample.pdf';
21
+
22
+ async function extractPDFInfo() {
23
+ console.log('Loading PDF from:', PDF_URL);
24
+ console.log('---');
25
+
26
+ try {
27
+ // Load the PDF document
28
+ const loadingTask = getDocument(PDF_URL);
29
+ const pdf = await loadingTask.promise;
30
+
31
+ console.log('PDF loaded successfully!');
32
+ console.log('Number of pages:', pdf.numPages);
33
+ console.log('---');
34
+
35
+ // Get metadata
36
+ const metadata = await pdf.getMetadata();
37
+ console.log('Document Info:');
38
+ if (metadata.info) {
39
+ console.log(' Title:', metadata.info.Title || 'N/A');
40
+ console.log(' Author:', metadata.info.Author || 'N/A');
41
+ console.log(' Creator:', metadata.info.Creator || 'N/A');
42
+ console.log(' Producer:', metadata.info.Producer || 'N/A');
43
+ }
44
+ console.log('---');
45
+
46
+ // Extract text from first page
47
+ console.log('Extracting text from page 1...');
48
+ const page = await pdf.getPage(1);
49
+ const textContent = await page.getTextContent();
50
+
51
+ const text = textContent.items
52
+ .filter(item => 'str' in item)
53
+ .map(item => item.str)
54
+ .join(' ')
55
+ .substring(0, 500); // First 500 chars
56
+
57
+ console.log('Page 1 text (first 500 chars):');
58
+ console.log(text + (text.length >= 500 ? '...' : ''));
59
+ console.log('---');
60
+
61
+ // Get outline (table of contents) if available
62
+ const outline = await pdf.getOutline();
63
+ if (outline && outline.length > 0) {
64
+ console.log('Document Outline:');
65
+ outline.slice(0, 5).forEach((item, i) => {
66
+ console.log(` ${i + 1}. ${item.title}`);
67
+ });
68
+ if (outline.length > 5) {
69
+ console.log(` ... and ${outline.length - 5} more items`);
70
+ }
71
+ } else {
72
+ console.log('No outline/table of contents found.');
73
+ }
74
+
75
+ console.log('---');
76
+ console.log('For the full React PDF viewer component, install the package:');
77
+ console.log(' npm install pdfjs-reader-core');
78
+ console.log('');
79
+ console.log('Then use in your React app:');
80
+ console.log(' import { PDFViewerClient } from "pdfjs-reader-core";');
81
+ console.log(' import "pdfjs-reader-core/styles.css";');
82
+
83
+ // Clean up
84
+ await pdf.destroy();
85
+
86
+ } catch (error) {
87
+ console.error('Error loading PDF:', error.message);
88
+ }
89
+ }
90
+
91
+ extractPDFInfo();