@mikecousins/react-pdf
Advanced tools
Comparing version 5.5.1 to 6.0.4
@@ -1,17 +0,11 @@ | ||
import pdfjs from '@bundled-es-modules/pdfjs-dist'; | ||
import React from 'react'; | ||
declare type ComponentRenderProps = HookReturnValues & { | ||
canvas: React.ReactElement; | ||
}; | ||
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>>; | ||
import { PDFDocumentProxy, PDFPageProxy } from 'pdfjs-dist'; | ||
declare type HookProps = { | ||
canvasRef: React.RefObject<HTMLCanvasElement | null>; | ||
file: string; | ||
onDocumentLoadSuccess?: (document: pdfjs.PDFDocumentProxy) => void; | ||
onDocumentLoadSuccess?: (document: PDFDocumentProxy) => void; | ||
onDocumentLoadFail?: () => void; | ||
onPageLoadSuccess?: (page: pdfjs.PDFPageProxy) => void; | ||
onPageLoadSuccess?: (page: PDFPageProxy) => void; | ||
onPageLoadFail?: () => void; | ||
onPageRenderSuccess?: (page: pdfjs.PDFPageProxy) => void; | ||
onPageRenderSuccess?: (page: PDFPageProxy) => void; | ||
onPageRenderFail?: () => void; | ||
@@ -27,6 +21,7 @@ scale?: number; | ||
declare type HookReturnValues = { | ||
pdfDocument: pdfjs.PDFDocumentProxy | undefined; | ||
pdfPage: pdfjs.PDFPageProxy | undefined; | ||
pdfDocument: PDFDocumentProxy | undefined; | ||
pdfPage: 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; | ||
declare const usePdf: ({ canvasRef, file, onDocumentLoadSuccess, onDocumentLoadFail, onPageLoadSuccess, onPageLoadFail, onPageRenderSuccess, onPageRenderFail, scale, rotate, page, cMapUrl, cMapPacked, workerSrc, withCredentials, }: HookProps) => HookReturnValues; | ||
export { usePdf }; |
@@ -1,8 +0,257 @@ | ||
'use strict' | ||
if (process.env.NODE_ENV === 'production') { | ||
module.exports = require('./react-pdf.cjs.production.min.js') | ||
} else { | ||
module.exports = require('./react-pdf.cjs.development.js') | ||
"use strict"; | ||
function _arrayLikeToArray(arr, len) { | ||
if (len == null || len > arr.length) len = arr.length; | ||
for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i]; | ||
return arr2; | ||
} | ||
function _arrayWithHoles(arr) { | ||
if (Array.isArray(arr)) return arr; | ||
} | ||
function _iterableToArrayLimit(arr, i) { | ||
var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; | ||
if (_i == null) return; | ||
var _arr = []; | ||
var _n = true; | ||
var _d = false; | ||
var _s, _e; | ||
try { | ||
for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){ | ||
_arr.push(_s.value); | ||
if (i && _arr.length === i) break; | ||
} | ||
} catch (err) { | ||
_d = true; | ||
_e = err; | ||
} finally{ | ||
try { | ||
if (!_n && _i["return"] != null) _i["return"](); | ||
} finally{ | ||
if (_d) throw _e; | ||
} | ||
} | ||
return _arr; | ||
} | ||
function _nonIterableRest() { | ||
throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); | ||
} | ||
function _slicedToArray(arr, i) { | ||
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); | ||
} | ||
function _unsupportedIterableToArray(o, minLen) { | ||
if (!o) return; | ||
if (typeof o === "string") return _arrayLikeToArray(o, minLen); | ||
var n = Object.prototype.toString.call(o).slice(8, -1); | ||
if (n === "Object" && o.constructor) n = o.constructor.name; | ||
if (n === "Map" || n === "Set") return Array.from(n); | ||
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); | ||
} | ||
var __create = Object.create; | ||
var __defProp = Object.defineProperty; | ||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
var __getOwnPropNames = Object.getOwnPropertyNames; | ||
var __getProtoOf = Object.getPrototypeOf; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __export = function(target, all) { | ||
for(var name in all)__defProp(target, name, { | ||
get: all[name], | ||
enumerable: true | ||
}); | ||
}; | ||
var __copyProps = function(to, from, except, desc) { | ||
if (from && typeof from === "object" || typeof from === "function") { | ||
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined; | ||
try { | ||
var _loop = function(_iterator, _step) { | ||
var key = _step.value; | ||
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { | ||
get: function() { | ||
return from[key]; | ||
}, | ||
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable | ||
}); | ||
}; | ||
for(var _iterator = __getOwnPropNames(from)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true)_loop(_iterator, _step); | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally{ | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return != null) { | ||
_iterator.return(); | ||
} | ||
} finally{ | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
} | ||
return to; | ||
}; | ||
var __toESM = function(mod, isNodeMode, target) { | ||
return target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { | ||
value: mod, | ||
enumerable: true | ||
}) : target, mod); | ||
}; | ||
var __toCommonJS = function(mod) { | ||
return __copyProps(__defProp({}, "__esModule", { | ||
value: true | ||
}), mod); | ||
}; | ||
// src/index.tsx | ||
var src_exports = {}; | ||
__export(src_exports, { | ||
usePdf: function() { | ||
return usePdf; | ||
} | ||
}); | ||
module.exports = __toCommonJS(src_exports); | ||
var import_react = require("react"); | ||
var pdfjs = __toESM(require("pdfjs-dist")); | ||
function isFunction(value) { | ||
return typeof value === "function"; | ||
} | ||
var usePdf = function(param) { | ||
var canvasRef = param.canvasRef, file = param.file, onDocumentLoadSuccess = param.onDocumentLoadSuccess, onDocumentLoadFail = param.onDocumentLoadFail, onPageLoadSuccess = param.onPageLoadSuccess, onPageLoadFail = param.onPageLoadFail, onPageRenderSuccess = param.onPageRenderSuccess, onPageRenderFail = param.onPageRenderFail, _scale = param.scale, scale = _scale === void 0 ? 1 : _scale, _rotate = param.rotate, rotate = _rotate === void 0 ? 0 : _rotate, _page = param.page, page = _page === void 0 ? 1 : _page, cMapUrl = param.cMapUrl, cMapPacked = param.cMapPacked, _workerSrc = param.workerSrc, workerSrc = _workerSrc === void 0 ? "//cdnjs.cloudflare.com/ajax/libs/pdf.js/".concat(pdfjs.version, "/pdf.worker.js") : _workerSrc, _withCredentials = param.withCredentials, withCredentials = _withCredentials === void 0 ? false : _withCredentials; | ||
var ref = _slicedToArray((0, import_react.useState)(), 2), pdfDocument = ref[0], setPdfDocument = ref[1]; | ||
var ref1 = _slicedToArray((0, import_react.useState)(), 2), pdfPage = ref1[0], setPdfPage = ref1[1]; | ||
var renderTask = (0, import_react.useRef)(null); | ||
var onDocumentLoadSuccessRef = (0, import_react.useRef)(onDocumentLoadSuccess); | ||
var onDocumentLoadFailRef = (0, import_react.useRef)(onDocumentLoadFail); | ||
var onPageLoadSuccessRef = (0, import_react.useRef)(onPageLoadSuccess); | ||
var onPageLoadFailRef = (0, import_react.useRef)(onPageLoadFail); | ||
var onPageRenderSuccessRef = (0, import_react.useRef)(onPageRenderSuccess); | ||
var onPageRenderFailRef = (0, import_react.useRef)(onPageRenderFail); | ||
(0, import_react.useEffect)(function() { | ||
onDocumentLoadSuccessRef.current = onDocumentLoadSuccess; | ||
}, [ | ||
onDocumentLoadSuccess | ||
]); | ||
(0, import_react.useEffect)(function() { | ||
onDocumentLoadFailRef.current = onDocumentLoadFail; | ||
}, [ | ||
onDocumentLoadFail | ||
]); | ||
(0, import_react.useEffect)(function() { | ||
onPageLoadSuccessRef.current = onPageLoadSuccess; | ||
}, [ | ||
onPageLoadSuccess | ||
]); | ||
(0, import_react.useEffect)(function() { | ||
onPageLoadFailRef.current = onPageLoadFail; | ||
}, [ | ||
onPageLoadFail | ||
]); | ||
(0, import_react.useEffect)(function() { | ||
onPageRenderSuccessRef.current = onPageRenderSuccess; | ||
}, [ | ||
onPageRenderSuccess | ||
]); | ||
(0, import_react.useEffect)(function() { | ||
onPageRenderFailRef.current = onPageRenderFail; | ||
}, [ | ||
onPageRenderFail | ||
]); | ||
(0, import_react.useEffect)(function() { | ||
pdfjs.GlobalWorkerOptions.workerSrc = workerSrc; | ||
}, [ | ||
workerSrc | ||
]); | ||
(0, import_react.useEffect)(function() { | ||
var config = { | ||
url: file, | ||
withCredentials: withCredentials | ||
}; | ||
if (cMapUrl) { | ||
config.cMapUrl = cMapUrl; | ||
config.cMapPacked = cMapPacked; | ||
} | ||
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 | ||
]); | ||
(0, import_react.useEffect)(function() { | ||
var drawPDF = function(page2) { | ||
var rotation = rotate === 0 ? page2.rotate : page2.rotate + rotate; | ||
var dpRatio = window.devicePixelRatio; | ||
var adjustedScale = scale * dpRatio; | ||
var viewport = page2.getViewport({ | ||
scale: adjustedScale, | ||
rotation: rotation | ||
}); | ||
var canvasEl = canvasRef.current; | ||
if (!canvasEl) { | ||
return; | ||
} | ||
var canvasContext = canvasEl.getContext("2d"); | ||
if (!canvasContext) { | ||
return; | ||
} | ||
canvasEl.style.width = "".concat(viewport.width / dpRatio, "px"); | ||
canvasEl.style.height = "".concat(viewport.height / dpRatio, "px"); | ||
canvasEl.height = viewport.height; | ||
canvasEl.width = viewport.width; | ||
if (renderTask.current) { | ||
renderTask.current.cancel(); | ||
return; | ||
} | ||
renderTask.current = page2.render({ | ||
canvasContext: canvasContext, | ||
viewport: viewport | ||
}); | ||
return renderTask.current.promise.then(function() { | ||
renderTask.current = null; | ||
if (isFunction(onPageRenderSuccessRef.current)) { | ||
onPageRenderSuccessRef.current(page2); | ||
} | ||
}, function(reason) { | ||
renderTask.current = null; | ||
if (reason && reason.name === "RenderingCancelledException") { | ||
drawPDF(page2); | ||
} else if (isFunction(onPageRenderFailRef.current)) { | ||
onPageRenderFailRef.current(); | ||
} | ||
}); | ||
}; | ||
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 | ||
}; | ||
}; | ||
// Annotate the CommonJS export names for ESM import in node: | ||
0 && (module.exports = { | ||
usePdf: usePdf | ||
}); |
{ | ||
"name": "@mikecousins/react-pdf", | ||
"version": "5.5.1", | ||
"version": "6.0.4", | ||
"description": "Simple React component to wrap up PDF.js. The easiest way to render PDFs in your React app.", | ||
@@ -8,22 +8,20 @@ "author": "mikecousins", | ||
"repository": "mikecousins/react-pdf-js", | ||
"main": "dist/index.js", | ||
"module": "dist/react-pdf.esm.js", | ||
"typings": "dist/index.d.ts", | ||
"files": [ | ||
"dist" | ||
], | ||
"main": "./dist/index.mjs", | ||
"types": "./dist/index.d.ts", | ||
"exports": { | ||
".": { | ||
"import": "./dist/index.mjs", | ||
"require": "./dist/index.js" | ||
} | ||
}, | ||
"scripts": { | ||
"start": "tsdx watch", | ||
"build": "tsdx build", | ||
"test": "tsdx test --env=jsdom", | ||
"test:coverage": "CI=true tsdx test --color --coverage", | ||
"lint": "tsdx lint src test", | ||
"netlify": "yarn && yarn build && cd example && yarn && yarn build" | ||
"build": "tsup src/index.tsx --dts --format esm,cjs --target es5", | ||
"lint": "eslint" | ||
}, | ||
"peerDependencies": { | ||
"react": ">=16" | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "tsdx lint src test" | ||
"pre-commit": "npm run lint" | ||
} | ||
@@ -43,18 +41,19 @@ }, | ||
"devDependencies": { | ||
"@testing-library/react": "9.4.0", | ||
"@types/jest": "25.1.2", | ||
"@types/pdfjs-dist": "2.1.3", | ||
"@types/react": "16.9.19", | ||
"@types/react-dom": "16.9.5", | ||
"husky": "4.2.3", | ||
"jest-canvas-mock": "^2.2.0", | ||
"react": "16.12.0", | ||
"react-dom": "16.12.0", | ||
"tsdx": "0.12.3", | ||
"tslib": "1.10.0", | ||
"typescript": "3.7.5" | ||
"@swc/core": "1.2.211", | ||
"@testing-library/react": "13.3.0", | ||
"@types/jest": "28.1.4", | ||
"@types/react": "18.0.15", | ||
"@types/react-dom": "18.0.6", | ||
"@vitejs/plugin-react": "1.3.2", | ||
"jest-canvas-mock": "2.4.0", | ||
"tsup": "6.1.3", | ||
"typescript": "4.7.4" | ||
}, | ||
"dependencies": { | ||
"@bundled-es-modules/pdfjs-dist": "^2.2.228-alpha1" | ||
"pdfjs-dist": "2.14.305" | ||
}, | ||
"peerDependencies": { | ||
"react": "^18.2.0", | ||
"react-dom": "^18.2.0" | ||
} | ||
} |
@@ -9,4 +9,2 @@ # react-pdf-js | ||
[![NPM Downloads](https://img.shields.io/npm/dm/@mikecousins/react-pdf.svg?style=flat-square)](https://www.npmjs.com/package/@mikecousins/react-pdf) | ||
[![Dependency Status](https://david-dm.org/mikecousins/react-pdf-js.svg)](https://david-dm.org/mikecousins/react-pdf-js) | ||
[![devDependency Status](https://david-dm.org/mikecousins/react-pdf-js/dev-status.svg)](https://david-dm.org/mikecousins/react-pdf-js#info=devDependencies) | ||
[![Netlify Status](https://api.netlify.com/api/v1/badges/4ce8e5b5-16ca-4942-8c47-095debbc4693/deploy-status)](https://app.netlify.com/sites/pdf/deploys) | ||
@@ -161,75 +159,4 @@ [![codecov](https://codecov.io/gh/mikecousins/react-pdf-js/branch/master/graph/badge.svg)](https://codecov.io/gh/mikecousins/react-pdf-js) | ||
## `Pdf` component | ||
You can also use the `Pdf` component (which uses `usePdf` hook internally): | ||
```js | ||
import React, { useState } from 'react'; | ||
import Pdf from '@mikecousins/react-pdf'; | ||
const MyPdfViewer = () => { | ||
const [page, setPage] = useState(1); | ||
return <Pdf file="basic.33e35a62.pdf" page={page} />; | ||
}; | ||
``` | ||
Or if you want to use pdf's data (e.g. to render pagination): | ||
```js | ||
import React, { useState } from 'react'; | ||
import Pdf from '@mikecousins/react-pdf'; | ||
const MyPdfViewer = () => { | ||
const [page, setPage] = useState(1); | ||
return ( | ||
<Pdf file="basic.33e35a62.pdf" page={page}> | ||
{({ pdfDocument, pdfPage, canvas }) => ( | ||
<> | ||
{!pdfDocument && <span>Loading...</span>} | ||
{canvas} | ||
{Boolean(pdfDocument && pdfDocument.numPages) && ( | ||
<nav> | ||
<ul className="pager"> | ||
<li className="previous"> | ||
<button | ||
disabled={page === 1} | ||
onClick={() => setPage(page - 1)} | ||
> | ||
Previous | ||
</button> | ||
</li> | ||
<li className="next"> | ||
<button | ||
disabled={page === pdfDocument.numPages} | ||
onClick={() => setPage(page + 1)} | ||
> | ||
Next | ||
</button> | ||
</li> | ||
</ul> | ||
</nav> | ||
)} | ||
</> | ||
)} | ||
</Pdf> | ||
); | ||
}; | ||
``` | ||
Notice that in the second example, you are responsible for rendering the canvas element into the DOM. | ||
## Props | ||
`Pdf` component accepts all the props that `usePdf` hook do, with exception of `canvasRef` (the component renders it by itself). | ||
Additionaly, the component accepts: | ||
### children | ||
A function that receives data returned by `usePdf` hook with addition of canvas element. You are responsible for rendering that element into the DOM if you choose to pass children prop. | ||
# License | ||
MIT © [mikecousins](https://github.com/mikecousins) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Unpublished package
Supply chain riskPackage version was not found on the registry. It may exist on a different registry and need to be configured to pull from that registry.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
9
1
1
21228
3
5
412
161
+ Addedpdfjs-dist@2.14.305
+ Addeddommatrix@1.0.3(transitive)
+ Addedjs-tokens@4.0.0(transitive)
+ Addedloose-envify@1.4.0(transitive)
+ Addedpdfjs-dist@2.14.305(transitive)
+ Addedreact@18.3.1(transitive)
+ Addedreact-dom@18.3.1(transitive)
+ Addedscheduler@0.23.2(transitive)
+ Addedweb-streams-polyfill@3.3.3(transitive)
- Removed@bundled-es-modules/pdfjs-dist@2.16.106(transitive)
- Removedreact@19.0.0(transitive)