Installations
npm install @mikecousins/react-pdf
Developer Guide
Typescript
Yes
Module System
ESM
Node Version
22.1.0
NPM Version
10.7.0
Score
62.2
Supply Chain
93.7
Quality
77.2
Maintenance
100
Vulnerability
97
License
Releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (95.18%)
HTML (2.66%)
JavaScript (1.77%)
CSS (0.39%)
Developer
Download Statistics
Total Downloads
1,393,717
Last Day
496
Last Week
6,007
Last Month
38,189
Last Year
364,868
GitHub Statistics
734 Stars
553 Commits
150 Forks
13 Watching
13 Branches
29 Contributors
Bundle Size
2.18 kB
Minified
947.00 B
Minified + Gzipped
Package Meta Information
Latest Version
7.1.0
Package Id
@mikecousins/react-pdf@7.1.0
Unpacked Size
557.59 kB
Size
120.93 kB
File Count
9
NPM Version
10.7.0
Node Version
22.1.0
Publised On
29 May 2024
Total Downloads
Cumulative downloads
Total Downloads
1,393,717
Last day
-75.5%
496
Compared to previous day
Last week
-37.8%
6,007
Compared to previous week
Last month
11%
38,189
Compared to previous month
Last year
12.3%
364,868
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
react-pdf-js
react-pdf-js
provides a component for rendering PDF documents using PDF.js.
Demo
Usage
Install with yarn add @mikecousins/react-pdf pdfjs-dist
or npm install @mikecousins/react-pdf pdfjs-dist
usePdf
hook
Use the hook in your app (showing some basic pagination as well):
1import React, { useState, useRef } from 'react'; 2import { usePdf } from '@mikecousins/react-pdf'; 3 4const MyPdfViewer = () => { 5 const [page, setPage] = useState(1); 6 const canvasRef = useRef(null); 7 8 const { pdfDocument, pdfPage } = usePdf({ 9 file: 'test.pdf', 10 page, 11 canvasRef, 12 }); 13 14 return ( 15 <div> 16 {!pdfDocument && <span>Loading...</span>} 17 <canvas ref={canvasRef} /> 18 {Boolean(pdfDocument && pdfDocument.numPages) && ( 19 <nav> 20 <ul className="pager"> 21 <li className="previous"> 22 <button disabled={page === 1} onClick={() => setPage(page - 1)}> 23 Previous 24 </button> 25 </li> 26 <li className="next"> 27 <button 28 disabled={page === pdfDocument.numPages} 29 onClick={() => setPage(page + 1)} 30 > 31 Next 32 </button> 33 </li> 34 </ul> 35 </nav> 36 )} 37 </div> 38 ); 39};
Props
When you call usePdf you'll want to pass in a subset of these props, like this:
const { pdfDocument, pdfPage } = usePdf({ canvasRef, file: 'https://example.com/test.pdf', page });
canvasRef
A reference to the canvas element. Create with:
const canvasRef = useRef(null);
and then render it like:
<canvas ref={canvasRef} />
and then pass it into usePdf.
file
URL of the PDF file.
onDocumentLoadSuccess
Allows you to specify a callback that is called when the PDF document data will be fully loaded. Callback is called with PDFDocumentProxy as an only argument.
onDocumentLoadFail
Allows you to specify a callback that is called after an error occurred during PDF document data loading.
onPageLoadSuccess
Allows you to specify a callback that is called when the PDF page data will be fully loaded. Callback is called with PDFPageProxy as an only argument.
onPageLoadFail
Allows you to specify a callback that is called after an error occurred during PDF page data loading.
onPageRenderSuccess
Allows you to specify a callback that is called when the PDF page will be fully rendered into the DOM. Callback is called with PDFPageProxy as an only argument.
onPageRenderFail
Allows you to specify a callback that is called after an error occurred during PDF page rendering.
page
Specify the page that you want to display. Default = 1,
scale
Allows you to scale the PDF. Default = 1.
rotate
Allows you to rotate the PDF. Number is in degrees. Default = 0.
cMapUrl
Allows you to specify a cmap url. Default = '../node_modules/pdfjs-dist/cmaps/'.
cMapPacked
Allows you to specify whether the cmaps are packed or not. Default = false.
workerSrc
Allows you to specify a custom pdf worker url. Default = '//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.js'.
withCredentials
Allows you to add the withCredentials flag. Default = false.
Returned values
pdfDocument
pdfjs
's PDFDocumentProxy
object.
This can be undefined if document has not been loaded yet.
pdfPage
pdfjs
's PDFPageProxy
object
This can be undefined if page has not been loaded yet.
License
MIT © mikecousins
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
SAST tool is run on all commits
Details
- Info: all commits (16) are checked with a SAST tool
Reason
6 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-mwcw-c2x4-8c55
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-64vr-g452-qvp3
- Warn: Project is vulnerable to: GHSA-9cwx-2883-4wfx
Reason
3 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 2
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/npmpublish.yml:1
- Info: no jobLevel write permissions found
Reason
Found 0/14 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/npmpublish.yml:15: update your workflow using https://app.stepsecurity.io/secureworkflow/mikecousins/react-pdf-js/npmpublish.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/npmpublish.yml:16: update your workflow using https://app.stepsecurity.io/secureworkflow/mikecousins/react-pdf-js/npmpublish.yml/main?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/npmpublish.yml:24: update your workflow using https://app.stepsecurity.io/secureworkflow/mikecousins/react-pdf-js/npmpublish.yml/main?enable=pin
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 third-party GitHubAction dependencies pinned
Score
4.1
/10
Last Scanned on 2024-12-16
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More