How to use the pdfjs-dist.GlobalWorkerOptions function in pdfjs-dist

To help you get started, we’ve selected a few pdfjs-dist examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github burtonator / polar-bookshelf / web / spectron0 / ui-components / pdfviewer.ts View on Github external
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
github arso-project / archipel / packages / plugin-pdf / frontend.js View on Github external
// 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)
github mozilla / pdf.js / examples / browserify / main.js View on Github external
// 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,
github rahul2104 / reactjs-pdf-reader / src / components / MobilePDFReader / index.tsx View on Github external
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;
github mozilla / hubs / src / utils / configs.js View on Github external
].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`;
  }
});
github wojtekmaj / react-pdf / src / entry.parcel.js View on Github external
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,
};
github jmerle / competitive-companion / src / utils / pdf.ts View on Github external
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));
  }
github codeforboston / windfall-elimination / src / components / file-upload.jsx View on Github external
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': []
github mikecousins / react-pdf-js / src / index.tsx View on Github external
useEffect(() => {
    pdfjs.GlobalWorkerOptions.workerSrc = workerSrc;
  }, [workerSrc]);
github jmerle / competitive-companion / src / utils / pdf.ts View on Github external
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));
  }