Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'pdfjs-dist' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
options = DEFAULT_OPTIONS;
}
if (typeof options.pagerender !== 'function') {
options.pagerender = DEFAULT_OPTIONS.pagerender;
}
if (typeof options.max !== 'number') {
options.max = DEFAULT_OPTIONS.max;
}
if (typeof options.version !== 'string') {
options.version = DEFAULT_OPTIONS.version;
}
if (options.version === 'default') {
options.version = DEFAULT_OPTIONS.version;
}
ret.version = PDFJS.version;
// Disable workers to avoid yet another cross-origin issue (workers need
// the URL of the script to be loaded, and dynamically loading a cross-origin
// script does not work).
PDFJS.disableWorker = true;
let doc;
return new Promise((resolve, reject) => {
PDFJS.getDocument(dataBuffer)
.promise.then(document => {
doc = document;
ret.numpages = doc.numPages;
let metaData;
doc.getMetadata()
if (typeof options.max !== 'number') {
options.max = DEFAULT_OPTIONS.max;
}
if (typeof options.version !== 'string') {
options.version = DEFAULT_OPTIONS.version;
}
if (options.version === 'default') {
options.version = DEFAULT_OPTIONS.version;
}
ret.version = PDFJS.version;
// Disable workers to avoid yet another cross-origin issue (workers need
// the URL of the script to be loaded, and dynamically loading a cross-origin
// script does not work).
PDFJS.disableWorker = true;
let doc;
return new Promise((resolve, reject) => {
PDFJS.getDocument(dataBuffer)
.promise.then(document => {
doc = document;
ret.numpages = doc.numPages;
let metaData;
doc.getMetadata()
.then(metadata => {
metaData = metadata;
ret.info = metaData ? metaData.info : undefined;
ret.metadata = metaData ? metaData.metadata : undefined;
init (timestamp) {
const t = this;
this.loadingTask = pdfJsLib.getDocument({
url: this.url,
cMapUrl: '../../node_modules/pdfjs-dist/cmaps/',
cMapPacked: true
});
this.loadingTask.promise.then(async function(pdf) {
console.time('PDF_Render')
console.log('PDF loaded');
var container = document.querySelector(`.pdf-wrap`);
// for(var i = 0, len = pdf.numPages; i < len; i++) {
// await t.renderPage(pdf, i + 1, container)
// }
console.log(pdf.numPages);
var pages = await Promise.all(
Array.apply(null, Array(pdf.numPages)).map((item, index) => {
console.log(item, index)
async function extractPDFFile(input, output) {
let pdf = await pdfjsLib.getDocument({
data: input.array
});
// pdf files can have embedded properties; extract those
const meta = await pdf.getMetadata();
if (meta.info) {
if (meta.info.Author) {
output.author = meta.info.Author;
}
if (meta.info.Title) {
output.datasetTitle = meta.info.Title;
}
if (meta.info.Keywords) {
output.keywords = meta.info.Keywords.split(/,\s+/g);
return pdfDocument.getMetadata().then(function (data) {
let info = data.info; let metadata = data.metadata;
self.documentInfo = info ;
self.metadata = metadata;
// Provides some basic debug information
console.log("PDF " + pdfDocument.fingerprint + " [" +
info.PDFFormatVersion + " " + (info.Producer || "-").trim() +
" / " + (info.Creator || "-").trim() + "]" +
" (PDF.js: " + (pdfjsLib.version || "-") + ")");
let pdfTitle;
if (metadata && metadata.has("dc:title")) {
let title = metadata.get("dc:title");
// Ghostscript sometimes returns 'Untitled', so prevent setting the
// title to 'Untitled.
if (title !== "Untitled") {
pdfTitle = title;
}
}
if (!pdfTitle && info && info["Title"]) {
pdfTitle = info["Title"];
}
if (pdfTitle) {
import * as React from 'react';
import {Logger} from 'polar-shared/src/logger/Logger';
import {URLStr} from "polar-shared/src/util/Strings";
import PDFJS from 'pdfjs-dist';
import {Numbers} from "polar-shared/src/util/Numbers";
console.log("Running with pdf.js version: " + PDFJS.version);
// TODO: I'm not sure this is the safest way to find the worker path.
PDFJS.GlobalWorkerOptions.workerSrc = '../../../node_modules/pdfjs-dist/build/pdf.worker.js';
const log = Logger.create();
console.log("FIXME: ", (PDFJS as any).renderTextLayer);
export class PDFViewer extends React.Component {
// https://mozilla.github.io/pdf.js/examples/
constructor(props: IProps, context: any) {
super(props, context);
this.doRender = this.doRender.bind(this);
const pdfjs = require('pdfjs-dist/build/pdf.js');
const Trie = require('./trie');
const pinyin = require('pinyin');
const MarkdownIt = require('markdown-it');
pdfjs.GlobalWorkerOptions.workerSrc = require.resolve('pdfjs-dist/build/pdf.worker.js');
/* Set scale = -1 to auto-scale */
function renderPDF(content, scale, elem, targetWidth) {
return pdfjs.getDocument(content).then(pdf => {
const promises = [];
for(let i = 1; i <= pdf.numPages; ++i)
promises.push(pdf.getPage(i).then(page => {
let _scale = scale;
if(_scale === -1) {
const svp = page.getViewport(1);
_scale = targetWidth / svp.width;
console.log(_scale);
}
const vp = page.getViewport(_scale);
}, function (exception) {
let message = exception && exception.message
let l10n = self.l10n
let loadingErrorMessage
if (exception instanceof pdfjsLib.InvalidPDFException) {
// change error message also for other builds
loadingErrorMessage = l10n.get('invalid_file_error', null,
'Invalid or corrupted PDF file.')
} else if (exception instanceof pdfjsLib.MissingPDFException) {
// special message for missing PDFs
loadingErrorMessage = l10n.get('missing_file_error', null,
'Missing PDF file.')
} else if (exception instanceof pdfjsLib.UnexpectedResponseException) {
loadingErrorMessage = l10n.get('unexpected_response_error', null,
'Unexpected server response.')
} else {
loadingErrorMessage = l10n.get('loading_error', null,
'An error occurred while loading the PDF.')
}
loadingErrorMessage.then(function (msg) {
//console.log(msg)
})
self.loadingBar.hide()
})
//pdfLinkService = new PDFLinkService();
console.log("viewer", PDFViewer);
pdfViewer = new PDFJS.PDFViewer({
container: container,
//linkService: pdfLinkService,
});
//pdfLinkService.setViewer(pdfViewer);
container.addEventListener('pagesinit', function () {
// We can use pdfViewer now, e.g. let's change default scale.
pdfViewer.currentScaleValue = 'page-width';
});
// Loading document.
PDFJS.getDocument(data).then(function (pdfDocument) {
// Document loaded, specifying document for the viewer and
// the (optional) linkService.
pdfViewer.setDocument(pdfDocument);
//pdfLinkService.setDocument(pdfDocument, null);
});
}, function (exception) {
let message = exception && exception.message
let l10n = self.l10n
let loadingErrorMessage
if (exception instanceof pdfjsLib.InvalidPDFException) {
// change error message also for other builds
loadingErrorMessage = l10n.get('invalid_file_error', null,
'Invalid or corrupted PDF file.')
} else if (exception instanceof pdfjsLib.MissingPDFException) {
// special message for missing PDFs
loadingErrorMessage = l10n.get('missing_file_error', null,
'Missing PDF file.')
} else if (exception instanceof pdfjsLib.UnexpectedResponseException) {
loadingErrorMessage = l10n.get('unexpected_response_error', null,
'Unexpected server response.')
} else {
loadingErrorMessage = l10n.get('loading_error', null,
'An error occurred while loading the PDF.')
}
loadingErrorMessage.then(function (msg) {
//console.log(msg)
})
self.loadingBar.hide()
})
}