Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function parseExamPdf(fileData) {
const pdfjs = require('pdfjs-dist');
const moment = require('moment');
const _ = require('lodash');
const fileArray = new Uint8Array(fileData);
return pdfjs.getDocument(fileArray)
.then((pdfDocument) => {
// get all the pages from pdf
const numPages = pdfDocument.numPages;
return Promise.all(_.range(1, numPages + 1).map((pageNum) => {
return pdfDocument.getPage(pageNum);
}));
})
.then((pages) => {
// get text content items from all pages
return Promise.all(pages.map((page) => {
return page.getTextContent().then((content) => {
// remove headers and page number
return content.items.slice(17, -1);
});
}));
})
private open (params) {
let url = params.url
let self = this
this.setTitleUsingUrl(url)
// Loading document.
let loadingTask = pdfjsLib.getDocument({
url: url,
withCredentials: true,
maxImageSize: MAX_IMAGE_SIZE,
cMapPacked: CMAP_PACKED
})
this.pdfLoadingTask = loadingTask
loadingTask.onProgress = function (progressData) {
self.progress(progressData.loaded / progressData.total)
}
return loadingTask.promise.then(function (pdfDocument) {
// Document loaded, specifying document for the viewer.
self.pdfDocument = pdfDocument;
self.pdfViewer.setDocument(pdfDocument)
self.pdfLinkService.setDocument(pdfDocument)
const previewPDF = async function(ctx, pdfPath, callback) {
domStubs.setStubs(global);
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);
async mounted() {
const loadingTask = await getDocument(this.src).promise
const page = await loadingTask.getPage(1)
const {pdfCanvas: canvas, textLayer: textLayerDiv} = this.$refs
const scale = this.dim[0] / page.getViewport({scale: 1.0}).width
// Get viewport (dimensions)
const viewport = page.getViewport({scale})
const {width, height} = viewport
// Fetch canvas' 2d context
const context = canvas.getContext('2d')
// Prepare object needed by render method
const renderContext = {
this.setState((prevState) => {
if (!prevState.pdf) {
return null;
}
return { pdf: null };
});
const { options, onLoadProgress, onPassword } = this.props;
try {
// If another loading is in progress, let's cancel it
cancelRunningTask(this.runningTask);
const loadingTask = pdfjs.getDocument({ ...source, ...options });
loadingTask.onPassword = onPassword;
if (onLoadProgress) {
loadingTask.onProgress = onLoadProgress;
}
const cancellable = makeCancellable(loadingTask.promise);
this.runningTask = cancellable;
const pdf = await cancellable.promise;
this.setState((prevState) => {
if (prevState.pdf && prevState.pdf.fingerprint === pdf.fingerprint) {
return null;
}
return { pdf };
}, this.onLoadSuccess);
} catch (error) {
if (onDocumentComplete) {
this.props.onDocumentComplete(pdf.numPages);
}
}
this.setState( { totalPage: pdf.numPages });
this.setState({ pdf }, () => {
if (showAllPage) {
this.renderAllPage();
} else {
this.renderPage(dom, null);
}
});
});
} else {
// loaded the base64
const loadingTask = pdfjsLib.getDocument({data});
loadingTask.promise.then((pdf) => {
// is exit onDocumentComplete or not
if(!showAllPage){
if (onDocumentComplete) {
this.props.onDocumentComplete(pdf.numPages);
}
}
this.setState({ pdf }, () => {
if (showAllPage) {
this.renderAllPage();
} else {
this.renderPage(dom, null);
}
});
});
}
export default async function getPdfFingerprint(pdfURL) {
const PDFJS = require('pdfjs-dist')
try {
const pdf = await PDFJS.getDocument(pdfURL)
return pdf.fingerprint
} catch (err) {
throw err
}
}
private fn() {
PDFJS.getDocument(this._src).then((pdf: any) => {
this._pdf = pdf;
if (!this.isValidPageNumber(this._page)) {
this._page = 1;
}
if (!this._showAll) {
return this.renderPage(this._page);
}
return this.renderMultiplePages();
});
}
loadPDF () {
this.loadingTask = PDFjs.getDocument({ data: this.props.content }).then((pdf) => {
if (this.isUnmounting) return
this.setState({ pdf })
})
}