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.1 to 6.0.2

dist/index.mjs

25

dist/index.d.ts

@@ -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 };

161

dist/index.js

@@ -0,8 +1,157 @@

"use strict";
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 = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
'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')
// src/index.tsx
var src_exports = {};
__export(src_exports, {
usePdf: () => 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 = ({
canvasRef,
file,
onDocumentLoadSuccess,
onDocumentLoadFail,
onPageLoadSuccess,
onPageLoadFail,
onPageRenderSuccess,
onPageRenderFail,
scale = 1,
rotate = 0,
page = 1,
cMapUrl,
cMapPacked,
workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.js`,
withCredentials = false
}) => {
const [pdfDocument, setPdfDocument] = (0, import_react.useState)();
const [pdfPage, setPdfPage] = (0, import_react.useState)();
const renderTask = (0, import_react.useRef)(null);
const onDocumentLoadSuccessRef = (0, import_react.useRef)(onDocumentLoadSuccess);
const onDocumentLoadFailRef = (0, import_react.useRef)(onDocumentLoadFail);
const onPageLoadSuccessRef = (0, import_react.useRef)(onPageLoadSuccess);
const onPageLoadFailRef = (0, import_react.useRef)(onPageLoadFail);
const onPageRenderSuccessRef = (0, import_react.useRef)(onPageRenderSuccess);
const onPageRenderFailRef = (0, import_react.useRef)(onPageRenderFail);
(0, import_react.useEffect)(() => {
onDocumentLoadSuccessRef.current = onDocumentLoadSuccess;
}, [onDocumentLoadSuccess]);
(0, import_react.useEffect)(() => {
onDocumentLoadFailRef.current = onDocumentLoadFail;
}, [onDocumentLoadFail]);
(0, import_react.useEffect)(() => {
onPageLoadSuccessRef.current = onPageLoadSuccess;
}, [onPageLoadSuccess]);
(0, import_react.useEffect)(() => {
onPageLoadFailRef.current = onPageLoadFail;
}, [onPageLoadFail]);
(0, import_react.useEffect)(() => {
onPageRenderSuccessRef.current = onPageRenderSuccess;
}, [onPageRenderSuccess]);
(0, import_react.useEffect)(() => {
onPageRenderFailRef.current = onPageRenderFail;
}, [onPageRenderFail]);
(0, import_react.useEffect)(() => {
pdfjs.GlobalWorkerOptions.workerSrc = workerSrc;
}, [workerSrc]);
(0, import_react.useEffect)(() => {
const config = { url: file, withCredentials };
if (cMapUrl) {
config.cMapUrl = cMapUrl;
config.cMapPacked = cMapPacked;
}
pdfjs.getDocument(config).promise.then((loadedPdfDocument) => {
setPdfDocument(loadedPdfDocument);
if (isFunction(onDocumentLoadSuccessRef.current)) {
onDocumentLoadSuccessRef.current(loadedPdfDocument);
}
}, () => {
if (isFunction(onDocumentLoadFailRef.current)) {
onDocumentLoadFailRef.current();
}
});
}, [file, withCredentials, cMapUrl, cMapPacked]);
(0, import_react.useEffect)(() => {
const drawPDF = (page2) => {
const rotation = rotate === 0 ? page2.rotate : page2.rotate + rotate;
const dpRatio = window.devicePixelRatio;
const adjustedScale = scale * dpRatio;
const viewport = page2.getViewport({ scale: adjustedScale, rotation });
const canvasEl = canvasRef.current;
if (!canvasEl) {
return;
}
const canvasContext = canvasEl.getContext("2d");
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 (renderTask.current) {
renderTask.current.cancel();
return;
}
renderTask.current = page2.render({
canvasContext,
viewport
});
return renderTask.current.promise.then(() => {
renderTask.current = null;
if (isFunction(onPageRenderSuccessRef.current)) {
onPageRenderSuccessRef.current(page2);
}
}, (reason) => {
renderTask.current = null;
if (reason && reason.name === "RenderingCancelledException") {
drawPDF(page2);
} else if (isFunction(onPageRenderFailRef.current)) {
onPageRenderFailRef.current();
}
});
};
if (pdfDocument) {
pdfDocument.getPage(page).then((loadedPdfPage) => {
setPdfPage(loadedPdfPage);
if (isFunction(onPageLoadSuccessRef.current)) {
onPageLoadSuccessRef.current(loadedPdfPage);
}
drawPDF(loadedPdfPage);
}, () => {
if (isFunction(onPageLoadFailRef.current)) {
onPageLoadFailRef.current();
}
});
}
}, [canvasRef, page, pdfDocument, rotate, scale]);
return { pdfDocument, pdfPage };
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
usePdf
});
{
"name": "@mikecousins/react-pdf",
"version": "5.5.1",
"version": "6.0.2",
"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",
"lint": "eslint"
},
"peerDependencies": {
"react": ">=16"
},
"husky": {
"hooks": {
"pre-commit": "tsdx lint src test"
"pre-commit": "npm run lint"
}

@@ -43,18 +41,18 @@ },

"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"
"@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)
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