Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@mikecousins/react-pdf

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@mikecousins/react-pdf - npm Package Compare versions

Comparing version 5.5.0 to 5.5.1

39

dist/index.d.ts

@@ -0,25 +1,18 @@

import pdfjs from '@bundled-es-modules/pdfjs-dist';
import React from 'react';
declare type ComponentProps = {
file: string;
onDocumentComplete: (numPages: number) => void;
onPageLoaded: () => void;
page?: number;
scale?: number;
rotate?: number;
cMapUrl?: string;
cMapPacked?: boolean;
workerSrc?: string;
withCredentials?: boolean;
declare type ComponentRenderProps = HookReturnValues & {
canvas: React.ReactElement;
};
declare const Pdf: {
({ file, onDocumentComplete, onPageLoaded, page, scale, rotate, cMapUrl, cMapPacked, workerSrc, withCredentials, }: ComponentProps): JSX.Element;
defaultProps: {
onDocumentComplete: () => void;
onPageLoaded: () => void;
};
};
declare const Pdf: React.ForwardRefExoticComponent<Pick<HookProps, "file" | "onDocumentLoadSuccess" | "onDocumentLoadFail" | "onPageLoadSuccess" | "onPageLoadFail" | "onPageRenderSuccess" | "onPageRenderFail" | "scale" | "rotate" | "page" | "cMapUrl" | "cMapPacked" | "workerSrc" | "withCredentials"> & React.CanvasHTMLAttributes<HTMLCanvasElement> & {
children?: ((renderProps: ComponentRenderProps) => React.ReactElement<any, string | ((props: any) => React.ReactElement<any, string | any | (new (props: any) => React.Component<any, any, any>)> | null) | (new (props: any) => React.Component<any, any, any>)>) | undefined;
} & React.RefAttributes<HTMLCanvasElement | null>>;
declare type HookProps = {
canvasEl: React.RefObject<HTMLCanvasElement>;
canvasRef: React.RefObject<HTMLCanvasElement | null>;
file: string;
onPageLoaded: () => void;
onDocumentLoadSuccess?: (document: pdfjs.PDFDocumentProxy) => void;
onDocumentLoadFail?: () => void;
onPageLoadSuccess?: (page: pdfjs.PDFPageProxy) => void;
onPageLoadFail?: () => void;
onPageRenderSuccess?: (page: pdfjs.PDFPageProxy) => void;
onPageRenderFail?: () => void;
scale?: number;

@@ -33,3 +26,7 @@ rotate?: number;

};
export declare const usePdf: ({ canvasEl, file, onPageLoaded, scale, rotate, page, cMapUrl, cMapPacked, workerSrc, withCredentials, }: HookProps) => any[];
declare type HookReturnValues = {
pdfDocument: pdfjs.PDFDocumentProxy | undefined;
pdfPage: pdfjs.PDFPageProxy | undefined;
};
export declare const usePdf: ({ canvasRef, file, onDocumentLoadSuccess, onDocumentLoadFail, onPageLoadSuccess, onPageLoadFail, onPageRenderSuccess, onPageRenderFail, scale, rotate, page, cMapUrl, cMapPacked, workerSrc, withCredentials, }: HookProps) => HookReturnValues;
export default Pdf;

@@ -7,10 +7,53 @@ 'use strict';

var pdfjs = _interopDefault(require('pdfjs-dist'));
var pdfjs = _interopDefault(require('@bundled-es-modules/pdfjs-dist'));
var React = require('react');
var React__default = _interopDefault(React);
var Pdf = function Pdf(_ref) {
function _extends() {
_extends = Object.assign || function (target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
return target;
};
return _extends.apply(this, arguments);
}
function _objectWithoutPropertiesLoose(source, excluded) {
if (source == null) return {};
var target = {};
var sourceKeys = Object.keys(source);
var key, i;
for (i = 0; i < sourceKeys.length; i++) {
key = sourceKeys[i];
if (excluded.indexOf(key) >= 0) continue;
target[key] = source[key];
}
return target;
}
function isFunction(value) {
return typeof value === 'function';
}
var Pdf =
/*#__PURE__*/
React__default.forwardRef(function (_ref, ref) {
var file = _ref.file,
onDocumentComplete = _ref.onDocumentComplete,
onPageLoaded = _ref.onPageLoaded,
onDocumentLoadSuccess = _ref.onDocumentLoadSuccess,
onDocumentLoadFail = _ref.onDocumentLoadFail,
onPageLoadSuccess = _ref.onPageLoadSuccess,
onPageLoadFail = _ref.onPageLoadFail,
onPageRenderSuccess = _ref.onPageRenderSuccess,
onPageRenderFail = _ref.onPageRenderFail,
page = _ref.page,

@@ -22,8 +65,19 @@ scale = _ref.scale,

workerSrc = _ref.workerSrc,
withCredentials = _ref.withCredentials;
var canvasEl = React.useRef(null);
withCredentials = _ref.withCredentials,
children = _ref.children,
canvasProps = _objectWithoutPropertiesLoose(_ref, ["file", "onDocumentLoadSuccess", "onDocumentLoadFail", "onPageLoadSuccess", "onPageLoadFail", "onPageRenderSuccess", "onPageRenderFail", "page", "scale", "rotate", "cMapUrl", "cMapPacked", "workerSrc", "withCredentials", "children"]);
var _usePdf = usePdf({
canvasEl: canvasEl,
var canvasRef = React.useRef(null);
React.useImperativeHandle(ref, function () {
return canvasRef.current;
});
var pdfData = usePdf({
canvasRef: canvasRef,
file: file,
onDocumentLoadSuccess: onDocumentLoadSuccess,
onDocumentLoadFail: onDocumentLoadFail,
onPageLoadSuccess: onPageLoadSuccess,
onPageLoadFail: onPageLoadFail,
onPageRenderSuccess: onPageRenderSuccess,
onPageRenderFail: onPageRenderFail,
page: page,

@@ -35,23 +89,25 @@ scale: scale,

workerSrc: workerSrc,
withCredentials: withCredentials,
onPageLoaded: onPageLoaded
}),
numPages = _usePdf[1];
React.useEffect(function () {
onDocumentComplete(numPages);
}, [numPages, onDocumentComplete]);
return React__default.createElement("canvas", {
ref: canvasEl
withCredentials: withCredentials
});
};
var canvas = React__default.createElement("canvas", Object.assign({}, canvasProps, {
ref: canvasRef
}));
Pdf.defaultProps = {
onDocumentComplete: function onDocumentComplete() {},
onPageLoaded: function onPageLoaded() {}
};
if (isFunction(children)) {
return children(_extends({
canvas: canvas
}, pdfData));
}
return canvas;
});
var usePdf = function usePdf(_ref2) {
var canvasEl = _ref2.canvasEl,
var canvasRef = _ref2.canvasRef,
file = _ref2.file,
onPageLoaded = _ref2.onPageLoaded,
onDocumentLoadSuccess = _ref2.onDocumentLoadSuccess,
onDocumentLoadFail = _ref2.onDocumentLoadFail,
onPageLoadSuccess = _ref2.onPageLoadSuccess,
onPageLoadFail = _ref2.onPageLoadFail,
onPageRenderSuccess = _ref2.onPageRenderSuccess,
onPageRenderFail = _ref2.onPageRenderFail,
_ref2$scale = _ref2.scale,

@@ -71,8 +127,38 @@ scale = _ref2$scale === void 0 ? 1 : _ref2$scale,

var _useState = React.useState(),
pdf = _useState[0],
setPdf = _useState[1];
pdfDocument = _useState[0],
setPdfDocument = _useState[1];
var _useState2 = React.useState(),
pdfPage = _useState2[0],
setPdfPage = _useState2[1];
var renderTask = React.useRef(null);
var onDocumentLoadSuccessRef = React.useRef(onDocumentLoadSuccess);
var onDocumentLoadFailRef = React.useRef(onDocumentLoadFail);
var onPageLoadSuccessRef = React.useRef(onPageLoadSuccess);
var onPageLoadFailRef = React.useRef(onPageLoadFail);
var onPageRenderSuccessRef = React.useRef(onPageRenderSuccess);
var onPageRenderFailRef = React.useRef(onPageRenderFail); // assign callbacks to refs to avoid redrawing
React.useEffect(function () {
onDocumentLoadSuccessRef.current = onDocumentLoadSuccess;
}, [onDocumentLoadSuccess]);
React.useEffect(function () {
onDocumentLoadFailRef.current = onDocumentLoadFail;
}, [onDocumentLoadFail]);
React.useEffect(function () {
onPageLoadSuccessRef.current = onPageLoadSuccess;
}, [onPageLoadSuccess]);
React.useEffect(function () {
onPageLoadFailRef.current = onPageLoadFail;
}, [onPageLoadFail]);
React.useEffect(function () {
onPageRenderSuccessRef.current = onPageRenderSuccess;
}, [onPageRenderSuccess]);
React.useEffect(function () {
onPageRenderFailRef.current = onPageRenderFail;
}, [onPageRenderFail]);
React.useEffect(function () {
pdfjs.GlobalWorkerOptions.workerSrc = workerSrc;
}, []);
}, [workerSrc]);
React.useEffect(function () {

@@ -89,49 +175,89 @@ var config = {

pdfjs.getDocument(config).promise.then(setPdf);
}, [file, withCredentials, cMapUrl, cMapPacked]); // handle changes
pdfjs.getDocument(config).promise.then(function (loadedPdfDocument) {
setPdfDocument(loadedPdfDocument);
if (isFunction(onDocumentLoadSuccessRef.current)) {
onDocumentLoadSuccessRef.current(loadedPdfDocument);
}
}, function () {
if (isFunction(onDocumentLoadFailRef.current)) {
onDocumentLoadFailRef.current();
}
});
}, [file, withCredentials, cMapUrl, cMapPacked]);
React.useEffect(function () {
if (pdf) {
pdf.getPage(page).then(function (p) {
return drawPDF(p);
// 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 = window.devicePixelRatio;
var adjustedScale = scale * dpRatio;
var viewport = page.getViewport({
scale: adjustedScale,
rotation: rotation
});
}
}, [pdf, page, scale, rotate, canvasEl]); // draw a page of the pdf
var canvasEl = canvasRef.current;
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 (!canvasEl) {
return;
}
if (!canvas) {
return;
}
var canvasContext = canvasEl.getContext('2d');
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
if (!canvasContext) {
return;
}
canvasEl.style.width = viewport.width / dpRatio + "px";
canvasEl.style.height = viewport.height / dpRatio + "px";
canvasEl.height = viewport.height;
canvasEl.width = viewport.width; // if previous render isn't done yet, we cancel it
if (renderTask.current) {
renderTask.current.cancel();
return;
}
renderTask.current = page.render({
canvasContext: canvasContext,
viewport: viewport
});
return renderTask.current.promise.then(function () {
renderTask.current = null;
if (isFunction(onPageRenderSuccessRef.current)) {
onPageRenderSuccessRef.current(page);
}
}, function (err) {
renderTask.current = null; // @ts-ignore typings are outdated
if (err && err.name === 'RenderingCancelledException') {
drawPDF(page);
} else if (isFunction(onPageRenderFailRef.current)) {
onPageRenderFailRef.current();
}
});
};
page.render(renderContext).promise.then(onPageLoaded);
if (pdfDocument) {
pdfDocument.getPage(page).then(function (loadedPdfPage) {
setPdfPage(loadedPdfPage);
if (isFunction(onPageLoadSuccessRef.current)) {
onPageLoadSuccessRef.current(loadedPdfPage);
}
drawPDF(loadedPdfPage);
}, function () {
if (isFunction(onPageLoadFailRef.current)) {
onPageLoadFailRef.current();
}
});
}
}, [canvasRef, page, pdfDocument, rotate, scale]);
return {
pdfDocument: pdfDocument,
pdfPage: pdfPage
};
var loading = React.useMemo(function () {
return !pdf;
}, [pdf]);
var numPages = React.useMemo(function () {
return pdf ? pdf._pdfInfo.numPages : null;
}, [pdf]);
return [loading, numPages];
};

