-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdf-processor.js
More file actions
174 lines (141 loc) · 5.69 KB
/
pdf-processor.js
File metadata and controls
174 lines (141 loc) · 5.69 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// PDF processing module using PDF.js
// This module handles PDF text extraction for the Resume Matcher extension
class PDFProcessor {
constructor() {
this.pdfjsLib = null;
this.initialized = false;
}
// Initialize PDF.js library
async init() {
if (this.initialized) return;
try {
// Load PDF.js library
// PDF.js is now loaded directly in popup.html, so it should be available globally
if (typeof window !== 'undefined' && window.pdfjsLib) {
this.pdfjsLib = window.pdfjsLib;
} else {
throw new Error('PDF.js library not found. Ensure it is loaded correctly.');
}
// Set worker source
this.pdfjsLib.GlobalWorkerOptions.workerSrc = chrome.runtime.getURL("lib/pdf.worker.min.js");
this.initialized = true;
console.log('PDF.js initialized successfully');
} catch (error) {
console.error('Failed to initialize PDF.js:', error);
throw new Error('Failed to initialize PDF processor');
}
}
// Extract text from PDF file
async extractText(file) {
if (!this.initialized) {
await this.init();
}
try {
// Convert file to ArrayBuffer
const arrayBuffer = await this.fileToArrayBuffer(file);
// Load PDF document
const pdf = await this.pdfjsLib.getDocument({ data: arrayBuffer }).promise;
console.log(`PDF loaded: ${pdf.numPages} pages`);
let fullText = '';
// Extract text from each page
for (let pageNum = 1; pageNum <= pdf.numPages; pageNum++) {
try {
const page = await pdf.getPage(pageNum);
const textContent = await page.getTextContent();
// Combine text items
const pageText = textContent.items
.map(item => item.str)
.join(' ');
fullText += pageText + '\n';
console.log(`Extracted text from page ${pageNum}: ${pageText.length} characters`);
} catch (pageError) {
console.warn(`Error extracting text from page ${pageNum}:`, pageError);
// Continue with other pages
}
}
// Clean up the extracted text
const cleanedText = this.cleanText(fullText);
if (cleanedText.length < 100) {
throw new Error('Extracted text is too short. The PDF might be image-based or corrupted.');
}
console.log(`Total extracted text: ${cleanedText.length} characters`);
return cleanedText;
} catch (error) {
console.error('Error extracting text from PDF:', error);
// Provide more specific error messages
if (error.name === 'InvalidPDFException') {
throw new Error('Invalid PDF file. Please make sure the file is a valid PDF.');
} else if (error.name === 'MissingPDFException') {
throw new Error('PDF file appears to be corrupted or empty.');
} else if (error.name === 'UnexpectedResponseException') {
throw new Error('Unable to read PDF file. It might be password-protected or corrupted.');
} else {
throw new Error(`Failed to extract text from PDF: ${error.message}`);
}
}
}
// Convert File object to ArrayBuffer
fileToArrayBuffer(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = function(e) {
resolve(e.target.result);
};
reader.onerror = function() {
reject(new Error('Failed to read file'));
};
reader.readAsArrayBuffer(file);
});
}
// Clean and normalize extracted text
cleanText(text) {
return text
// Remove excessive whitespace
.replace(/\s+/g, ' ')
// Remove control characters
.replace(/[\x00-\x1F\x7F]/g, ' ')
// Remove multiple consecutive spaces
.replace(/ {2,}/g, ' ')
// Trim whitespace
.trim();
}
// Validate PDF file
validateFile(file) {
const errors = [];
if (!file) {
errors.push('No file provided');
} else {
if (file.type !== 'application/pdf') {
errors.push('File must be a PDF');
}
if (file.size === 0) {
errors.push('File is empty');
}
if (file.size > 10 * 1024 * 1024) { // 10MB limit
errors.push('File size must be less than 10MB');
}
}
return {
isValid: errors.length === 0,
errors: errors
};
}
// Get file info
getFileInfo(file) {
return {
name: file.name,
size: file.size,
type: file.type,
lastModified: new Date(file.lastModified)
};
}
}
// Create global instance
const pdfProcessor = new PDFProcessor();
// Export for use in other scripts
if (typeof module !== 'undefined' && module.exports) {
module.exports = PDFProcessor;
} else if (typeof window !== 'undefined') {
window.PDFProcessor = PDFProcessor;
window.pdfProcessor = pdfProcessor;
}