pdf-decomposer
Version:
📚 Extract text and images (as buffers) from PDF files using native parsing and OCR with Tesseract.
29 lines (28 loc) • 1.13 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractFromPDF = extractFromPDF;
const os_1 = __importDefault(require("os"));
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
const crypto_1 = require("crypto");
const pdfToPng_1 = require("./pdfToPng");
/**
* Extract all from pdf
* @param buffer file buffer
*/
async function extractFromPDF(buffer) {
// Work in temp folder
const tmpDir = path_1.default.join(os_1.default.tmpdir(), 'pdf-decomposer');
// Clean old parsing
if (fs_1.default.existsSync(tmpDir)) {
fs_1.default.rmSync(tmpDir, { recursive: true });
}
fs_1.default.mkdirSync(tmpDir);
const tmpFilePath = path_1.default.join(tmpDir, `${(0, crypto_1.randomUUID)()}.pdf`);
fs_1.default.writeFileSync(tmpFilePath, buffer);
const pages = await (0, pdfToPng_1.pdfToPng)(tmpFilePath);
console.log(`Successfully convert ${pages.length} pdf pages`);
}