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.0-alpha.1

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

173

dist/index.js

@@ -0,8 +1,169 @@

"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.0-alpha.1",
"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,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"
"@testing-library/react": "13.4.0",
"@types/jest": "29.0.3",
"@types/react": "18.0.20",
"@types/react-dom": "18.0.6",
"@vitejs/plugin-react": "2.1.0",
"jest-canvas-mock": "2.4.0",
"pdfjs-dist": "2.16.105",
"tslib": "2.4.0",
"tsup": "6.2.3",
"typescript": "4.8.3"
},
"dependencies": {
"@bundled-es-modules/pdfjs-dist": "^2.2.228-alpha1"
}
"peerDependencies": {
"pdfjs-dist": "2.16.105",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"bundleDependencies": true
}
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