Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
react-pdf
Advanced tools
The react-pdf package is a React component library that enables you to display PDF documents in your React applications. It provides a set of components that allow you to render PDF files as well as interact with them, such as zooming, paging, and rendering individual pages.
Displaying a PDF document
This code sample demonstrates how to display a single page from a PDF document. The Document component takes a file prop which can be a URL, a file (as in a Blob or File object), or an ArrayBuffer. The Page component inside it renders the specified page number.
import { Document, Page } from 'react-pdf';
function MyApp() {
return (
<Document file="somefile.pdf">
<Page pageNumber={1} />
</Document>
);
}
Custom rendering of pages
This code sample shows how to use a custom rendering mode for pages. The renderMode prop can be set to 'canvas', 'svg', or 'none'. The example also demonstrates how to set up the PDF.js worker script, which is required for parsing and rendering PDFs.
import { Document, Page, pdfjs } from 'react-pdf';
pdfjs.GlobalWorkerOptions.workerSrc = '//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.js';
function MyApp() {
return (
<Document file="somefile.pdf" renderMode="canvas">
<Page pageNumber={1} />
</Document>
);
}
Handling loading states
This code sample illustrates how to handle loading states when working with PDF documents. The onLoadSuccess and onLoadError callbacks can be used to manage the document's loading state and handle any errors that occur.
import { Document, Page } from 'react-pdf';
function MyApp() {
return (
<Document
file="somefile.pdf"
onLoadSuccess={() => console.log('Loaded successfully!')}
onLoadError={(error) => console.error('Error while loading document!', error.message)}
>
<Page pageNumber={1} />
</Document>
);
}
Navigating between pages
This code sample demonstrates how to navigate between pages in a PDF document. It uses React's useState hook to keep track of the current page number and provides buttons to go to the previous or next page.
import { Document, Page } from 'react-pdf';
import { useState } from 'react';
function MyApp() {
const [pageNumber, setPageNumber] = useState(1);
return (
<div>
<Document file="somefile.pdf">
<Page pageNumber={pageNumber} />
</Document>
<button onClick={() => setPageNumber(pageNumber - 1)}>Previous</button>
<button onClick={() => setPageNumber(pageNumber + 1)}>Next</button>
</div>
);
}
This is the base library that react-pdf wraps around. It is Mozilla's PDF.js library, which is a general-purpose, web standards-based platform for parsing and rendering PDFs.
pdf-lib is a package for creating and modifying PDF documents in any JavaScript environment. Unlike react-pdf, which is focused on rendering PDFs in React applications, pdf-lib allows for more extensive manipulation of PDF files, such as adding text, images, and pages.
react-pdf-viewer is another React library for displaying PDF documents. It provides a set of React components similar to react-pdf but with a different API and additional features like searching, custom toolbar, and more.
A component for showing a pdf page using pdf.js.
Install with npm install react-pdf
Use in your app:
var PDF = require('react-pdf');
var MyApp = React.createClass({
render: function() {
return <PDF file="somefile.pdf" page="2" />
}
});
Check the example-directory of this repository for a full working example
Unfortunately pdf.js isn't too friendly for commonjs environments so react-pdf
assumes a global PDFJS
variable, see the example directory of this repository
for an example.
The MIT License
Niklas Närhinen niklas@narhinen.net
FAQs
Display PDFs in your React app as easily as if they were images.
The npm package react-pdf receives a total of 596,268 weekly downloads. As such, react-pdf popularity was classified as popular.
We found that react-pdf demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.