GithubHelp home page GithubHelp logo

kunwarvsuryavanshi / reacttopdf Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 0.0 313 KB

React to pdf genrates PDF for any react component.

Home Page: https://www.npmjs.com/package/@kunwarji/react-to-pdf

License: MIT License

HTML 26.48% JavaScript 67.89% CSS 5.63%
npm react pdf-generation react-pdf react-to-pdf

reacttopdf's Introduction

ReactToPDF

So this 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 that is being provided by jsPdf and html2canvas.

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 - Defaults 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'
  • backgroundColor - Canvas background color, if none is specified in DOM. Set null for transparent
  • logging - Enable logging for debug purposes
  • 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.
  • imageTimeout - Timeout for loading an image (in milliseconds). Set to 0 to disable timeout.
  • width - The width of the canvas.
  • height - The height of the canvas.

Working

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

reacttopdf's People

Contributors

kunwarvsuryavanshi avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

reacttopdf's Issues

Next.js: Module not found -> prop-types

Fist of all, thanks for your work!

I was trying out this package, but I'm having a hard time. I use Next 13.0.4. and I get the following message.

Screenshot 2022-11-24 at 11 00 20

This can be easily fixed by running

npm install prop-types

But it would be better to have that installed as a dev-dependency when installing your package. Unfortunately I do not know, how to fix this myself otherwise I would create a PR...

I tested and confirmed this behaviour on Next 12.2.0. But on React 18.2.0 (by create-react-app) it works.

Ps. In ReactToPDF.jsx on line 46 seems to be a typo, see Link.

ReactToPDF.propTyppes = { -> ReactToPDF.propTypes = {

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.