-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdfextract.js
More file actions
21 lines (18 loc) · 785 Bytes
/
Copy pathpdfextract.js
File metadata and controls
21 lines (18 loc) · 785 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
const fs = require("fs");
const path = require("path")
const PdfParse = require("pdf-parse");
const { text } = require("stream/consumers");
// import PdfParse from "pdf-parse";
async function extractTextFromPDF(pdfPath) {
try {
const dataBuffer = fs.readFileSync(pdfPath); // Read the file as a buffer
const data = await PdfParse(dataBuffer); // Extract text from PDF
return data.text.replace(/\s+/g, ' ').trim(); // Return text as a single string
} catch (error) {
console.error("Error reading PDF:", error);
return null;
}
}
// const pdfPath = path.join(__dirname, "uploads", "sample.pdf");
// extractTextFromPDF(pdfPath).then(text=>console.log(text));
module.exports.extractTextFromPDF=extractTextFromPDF