Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const pagesDir = path.join(ctx.baseDir, PAGES_SUBDIRECTORY);
const output = path.join(pagesDir, TXT_CONTENT_FILENAME);
const data = new Uint8Array(fs.readFileSync(pdfPath));
try {
// Create a directory where we can store the files
await fsMakeDir(pagesDir);
// Will be using promises to load document, pages and misc data instead of
// callback.
const loadedPDFDocument = pdfjsLib.getDocument({
data,
// Try to export JPEG images directly if they don't need any further
// processing.
nativeImageDecoderSupport: pdfjsLib.NativeImageDecoding.NONE
});
const doc = await loadedPDFDocument.promise;
const { numPages } = doc;
ctx.addPreview(output, 'txt');
ctx.addPreviewMetadata('pageCount', numPages);
await processAllPages(ctx, pagesDir, numPages, doc);
await fsWriteFile(output, pdfContents.join(' '));
_generateThumbnail(ctx, pdfPath, pagesDir, callback);
} catch (error) {
const errorMessage = 'Unable to process PDF';
log().error({ error }, errorMessage);
return callback({ code: 500, msg: errorMessage });
parseFromFileBuffer(pdfBuffer) {
// Read the PDF file into a typed array so PDF.js can load it.
let rawData = new Uint8Array(pdfBuffer);
domFacade.setGlobalDom();
return PDFJSLib.getDocument({
data: rawData,
// Try to export JPEG images directly if they don't need any further processing.
nativeImageDecoderSupport: PDFJSLib.NativeImageDecoding.DISPLAY
}).then((doc) => this.parseDocument(doc))
.then(() => this.metaDataHandler);
}
isValidAutoLabel = true;
break;
}
}
if (isValidAutoLabel) {
automobiles.push(extractedImages[imageIndex]);
}
resolve(data);
}
});
});
}
let loadingTask = pdfjsLib.getDocument({
data: rawImageFile,
nativeImageDecoderSupport: pdfjsLib.NativeImageDecoding.DISPLAY,
});
loadingTask.promise.then(function (doc) {
let numPages = doc.numPages;
console.log('# Document Loaded');
console.log(`Number of Pages: ${numPages}`);
let lastPromise = Promise.resolve();
let loadPage = function (pageNum) {
return doc.getPage(pageNum).then(page => {
console.log(`# Page ${pageNum}`);
return page.getOperatorList().then(opList => {
let svgGfx = new pdfjsLib.SVGGraphics(page.commonObjs, page.objs);
svgGfx.embedFonts = true;
for (let i = 0; i < opList.fnArray.length; i++) {