question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issues with usage

See original GitHub issue

It seems like source code and built code have some inconsistencies.

I installed the latest version and my dist script looks like that:

import pdfjs from 'pdfjs-dist';
import React, { useRef, useEffect, useState, useMemo } from 'react';

var Pdf = function Pdf(_ref) {
  var file = _ref.file,
      onDocumentComplete = _ref.onDocumentComplete,
      onPageLoaded = _ref.onPageLoaded,
      page = _ref.page,
      scale = _ref.scale,
      rotate = _ref.rotate,
      cMapUrl = _ref.cMapUrl,
      cMapPacked = _ref.cMapPacked,
      workerSrc = _ref.workerSrc,
      withCredentials = _ref.withCredentials;
  var canvasEl = useRef(null);

  var _usePdf = usePdf({
    canvasEl: canvasEl,
    file: file,
    page: page,
    scale: scale,
    rotate: rotate,
    cMapUrl: cMapUrl,
    cMapPacked: cMapPacked,
    workerSrc: workerSrc,
    withCredentials: withCredentials,
    onPageLoaded: onPageLoaded
  }),
      numPages = _usePdf[1];

  useEffect(function () {
    onDocumentComplete(numPages);
  }, [numPages, onDocumentComplete]);
  return React.createElement("canvas", {
    ref: canvasEl
  });
};

Pdf.defaultProps = {
  onDocumentComplete: function onDocumentComplete() {},
  onPageLoaded: function onPageLoaded() {}
};

var usePdf = function usePdf(_ref2) {
  var canvasEl = _ref2.canvasEl,
      file = _ref2.file,
      onPageLoaded = _ref2.onPageLoaded,
      _ref2$scale = _ref2.scale,
      scale = _ref2$scale === void 0 ? 1 : _ref2$scale,
      _ref2$rotate = _ref2.rotate,
      rotate = _ref2$rotate === void 0 ? 0 : _ref2$rotate,
      _ref2$page = _ref2.page,
      page = _ref2$page === void 0 ? 1 : _ref2$page,
      cMapUrl = _ref2.cMapUrl,
      cMapPacked = _ref2.cMapPacked,
      _ref2$workerSrc = _ref2.workerSrc,
      workerSrc = _ref2$workerSrc === void 0 ? "//cdnjs.cloudflare.com/ajax/libs/pdf.js/" + pdfjs.version + "/pdf.worker.js" : _ref2$workerSrc,
      _ref2$withCredentials = _ref2.withCredentials,
      withCredentials = _ref2$withCredentials === void 0 ? false : _ref2$withCredentials;

  var _useState = useState(),
      pdf = _useState[0],
      setPdf = _useState[1];

  useEffect(function () {
    pdfjs.GlobalWorkerOptions.workerSrc = workerSrc;
  }, []);
  useEffect(function () {
    var config = {
      url: file,
      withCredentials: withCredentials
    };

    if (cMapUrl) {
      config.cMapUrl = cMapUrl;
      config.cMapPacked = cMapPacked;
    }

    pdfjs.getDocument(config).promise.then(setPdf);
  }, [file, withCredentials, cMapUrl, cMapPacked]); // handle changes

  useEffect(function () {
    if (pdf) {
      pdf.getPage(page).then(function (p) {
        return drawPDF(p);
      });
    }
  }, [pdf, page, scale, rotate, canvasEl]); // draw a page of the pdf

  var drawPDF = function drawPDF(page) {
    // Because this page's rotation option overwrites pdf default rotation value,
    // calculating page rotation option value from pdf default and this component prop rotate.
    var rotation = rotate === 0 ? page.rotate : page.rotate + rotate;
    var dpRatio = 1;
    dpRatio = window.devicePixelRatio;
    var adjustedScale = scale * dpRatio;
    var viewport = page.getViewport({
      scale: adjustedScale,
      rotation: rotation
    });
    var canvas = canvasEl.current;

    if (!canvas) {
      return;
    }

    var canvasContext = canvas.getContext('2d');
    canvas.style.width = viewport.width / dpRatio + "px";
    canvas.style.height = viewport.height / dpRatio + "px";
    canvas.height = viewport.height;
    canvas.width = viewport.width;
    var renderContext = {
      canvasContext: canvasContext,
      viewport: viewport
    };
    page.render(renderContext).promise.then(onPageLoaded);
  };

  var loading = useMemo(function () {
    return !pdf;
  }, [pdf]);
  var numPages = useMemo(function () {
    return pdf ? pdf._pdfInfo.numPages : null;
  }, [pdf]);
  return [loading, numPages];
};

export default Pdf;
export { usePdf };

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
NexisSiscommented, Jan 31, 2020

I installed the latest version (5.5) from npm, but nothing different from 5.4, seems like incorrect loading of the package. Please check it.

0reactions
mikecousinscommented, Feb 15, 2020

Oooh right, thanks guys. This should be fixed now with v5.5.1.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Most Common Usage Problems - English Creek
Most Common Usage Problems. 1) A / AN - The article “a” is used before consonant sounds, and the article “an” is used...
Read more >
Usage issues—How are you doing? | OUPblog
When people talk about grammar problems, they often mean usage issues—departures from the traditional conventions for edited English and the ...
Read more >
10 common usage problems - Quill
Dictionaries include as many usage notes as they can, but dictionaries are descriptive, not prescriptive.
Read more >
15 common usage problems by Shawn McDonough - Prezi
Do not use LIKE before a subject and a verb. Use AS or THAT instead. Incorrect: This neighborhood doesn't look LIKE I remember....
Read more >
Are Grammar Critics Making a Usage Error? - Merriam-Webster
Some people would consider the question of whether a writer is potentially misusing a word to be more of an issue of usage—a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found