Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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()
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);
export const usePdf = ({
canvasEl,
file,
onPageLoaded = undefined,
scale = 1,
rotate = 0,
page = 1,
cMapUrl,
cMapPacked,
workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.js`,
withCredentials = false,
}: HookProps) => {
const [pdf, setPdf] = useState();
const renderTask = useRef(null);
useEffect(() => {
pdfjs.GlobalWorkerOptions.workerSrc = workerSrc;
}, [workerSrc]);
useEffect(() => {
const config: pdfjs.PDFSource = { url: file, withCredentials };
if (cMapUrl) {
config.cMapUrl = cMapUrl;
config.cMapPacked = cMapPacked;
}
pdfjs.getDocument(config).promise.then(setPdf);