UNPKG

@thecodingwhale/cv-processor

Version:

CV Processor to extract structured data from PDF resumes using TypeScript

107 lines (106 loc) 4.51 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.convertPdfToTexts = convertPdfToTexts; exports.convertPdfToImages = convertPdfToImages; const child_process_1 = require("child_process"); const fs = __importStar(require("fs")); const os = __importStar(require("os")); const path = __importStar(require("path")); const pdf_parse_1 = __importDefault(require("pdf-parse")); const util_1 = require("util"); const execAsync = (0, util_1.promisify)(child_process_1.exec); /** * Converts a PDF file to base64-encoded PNG images using pdftoppm. * Requires poppler-utils to be installed. * * @param pdfPath - The file path of the PDF to convert * @returns A promise that resolves to an array of base64 image data URLs */ async function convertPdfToImages(pdfPath) { console.log(`[convertPdfToImages] Creating temp directory for PDF images`); const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'pdf-images-')); console.log(`[convertPdfToImages] Temp directory created: ${tempDir}`); try { const command = `pdftoppm -png -r 200 "${pdfPath}" "${path.join(tempDir, 'page')}"`; console.log(`[convertPdfToImages] Executing command: ${command}`); await execAsync(command); const files = fs .readdirSync(tempDir) .filter((file) => file.endsWith('.png')); console.log(`[convertPdfToImages] Found ${files.length} image files: ${files.join(', ')}`); const sortedFiles = files.map((file) => path.join(tempDir, file)).sort(); console.log(`[convertPdfToImages] Sorted file paths: ${sortedFiles.join(', ')}`); const imageUrls = sortedFiles.map((file) => { const data = fs.readFileSync(file); const base64 = data.toString('base64'); console.log(`[convertPdfToImages] Converted image ${file}, size: ${base64.length} chars`); return `data:image/png;base64,${base64}`; }); console.log(`[convertPdfToImages] Returning ${imageUrls.length} base64 image URLs`); return imageUrls; } catch (error) { console.error('[convertPdfToImages] Error converting PDF to images:', error); throw error; } } /** * Convert PDF to text using pdf-parse * @param pdfPath Path to the PDF file * @returns Array of text content from each page */ async function convertPdfToTexts(pdfPath) { try { // Read the PDF file const dataBuffer = fs.readFileSync(pdfPath); // Parse the PDF const data = await (0, pdf_parse_1.default)(dataBuffer); // Split the text into pages // Note: pdf-parse doesn't provide direct page separation // We'll use a simple heuristic to split pages based on page numbers const pages = data.text .split(/\n\s*\d+\s*\n/) .filter((page) => page.trim().length > 0); return pages; } catch (error) { console.error('Error converting PDF to text:', error); throw error; } }