Download utils/fileParser.js from ismizo/CVBora: direct link, hf CLI and curl.
- Browser
- Download file 2.22 kB
-
https://huggingface.co/spaces/ismizo/CVBora/resolve/main/utils/fileParser.js
- Command line
-
hf download hf://spaces/ismizo/CVBora/utils/fileParser.js
-
curl -L -o fileParser.js https://huggingface.co/spaces/ismizo/CVBora/resolve/main/utils/fileParser.js
2.22 kB
| // Ensure the require statement for pdf-parse is correct | |
| const {PDFParse} = require('pdf-parse'); | |
| const mammoth = require('mammoth'); | |
| const fs = require('fs'); | |
| const path = require('path'); | |
| const os = require('os'); | |
| /** | |
| * Extracts text from a file buffer based on mimetype | |
| * @param {Buffer} buffer - The file data in memory | |
| * @param {String} mimetype - The type of file (pdf/docx) | |
| * @returns {Promise<String>} - The extracted text | |
| */ | |
| const extractText = async (buffer, mimetype) => { | |
| try { | |
| if (mimetype === 'application/pdf') { | |
| // Create a path in the system's temp directory | |
| const tempFilePath = path.join(os.tmpdir(), `upload_${Date.now()}.pdf`); | |
| // Write the buffer to the temp file | |
| fs.writeFileSync(tempFilePath, buffer); | |
| // Convert file path to a file:// URL | |
| const url = `file://${tempFilePath}`; | |
| const parser = new PDFParse({url}); | |
| //const data = await parser.getText(); | |
| const allImages = await parser.getScreenshot({ scale: 1.69 });; // Get images to trigger parsing and text extraction 9 | |
| fs.unlinkSync(tempFilePath); // Clean up temp file after processing | |
| const data = { init: true, pageImages:[]}; | |
| for (const page of allImages.pages) { | |
| //const buffer = Buffer.from(page.dataUrl, 'base64'); | |
| data.pageImages.push(page.dataUrl); // Store image data for each page | |
| } | |
| return data; | |
| } | |
| else if (mimetype === 'application/vnd.openxmlformats-officedocument.wordprocessingml.document') { // .docx | |
| const result = await mammoth.extractRawText({ buffer: buffer }); | |
| return result.value; | |
| } | |
| else if (mimetype === 'text/plain') { | |
| return buffer.toString('utf8'); | |
| } | |
| else { | |
| throw new Error('Unsupported file type'); | |
| } | |
| } catch (error) { | |
| console.error("Text Extraction Error:", error); | |
| throw new Error('Failed to parse file content'); | |
| } | |
| }; | |
| module.exports = { extractText }; | |