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

react-component-export-img

v1.0.7

Published

export component as jpeg, png, pdf

Downloads

7

Readme

react-component-export-image

Demo Codesandbox

Introduction

  • Export component as jpeg, png or pdf
  • Each export expect a {React.RefObject} node, optional fileName, and optional html2CanvasOptions object which you wish to pass it to html2Canvas
  • exportComponentAsPDF also accepts an optional pdfOptions object with these optional fields {w, h, x, y, unit, orientation, pdfFormat}
w = 100 (Width in pixels - defaults to the width of the element)

h = 50 (Height in pixels - defaults to the height of the element)

x = 0 (X Coordinate in pixels against left edge of the page - defaults to 0)

y = 0 (Y Coordinate in pixels against left edge of the page - defaults to 0)

unit = 'px' (Measurement unit (base unit) to be used when coordinates are specified.
Possible values are "pt" (points), "mm", "cm", "m", "in" or "px". - defaults to 'mm')
    - if you are trying to get the pdf to fill up the exact space, try setting unit to "px"

orientation = 'p' (portrait) OR 'l' (landscape) - defaults: 
    - landscape if width > height
     or 
    - portrait if height > width

The format of the PDF. Can be:

    a0 - a10
    b0 - b10
    c0 - c10
    dl
    letter
    government-letter
    legal
    junior-legal
    ledger
    tabloid
    credit-card

    Default is "a4". If you want to use your own format just pass instead of one of the above predefined formats the size as an number-array, e.g. [595.28, 841.89]

How to Upgrade

The previous way of using an export looked like this:

exportComponentAsJPEG(node, fileName, type, backgroundColor, options)

The new way: pass node & an optional object with only the fields you need.

  • backgroundColor is no longer accepted in this main object, but is accepted in the "html2CanvasOptions" object, which is passed directly to html2canvas
  • type is no longer accepted - JPG will always produce JPG, PNG => PNG, PDF => PDF

exportComponentAsJPEG(node, {fileName, html2CanvasOptions})

  • exportComponentAsPDF also accepts an additional pdfOptions object (see introduction)

exportComponentAsPDF(node, {fileName, html2CanvasOptions, pdfOptions})

Code Samples

Component

import { exportComponentAsJPEG, exportComponentAsPDF, exportComponentAsPNG } from 'react-component-export-image';
import React from 'react';

class ComponentToPrint extends React.Component {
 render() {
   return <div>Hello World</div>;
 }
}
export default class MyComponent extends React.Component {
 constructor(props) {
   super(props);
   this.componentRef = React.createRef();
 }

 render() {
   return (
     <React.Fragment>
       <ComponentToPrint ref={this.componentRef} />
       <button onClick={() => exportComponentAsJPEG(this.componentRef)}>
         Export As JPEG
       </button>
       <button onClick={() => exportComponentAsPDF(this.componentRef)}>
         Export As PDF
       </button>
       <button onClick={() => exportComponentAsPNG(this.componentRef)}>
         Export As PNG
       </button>
     </React.Fragment>
   );
 }
}

Function component

import { exportComponentAsJPEG, exportComponentAsPDF, exportComponentAsPNG } from 'react-component-export-image';
import React, { useRef } from 'react';

const ComponentToPrint = React.forwardRef((props, ref) => (
  <div ref={ref}>Hello World</div>
));

const MyComponent = () => {
  const componentRef = useRef();

  return (
    <React.Fragment>
      <ComponentToPrint ref={componentRef} />
      <button onClick={() => exportComponentAsJPEG(componentRef)}>
        Export As JPEG
      </button>
      <button onClick={() => exportComponentAsPDF(componentRef)}>
        Export As PDF
      </button>
      <button onClick={() => exportComponentAsPNG(componentRef)}>
        Export As PNG
      </button>
    </React.Fragment>
  );
};

export default MyComponent;

Installation

npm i react-component-export-image
or
yarn add react-component-export-image