-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-pdf-extract.js
More file actions
119 lines (109 loc) · 2.17 KB
/
Copy pathtest-pdf-extract.js
File metadata and controls
119 lines (109 loc) · 2.17 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
const fs = require('fs')
const path = require('path')
const os = require('os')
async function testPdfExtract() {
try {
console.log('Testing pdf-extract library...')
// Create a simple test PDF file
const testPdfPath = path.join(os.tmpdir(), 'test.pdf')
console.log('Creating test PDF at:', testPdfPath)
// Write a simple PDF file (this is a minimal valid PDF)
const pdfContent = `%PDF-1.4
1 0 obj
<<
/Type /Catalog
/Pages 2 0 R
>>
endobj
2 0 obj
<<
/Type /Pages
/Kids [3 0 R]
/Count 1
>>
endobj
3 0 obj
<<
/Type /Page
/Parent 2 0 R
/MediaBox [0 0 612 792]
/Contents 4 0 R
/Resources <<
/Font <<
/F1 5 0 R
>>
>>
>>
endobj
4 0 obj
<<
/Length 44
>>
stream
BT
/F1 12 Tf
72 720 Td
(Hello World) Tj
ET
endstream
endobj
5 0 obj
<<
/Type /Font
/Subtype /Type1
/BaseFont /Helvetica
>>
endobj
xref
0 6
0000000000 65535 f
0000000010 00000 n
0000000053 00000 n
0000000163 00000 n
0000000340 00000 n
0000000430 00000 n
trailer
<<
/Size 6
/Root 1 0 R
>>
startxref
538
%%EOF`
fs.writeFileSync(testPdfPath, pdfContent)
console.log('Test PDF created successfully')
// Try to import pdf-extract
console.log('Importing pdf-extract...')
const pdfExtract = (await import('pdf-extract')).default
console.log('pdf-extract imported successfully')
// Try to extract text from the PDF with the correct options
console.log('Extracting text from PDF...')
const extracted = await new Promise((resolve, reject) => {
pdfExtract(testPdfPath, { type: "text" }, (err, data) => {
if (err) {
console.error('PDF extraction error:', err)
reject(err)
} else {
console.log('PDF extraction successful')
resolve(data)
}
})
})
console.log('Extracted text:', extracted.text)
console.log('PDF extract test passed')
// Clean up
fs.unlinkSync(testPdfPath)
console.log('Test PDF cleaned up')
return true
} catch (error) {
console.error('Error testing pdf-extract:', error)
return false
}
}
testPdfExtract().then(success => {
if (success) {
console.log('PDF extract test passed')
} else {
console.log('PDF extract test failed')
}
})