@@ -138,0 +264,0 @@

@@ -1,2 +0,2 @@

"use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var t=e(require("pdfjs-dist")),r=require("react"),o=e(r),a=function(e){var t=e.file,a=e.onDocumentComplete,i=e.onPageLoaded,c=e.page,u=e.scale,d=e.rotate,s=e.cMapUrl,f=e.cMapPacked,l=e.workerSrc,p=e.withCredentials,v=r.useRef(null),h=n({canvasEl:v,file:t,page:c,scale:u,rotate:d,cMapUrl:s,cMapPacked:f,workerSrc:l,withCredentials:p,onPageLoaded:i})[1];return r.useEffect((function(){a(h)}),[h,a]),o.createElement("canvas",{ref:v})};a.defaultProps={onDocumentComplete:function(){},onPageLoaded:function(){}};var n=function(e){var o=e.canvasEl,a=e.file,n=e.onPageLoaded,i=e.scale,c=void 0===i?1:i,u=e.rotate,d=void 0===u?0:u,s=e.page,f=void 0===s?1:s,l=e.cMapUrl,p=e.cMapPacked,v=e.workerSrc,h=void 0===v?"//cdnjs.cloudflare.com/ajax/libs/pdf.js/"+t.version+"/pdf.worker.js":v,g=e.withCredentials,w=void 0!==g&&g,P=r.useState(),m=P[0],M=P[1];r.useEffect((function(){t.GlobalWorkerOptions.workerSrc=h}),[]),r.useEffect((function(){var e={url:a,withCredentials:w};l&&(e.cMapUrl=l,e.cMapPacked=p),t.getDocument(e).promise.then(M)}),[a,w,l,p]),r.useEffect((function(){m&&m.getPage(f).then((function(e){return k(e)}))}),[m,f,c,d,o]);var k=function(e){var t;t=window.devicePixelRatio;var r=e.getViewport({scale:c*t,rotation:0===d?e.rotate:e.rotate+d}),a=o.current;if(a){var i=a.getContext("2d");a.style.width=r.width/t+"px",a.style.height=r.height/t+"px",a.height=r.height,a.width=r.width,e.render({canvasContext:i,viewport:r}).promise.then(n)}};return[r.useMemo((function(){return!m}),[m]),r.useMemo((function(){return m?m._pdfInfo.numPages:null}),[m])]};exports.default=a,exports.usePdf=n;
"use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var n=e(require("@bundled-es-modules/pdfjs-dist")),r=require("react"),t=e(r);function c(){return(c=Object.assign||function(e){for(var n=1;n<arguments.length;n++){var r=arguments[n];for(var t in r)Object.prototype.hasOwnProperty.call(r,t)&&(e[t]=r[t])}return e}).apply(this,arguments)}function a(e){return"function"==typeof e}var o=t.forwardRef((function(e,n){var o=e.file,i=e.onDocumentLoadSuccess,s=e.onDocumentLoadFail,f=e.onPageLoadSuccess,d=e.onPageLoadFail,l=e.onPageRenderSuccess,p=e.onPageRenderFail,g=e.page,v=e.scale,P=e.rotate,h=e.cMapUrl,w=e.cMapPacked,R=e.workerSrc,S=e.withCredentials,m=e.children,L=function(e,n){if(null==e)return{};var r,t,c={},a=Object.keys(e);for(t=0;t<a.length;t++)n.indexOf(r=a[t])>=0||(c[r]=e[r]);return c}(e,["file","onDocumentLoadSuccess","onDocumentLoadFail","onPageLoadSuccess","onPageLoadFail","onPageRenderSuccess","onPageRenderFail","page","scale","rotate","cMapUrl","cMapPacked","workerSrc","withCredentials","children"]),k=r.useRef(null);r.useImperativeHandle(n,(function(){return k.current}));var F=u({canvasRef:k,file:o,onDocumentLoadSuccess:i,onDocumentLoadFail:s,onPageLoadSuccess:f,onPageLoadFail:d,onPageRenderSuccess:l,onPageRenderFail:p,page:g,scale:v,rotate:P,cMapUrl:h,cMapPacked:w,workerSrc:R,withCredentials:S}),j=t.createElement("canvas",Object.assign({},L,{ref:k}));return a(m)?m(c({canvas:j},F)):j})),u=function(e){var t=e.canvasRef,c=e.file,o=e.onDocumentLoadSuccess,u=e.onDocumentLoadFail,i=e.onPageLoadSuccess,s=e.onPageLoadFail,f=e.onPageRenderSuccess,d=e.onPageRenderFail,l=e.scale,p=void 0===l?1:l,g=e.rotate,v=void 0===g?0:g,P=e.page,h=void 0===P?1:P,w=e.cMapUrl,R=e.cMapPacked,S=e.workerSrc,m=void 0===S?"//cdnjs.cloudflare.com/ajax/libs/pdf.js/"+n.version+"/pdf.worker.js":S,L=e.withCredentials,k=void 0!==L&&L,F=r.useState(),j=F[0],x=F[1],E=r.useState(),M=E[0],D=E[1],b=r.useRef(null),y=r.useRef(o),C=r.useRef(u),O=r.useRef(i),U=r.useRef(s),q=r.useRef(f),_=r.useRef(d);return r.useEffect((function(){y.current=o}),[o]),r.useEffect((function(){C.current=u}),[u]),r.useEffect((function(){O.current=i}),[i]),r.useEffect((function(){U.current=s}),[s]),r.useEffect((function(){q.current=f}),[f]),r.useEffect((function(){_.current=d}),[d]),r.useEffect((function(){n.GlobalWorkerOptions.workerSrc=m}),[m]),r.useEffect((function(){var e={url:c,withCredentials:k};w&&(e.cMapUrl=w,e.cMapPacked=R),n.getDocument(e).promise.then((function(e){x(e),a(y.current)&&y.current(e)}),(function(){a(C.current)&&C.current()}))}),[c,k,w,R]),r.useEffect((function(){j&&j.getPage(h).then((function(e){D(e),a(O.current)&&O.current(e),function e(n){var r=window.devicePixelRatio,c=n.getViewport({scale:p*r,rotation:0===v?n.rotate:n.rotate+v}),o=t.current;if(o){var u=o.getContext("2d");if(u){if(o.style.width=c.width/r+"px",o.style.height=c.height/r+"px",o.height=c.height,o.width=c.width,!b.current)return b.current=n.render({canvasContext:u,viewport:c}),b.current.promise.then((function(){b.current=null,a(q.current)&&q.current(n)}),(function(r){b.current=null,r&&"RenderingCancelledException"===r.name?e(n):a(_.current)&&_.current()}));b.current.cancel()}}}(e)}),(function(){a(U.current)&&U.current()}))}),[t,h,j,v,p]),{pdfDocument:j,pdfPage:M}};exports.default=o,exports.usePdf=u;
//# sourceMappingURL=react-pdf.cjs.production.min.js.map

