
Security News
vlt Launches "reproduce": A New Tool Challenging the Limits of Package Provenance
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
react-to-pdf
Advanced tools
Easily create PDF documents from React components.
# Yarn
yarn add react-to-pdf
# NPM
npm install --save react-to-pdf
Using usePDF
hook
import { usePDF } from 'react-to-pdf';
const Component = () => {
const { toPDF, targetRef } = usePDF({filename: 'page.pdf'});
return (
<div>
<button onClick={() => toPDF()}>Download PDF</button>
<div ref={targetRef}>
Content to be generated to PDF
</div>
</div>
)
}
Using default function
import { useRef } from 'react';
import generatePDF from 'react-to-pdf';
const Component = () => {
const targetRef = useRef();
return (
<div>
<button onClick={() => generatePDF(targetRef, {filename: 'page.pdf'})}>Download PDF</button>
<div ref={targetRef}>
Content to be included in the PDF
</div>
</div>
)
}
Advanced options
import generatePDF, { Resolution, Margin } from 'react-to-pdf';
const options = {
// default is `save`
method: 'open',
// default is Resolution.MEDIUM = 3, which should be enough, higher values
// increases the image quality but also the size of the PDF, so be careful
// using values higher than 10 when having multiple pages generated, it
// might cause the page to crash or hang.
resolution: Resolution.HIGH,
page: {
// margin is in MM, default is Margin.NONE = 0
margin: Margin.SMALL,
// default is 'A4'
format: 'letter',
// default is 'portrait'
orientation: 'landscape',
},
canvas: {
// default is 'image/jpeg' for better size performance
mimeType: 'image/png',
qualityRatio: 1
},
// Customize any value passed to the jsPDF instance and html2canvas
// function. You probably will not need this and things can break,
// so use with caution.
overrides: {
// see https://artskydj.github.io/jsPDF/docs/jsPDF.html for more options
pdf: {
compress: true
},
// see https://html2canvas.hertzen.com/configuration for more options
canvas: {
useCORS: true
}
},
};
// you can use a function to return the target element besides using React refs
const getTargetElement = () => document.getElementById('content-id');
const Component = () => {
return (
<div>
<button onClick={() => generatePDF(getTargetElement, options)}>Generate PDF</button>
<div id="content-id">
Content to be generated to PDF
</div>
</div>
);
}
FAQs
Create PDF documents from React Components
The npm package react-to-pdf receives a total of 44,381 weekly downloads. As such, react-to-pdf popularity was classified as popular.
We found that react-to-pdf demonstrated a not healthy version release cadence and project activity because the last version was released 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
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
Research
Security News
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.