Nabil Mehtab
Nabil Mehtab

Reputation: 37

react-to-pdf between two components

So I am using react-to-pdf to print Html tags to PDF the button is on one component and text is on another, I don't seem to have much knowledge as to how to make the button print index.js as pdf. I am pretty sure that something is wrong in ref and imports

button.js

import React, { Component } from "react";

import Pdf from "react-to-pdf";
const ref = React.createRef();
class Button extends Component {
  render() {
    return (
      <React.Fragment>
        <Pdf targetRef={ref} filename="code-example.pdf">
          {({ toPdf }) => <button onClick={toPdf}>Generate Pdf</button>}
        </Pdf>
      </React.Fragment>
    );
  }
}

export default Button;

index.js

import React, { Component } from "react";
import "./styles.css";
import Button from "./button";
const ref = React.createRef();
class Index extends Component {
  render() {
    return (
      <React.Fragment>
        <div className="App">
          <div ref={ref}>
            <h1>Hello CodeSandbox</h1>
            <h2>Start editing to see some magic happen!</h2>
          </div>
        </div>
      </React.Fragment>
    );
  }
}

export default Index;

ReactDOM.render(<Button />, document.getElementById("top"));
const rootElement = document.getElementById("root");
ReactDOM.render(<Index />, rootElement);

Upvotes: 0

Views: 5111

Answers (2)

Drew Reese
Drew Reese

Reputation: 203373

You can expose out props from the button to pass in the ref to the component you want saved to PDF. Here's a demo of a PDF button with targetRef and fileName props.

PDFButton.jsx

import React from "react";
import PropTypes from "prop-types";
import Pdf from "react-to-pdf";

// expose a targetRef prop and filename
const PDFButton = ({ children, filename, targetRef }) => (
  <Pdf targetRef={targetRef} filename={filename}>
    {({ toPdf }) => <button onClick={toPdf}>{children}</button>}
  </Pdf>
);

PDFButton.propTypes = {
  filename: PropTypes.string,
  targetRef: PropTypes.any
};

PDFButton.defaultProps = {
  filename: "code-example.pdf"
};

export default PDFButton;

App.js

import React, { createRef } from "react";
import PDFButton from "./PDFButton";
import "./styles.css";

export default function App() {
  const pdfRef = createRef(); // create a single ref to pass to button
  return (
    <div ref={pdfRef} className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
      <p>This is a demo how to create a save to PDF button</p>
      <PDFButton
        fileName="awesomePDFButtonDemo.pdf"
        targetRef={pdfRef}
      >
        Save to PDF!
      </PDFButton>
    </div>
  );
}

Edit PDFButton Demo

Upvotes: 0

Babak Yaghoobi
Babak Yaghoobi

Reputation: 1985

Use like this:

import React from "react";
import ReactDOM from "react-dom";
import Pdf from "react-to-pdf";

const Button = React.forwardRef((props, ref) => {
  return (
    <React.Fragment>
      <Pdf targetRef={ref} filename="code-example.pdf">
        {({ toPdf }) => <button onClick={toPdf}>Generate Pdf</button>}
      </Pdf>
    </React.Fragment>
  );
});

const App = () => {
  let docToPrint = React.createRef();

  return (
    <div>
      <div>
        <Button ref={docToPrint} />
      </div>
      <React.Fragment>
        <div className="App">
          <div
            ref={docToPrint}
            style={{
              borderRadius: "5px",
              width: "600px",
              height: "400px",
              margin: "0 auto",
              padding: "10mm"
            }}
          >
            <h1>Hello CodeSandbox</h1>
            <h2>Start editing to see some magic happen!</h2>
          </div>
        </div>
      </React.Fragment>
    </div>
  );
};

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Answer output: HERE

Upvotes: 1

Related Questions