Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function createPDFViewer() {
PDFJS.GlobalWorkerOptions.workerSrc = '../../../node_modules/pdfjs-dist/build/pdf.worker.js';
const container = document.getElementById('viewerContainer');
if (! container) {
throw new Error("No container");
}
const pdfLinkService = new PDFLinkService();
// the text area is working!
//
// FIXME: I think I ahve to specify a custom RenderingQueue here because right now it renders ALL the pages...
//
// // and I need to limit this to say += 10 pages..
//
// // FIXME: how do we change the zoom
// Inspired by: https://github.com/javascriptiscoolpl/npm-simple-react-pdf/blob/master/src/index.js
import React from 'react'
import { PDFViewControl } from '@archipel/ui'
import PDFjs from 'pdfjs-dist'
import registry from '@archipel/app/src/lib/component-registry'
// IMPORTANT: node_modules/pdfjs-dist/build/pdf.worker.js needs to be simlinked
// to the localhost:8080 basefolder. At the time of writing this, it was
// packages/app/dist/
PDFjs.GlobalWorkerOptions.workerSrc = './pdf.worker.js'
export default function init () {
registry.add('fileViewer', PDFViewer, {
stream: false,
match: pdfMatcher
})
}
function pdfMatcher (file) {
if (!file.mimetype) return false
return file.mimetype.match(/application\/pdf/)
}
export class PDFViewer extends React.Component {
constructor (props) {
super(props)
// Any copyright is dedicated to the Public Domain.
// http://creativecommons.org/licenses/publicdomain/
// Hello world example for browserify.
var pdfjsLib = require("pdfjs-dist");
var pdfPath = "../learning/helloworld.pdf";
// Setting worker path to worker bundle.
pdfjsLib.GlobalWorkerOptions.workerSrc =
"../../build/browserify/pdf.worker.bundle.js";
// Loading a document.
var loadingTask = pdfjsLib.getDocument(pdfPath);
loadingTask.promise
.then(function(pdfDocument) {
// Request a first page
return pdfDocument.getPage(1).then(function(pdfPage) {
// Display page on the existing canvas with 100% scale.
var viewport = pdfPage.getViewport({ scale: 1.0 });
var canvas = document.getElementById("theCanvas");
canvas.width = viewport.width;
canvas.height = viewport.height;
var ctx = canvas.getContext("2d");
var renderTask = pdfPage.render({
canvasContext: ctx,
import * as React from "react";
import * as CSSModules from "react-css-modules";
import * as styles from "./index.less";
import * as pdfjsLib from "pdfjs-dist";
const pdfjsViewer = require("../../../node_modules/pdfjs-dist/web/pdf_viewer.js");
// The workerSrc property shall be specified.
pdfjsLib.GlobalWorkerOptions.workerSrc = "https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.0.550/pdf.worker.js";
// default scale
const DEFAULT_MIN_SCALE = 0.25;
const DEFAULT_MAX_SCALE = 10.0;
let USE_ONLY_CSS_ZOOM = true;
let TEXT_LAYER_MODE = 0; // DISABLE
let MAX_IMAGE_SIZE = 1024 * 1024;
let CMAP_PACKED = true;
let DEFAULT_URL = "/test.pdf";
let DEFAULT_SCALE_DELTA = 1.1;
let MIN_SCALE = DEFAULT_MIN_SCALE;
let MAX_SCALE = DEFAULT_MAX_SCALE;
let DEFAULT_SCALE_VALUE: string|number = "auto"; // in order to be responsive
interface IProps {
url: string|object;
page?: number|string;
scale?: number|string;
].forEach(x => {
const el = document.querySelector(`meta[name='env:${x.toLowerCase()}']`);
configs[x] = el ? el.getAttribute("content") : process.env[x];
if (x === "BASE_ASSETS_PATH" && configs[x]) {
// eslint-disable-next-line no-undef
__webpack_public_path__ = configs[x];
// Using external CDN to reduce build size
pdfjs.GlobalWorkerOptions.workerSrc = `${configs[x]}../assets/js/pdfjs-dist@2.1.266/build/pdf.worker.js`;
}
});
import pdfjs from 'pdfjs-dist';
import Document from './Document';
import Outline from './Outline';
import Page from './Page';
import { isLocalFileSystem, warnOnDev } from './shared/utils';
if (isLocalFileSystem) {
// eslint-disable-next-line no-console
warnOnDev('You are running React-PDF from your local file system. PDF.js Worker may fail to load due to browser\'s security policies. If you\'re on Google Chrome, you can use --allow-file-access-from-files flag for debugging purposes.');
}
if (typeof window !== 'undefined' && 'Worker' in window) {
pdfjs.GlobalWorkerOptions.workerPort = new Worker('./pdf.worker.entry.js');
}
export {
pdfjs,
Document,
Outline,
Page,
};
export async function readPdf(pdfUrl: string): Promise {
if (!pdfjsLib.GlobalWorkerOptions.workerPort) {
pdfjsLib.GlobalWorkerOptions.workerPort = new PdfjsWorker();
}
const pdf = await pdfjsLib.getDocument(pdfUrl);
const lines: string[] = [];
for (let i = 0; i < pdf._pdfInfo.numPages; i++) {
const page = await pdf.getPage(i + 1);
const textContent = await page.getTextContent();
textContent.items
.map((item: any) => item.str)
.map((line: string) => line.replace(/([^ ]) ([^ ])/g, '$1 $2'))
.forEach((line: string) => lines.push(line));
}
constructor(props, context) {
super(props, context);
//Make sure that the worker version matches package.json pdfjs-dist version.
pdfJS.GlobalWorkerOptions.workerSrc = 'https://cdn.jsdelivr.net/npm/pdfjs-dist@2.1.266/build/pdf.worker.js';
this.handleUpload = this.handleUpload.bind(this);
this.handleXMLFile = this.handleXMLFile.bind(this);
this.handlePDFFile = this.handlePDFFile.bind(this);
this.handleInputEarnings = this.handleInputEarnings.bind(this);
this.handleManualEarnings = this.handleManualEarnings.bind(this);
this.handleSave = this.handleSave.bind(this);
this.fileInput = React.createRef();
this.state = {
elementLoaded: false,
earningsRecord: undefined,
defaultRecord: {
'osss:OnlineSocialSecurityStatementData': {
'osss:EarningsRecord': {
'osss:Earnings': []
useEffect(() => {
pdfjs.GlobalWorkerOptions.workerSrc = workerSrc;
}, [workerSrc]);
export async function readPdf(pdfUrl: string): Promise {
if (!pdfjsLib.GlobalWorkerOptions.workerPort) {
pdfjsLib.GlobalWorkerOptions.workerPort = new PdfjsWorker();
}
const pdf = await pdfjsLib.getDocument(pdfUrl);
const lines: string[] = [];
for (let i = 0; i < pdf._pdfInfo.numPages; i++) {
const page = await pdf.getPage(i + 1);
const textContent = await page.getTextContent();
textContent.items
.map((item: any) => item.str)
.map((line: string) => line.replace(/([^ ]) ([^ ])/g, '$1 $2'))
.forEach((line: string) => lines.push(line));
}