@mikecousins/react-pdf
Advanced tools
Comparing version 5.5.1 to 6.0.0
@@ -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 }; |
@@ -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.0", | ||
"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" | ||
} | ||
} |
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
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
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
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 2 instances in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
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
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
849771
8
14996
3
7
1
0
3
13
+ 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)