@@ -1,8 +0,51 @@

import pdfjs from 'pdfjs-dist';
import React, { useRef, useEffect, useState, useMemo } from 'react';
import pdfjs from '@bundled-es-modules/pdfjs-dist';
import React, { useRef, useImperativeHandle, useState, useEffect } from 'react';
var Pdf = function Pdf(_ref) {
function _extends() {
_extends = Object.assign || function (target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
return target;
};
return _extends.apply(this, arguments);
}
function _objectWithoutPropertiesLoose(source, excluded) {
if (source == null) return {};
var target = {};
var sourceKeys = Object.keys(source);
var key, i;
for (i = 0; i < sourceKeys.length; i++) {
key = sourceKeys[i];
if (excluded.indexOf(key) >= 0) continue;
target[key] = source[key];
}
return target;
}
function isFunction(value) {
return typeof value === 'function';
}
var Pdf =
/*#__PURE__*/
React.forwardRef(function (_ref, ref) {
var file = _ref.file,
onDocumentComplete = _ref.onDocumentComplete,
onPageLoaded = _ref.onPageLoaded,
onDocumentLoadSuccess = _ref.onDocumentLoadSuccess,
onDocumentLoadFail = _ref.onDocumentLoadFail,
onPageLoadSuccess = _ref.onPageLoadSuccess,
onPageLoadFail = _ref.onPageLoadFail,
onPageRenderSuccess = _ref.onPageRenderSuccess,
onPageRenderFail = _ref.onPageRenderFail,
page = _ref.page,

@@ -14,8 +57,19 @@ scale = _ref.scale,

workerSrc = _ref.workerSrc,
withCredentials = _ref.withCredentials;
var canvasEl = useRef(null);
withCredentials = _ref.withCredentials,
children = _ref.children,
canvasProps = _objectWithoutPropertiesLoose(_ref, ["file", "onDocumentLoadSuccess", "onDocumentLoadFail", "onPageLoadSuccess", "onPageLoadFail", "onPageRenderSuccess", "onPageRenderFail", "page", "scale", "rotate", "cMapUrl", "cMapPacked", "workerSrc", "withCredentials", "children"]);
var _usePdf = usePdf({
canvasEl: canvasEl,
var canvasRef = useRef(null);
useImperativeHandle(ref, function () {
return canvasRef.current;
});
var pdfData = usePdf({
canvasRef: canvasRef,
file: file,
onDocumentLoadSuccess: onDocumentLoadSuccess,
onDocumentLoadFail: onDocumentLoadFail,
onPageLoadSuccess: onPageLoadSuccess,
onPageLoadFail: onPageLoadFail,
onPageRenderSuccess: onPageRenderSuccess,
onPageRenderFail: onPageRenderFail,
page: page,

@@ -27,23 +81,25 @@ scale: scale,

workerSrc: workerSrc,
withCredentials: withCredentials,
onPageLoaded: onPageLoaded
}),
numPages = _usePdf[1];
useEffect(function () {
onDocumentComplete(numPages);
}, [numPages, onDocumentComplete]);
return React.createElement("canvas", {
ref: canvasEl
withCredentials: withCredentials
});
};
var canvas = React.createElement("canvas", Object.assign({}, canvasProps, {
ref: canvasRef
}));
Pdf.defaultProps = {
onDocumentComplete: function onDocumentComplete() {},
onPageLoaded: function onPageLoaded() {}
};
if (isFunction(children)) {
return children(_extends({
canvas: canvas
}, pdfData));
}
return canvas;
});
var usePdf = function usePdf(_ref2) {
var canvasEl = _ref2.canvasEl,
var canvasRef = _ref2.canvasRef,
file = _ref2.file,
onPageLoaded = _ref2.onPageLoaded,
onDocumentLoadSuccess = _ref2.onDocumentLoadSuccess,
onDocumentLoadFail = _ref2.onDocumentLoadFail,
onPageLoadSuccess = _ref2.onPageLoadSuccess,
onPageLoadFail = _ref2.onPageLoadFail,
onPageRenderSuccess = _ref2.onPageRenderSuccess,
onPageRenderFail = _ref2.onPageRenderFail,
_ref2$scale = _ref2.scale,

@@ -63,8 +119,38 @@ scale = _ref2$scale === void 0 ? 1 : _ref2$scale,

var _useState = useState(),
pdf = _useState[0],
setPdf = _useState[1];
pdfDocument = _useState[0],
setPdfDocument = _useState[1];
var _useState2 = useState(),
pdfPage = _useState2[0],
setPdfPage = _useState2[1];
var renderTask = useRef(null);
var onDocumentLoadSuccessRef = useRef(onDocumentLoadSuccess);
var onDocumentLoadFailRef = useRef(onDocumentLoadFail);
var onPageLoadSuccessRef = useRef(onPageLoadSuccess);
var onPageLoadFailRef = useRef(onPageLoadFail);
var onPageRenderSuccessRef = useRef(onPageRenderSuccess);
var onPageRenderFailRef = useRef(onPageRenderFail); // assign callbacks to refs to avoid redrawing
useEffect(function () {
onDocumentLoadSuccessRef.current = onDocumentLoadSuccess;
}, [onDocumentLoadSuccess]);
useEffect(function () {
onDocumentLoadFailRef.current = onDocumentLoadFail;
}, [onDocumentLoadFail]);
useEffect(function () {
onPageLoadSuccessRef.current = onPageLoadSuccess;
}, [onPageLoadSuccess]);
useEffect(function () {
onPageLoadFailRef.current = onPageLoadFail;
}, [onPageLoadFail]);
useEffect(function () {
onPageRenderSuccessRef.current = onPageRenderSuccess;
}, [onPageRenderSuccess]);
useEffect(function () {
onPageRenderFailRef.current = onPageRenderFail;
}, [onPageRenderFail]);
useEffect(function () {
pdfjs.GlobalWorkerOptions.workerSrc = workerSrc;
}, []);
}, [workerSrc]);
useEffect(function () {

@@ -81,49 +167,89 @@ var config = {

pdfjs.getDocument(config).promise.then(setPdf);
}, [file, withCredentials, cMapUrl, cMapPacked]); // handle changes
pdfjs.getDocument(config).promise.then(function (loadedPdfDocument) {
setPdfDocument(loadedPdfDocument);
if (isFunction(onDocumentLoadSuccessRef.current)) {
onDocumentLoadSuccessRef.current(loadedPdfDocument);
}
}, function () {
if (isFunction(onDocumentLoadFailRef.current)) {
onDocumentLoadFailRef.current();
}
});
}, [file, withCredentials, cMapUrl, cMapPacked]);
useEffect(function () {
if (pdf) {
pdf.getPage(page).then(function (p) {
return drawPDF(p);
// 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 = window.devicePixelRatio;
var adjustedScale = scale * dpRatio;
var viewport = page.getViewport({
scale: adjustedScale,
rotation: rotation
});
}
}, [pdf, page, scale, rotate, canvasEl]); // draw a page of the pdf
var canvasEl = canvasRef.current;
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 (!canvasEl) {
return;
}
if (!canvas) {
return;
}
var canvasContext = canvasEl.getContext('2d');
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
if (!canvasContext) {
return;
}
canvasEl.style.width = viewport.width / dpRatio + "px";
canvasEl.style.height = viewport.height / dpRatio + "px";
canvasEl.height = viewport.height;
canvasEl.width = viewport.width; // if previous render isn't done yet, we cancel it
if (renderTask.current) {
renderTask.current.cancel();
return;
}
renderTask.current = page.render({
canvasContext: canvasContext,
viewport: viewport
});
return renderTask.current.promise.then(function () {
renderTask.current = null;
if (isFunction(onPageRenderSuccessRef.current)) {
onPageRenderSuccessRef.current(page);
}
}, function (err) {
renderTask.current = null; // @ts-ignore typings are outdated
if (err && err.name === 'RenderingCancelledException') {
drawPDF(page);
} else if (isFunction(onPageRenderFailRef.current)) {
onPageRenderFailRef.current();
}
});
};
page.render(renderContext).promise.then(onPageLoaded);
if (pdfDocument) {
pdfDocument.getPage(page).then(function (loadedPdfPage) {
setPdfPage(loadedPdfPage);
if (isFunction(onPageLoadSuccessRef.current)) {
onPageLoadSuccessRef.current(loadedPdfPage);
}
drawPDF(loadedPdfPage);
}, function () {
if (isFunction(onPageLoadFailRef.current)) {
onPageLoadFailRef.current();
}
});
}
}, [canvasRef, page, pdfDocument, rotate, scale]);
return {
pdfDocument: pdfDocument,
pdfPage: pdfPage
};
var loading = useMemo(function () {
return !pdf;
}, [pdf]);
var numPages = useMemo(function () {
return pdf ? pdf._pdfInfo.numPages : null;
}, [pdf]);
return [loading, numPages];
};

@@ -130,0 +256,0 @@

{
"name": "@mikecousins/react-pdf",
"version": "5.5.0",
"version": "5.5.1",
"description": "Simple React component to wrap up PDF.js. The easiest way to render PDFs in your React app.",

@@ -43,7 +43,7 @@ "author": "mikecousins",

"@testing-library/react": "9.4.0",
"@types/jest": "24.9.0",
"@types/jest": "25.1.2",
"@types/pdfjs-dist": "2.1.3",
"@types/react": "16.9.17",
"@types/react-dom": "16.9.4",
"husky": "4.0.10",
"@types/react": "16.9.19",
"@types/react-dom": "16.9.5",
"husky": "4.2.3",
"jest-canvas-mock": "^2.2.0",

@@ -57,4 +57,4 @@ "react": "16.12.0",

"dependencies": {
"pdfjs-dist": "2.2.228"
"@bundled-es-modules/pdfjs-dist": "^2.2.228-alpha1"
}
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc