npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@kunwarji/react-to-pdf

v1.3.1

Published

React to pdf converts the JSX of react component to pdf

Downloads

141

Readme

ReactToPDF

React to pdf helps one create a PDF file for any React component. There is already one npm pkg for acheiving this functionality, so why this one you ask?

  1. Some of them were failing with newer version of react.
  2. Wanted to add certain more feature which is already being provided by jsPdf and html2canvas, but were not there in other packages.

Installing

npm i @kunwarji/react-to-pdf

Usage

Creating reference for passing it down to ReactToPDF:-

<div ref={refVar}>
  Something inside the component being rendered. Bazinga!
</div>

Calling component:-

  <ReactToPDF element={refVar}>
    {(toPdf) => (
      <button type="button" onClick={toPdf}>
        Click me
      </button>
      )
    }
  </ReactToPDF>

Reference passed to an element prop.

Other props

  • scale - The scale to use for rendering. (Optional - Default value is 1)
  • cropX - Crops canvas x-coordinate. (Optional - Default value is 0)
  • cropY - Crop canvas y-coordinate. (Optional - Default value is 0)
  • printStart - Array of [x, y] coordinate from where one wants the element to appear from on canvas. (Default - [0, 0])
  • fileName - Name of the pdf file when it is downloaded. (Default - PDF_FILE)
  • format - Rarely needed, but the image used for printing whether it should be JPEG, PNG, WEBP.
  • compression - compression of the generated JPEG, can have the values 'NONE', 'FAST', 'MEDIUM' and 'SLOW. (Optional - Default value is NONE)
  • backgroundColor - Canvas background color, if none is specified in DOM. Set null for transparent. (Optional - Default value is null)
  • logging - Enable logging for debug purposes. (Optional - Default value is false)
  • modifyFn - Callback function which is called when the Document has been cloned for rendering, can be used to modify the contents that will be rendered without affecting the original source document. (Optional)
  • imageTimeout - Timeout for loading an image (in milliseconds). (Optional - Default value is 0)
  • width - The width of the canvas. (Optional)
  • height - The height of the canvas. (Optional)

Working

Basically it is using html2canvas (to take snap after traversing the DOM) and jsPDF (to generate pdf).