Skip to content

Print React components in the browser. Supports Chrome, Safari, Firefox and EDGE

License

Notifications You must be signed in to change notification settings

eleonora-guidi/react-to-print

 
 

Repository files navigation

ReactToPrint - Print React components in the browser

Build Status NPM Downloads dependencies Status npm version

So you've created a React component but would love to give end users the ability to print out the contents of that component. This package aims to solve that by popping up a new print window with CSS styles copied over as well.

Install

npm install react-to-print --save-dev

Demo

Edit react-to-print

Example

Calling from class components

import React from 'react';
import ReactToPrint from 'react-to-print';

class ComponentToPrint extends React.Component {
  render() {
    return (
      <table>
        <thead>
          <th>column 1</th>
          <th>column 2</th>
          <th>column 3</th>
        </thead>
        <tbody>
          <tr>
            <td>data 1</td>
            <td>data 2</td>
            <td>data 3</td>
          </tr>
          <tr>
            <td>data 1</td>
            <td>data 2</td>
            <td>data 3</td>
          </tr>
          <tr>
            <td>data 1</td>
            <td>data 2</td>
            <td>data 3</td>
          </tr>
        </tbody>
      </table>
    );
  }
}

class Example extends React.Component {
  render() {
    return (
      <div>
        <ReactToPrint
          trigger={() => <a href="#">Print this out!</a>}
          content={() => this.componentRef}
        />
        <ComponentToPrint ref={el => (this.componentRef = el)} />
      </div>
    );
  }
}

Calling from functional components with hooks

import React, { useRef } from 'react';
import ReactToPrint from 'react-to-print';

class ComponentToPrint extends React.Component {
  render() {
    return (
      <table>
        <thead>
          <th>column 1</th>
          <th>column 2</th>
          <th>column 3</th>
        </thead>
        <tbody>
          <tr>
            <td>data 1</td>
            <td>data 2</td>
            <td>data 3</td>
          </tr>
          <tr>
            <td>data 1</td>
            <td>data 2</td>
            <td>data 3</td>
          </tr>
          <tr>
            <td>data 1</td>
            <td>data 2</td>
            <td>data 3</td>
          </tr>
        </tbody>
      </table>
    );
  }
}

const Example = () => {
  const componentRef = useRef();
  return (
    <div>
      <ReactToPrint
        trigger={() => <button>Print this out!</button>}
        content={() => componentRef.current}
      />
      <ComponentToPrint ref={componentRef} />
    </div>
  );
};

API

<ReactToPrint />

The component accepts the following props:

Name Type Description
trigger function A function that returns a React Component or HTML element
content function A function that returns a component reference value. The content of this reference value is then used for print
copyStyles boolean Copies all <style> and <link type="stylesheet" /> from inside the parent window into the print window. (default: true)
onBeforePrint function A callback function that triggers before print
onAfterPrint function A callback function that triggers after print
closeAfterPrint boolean Close the print window after action
pageStyle string Override default print window styling
bodyClass string Optional class to pass to the print window body

FAQ

Why does react-to-print skip <link rel="stylesheet" href=""> tags? <link>s with empty href attributes are INVALID HTML. In addition, they can cause all sorts of undesirable behavior. For example, many browsers - including modern ones, when presented with <link href=""> will attempt to load the current page. Some even attempt to load the current page's parent directory.

About

Print React components in the browser. Supports Chrome, Safari, Firefox and EDGE

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 81.8%
  • JavaScript 14.0%
  • HTML 3.5%
  • CSS 0.7%