Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

React to pdf

const ref = React.createRef();

<div>
    <ReactToPdf targetRef={ref} filename="div-blue.pdf">
        {({toPdf}) => (
            <button onClick={toPdf}>Generate pdf</button>
        )}
    </ReactToPdf>
    <div style={{width: 500, height: 500, background: 'blue'}} ref={ref}/>
</div>
Comment

react convert table to pdf

import React from 'react';
import jsPDF from "jspdf";
import "jspdf-autotable";
import './App.css';

class App extends React.Component {

  constructor() {
    super();
    this.state = {
      people: [
        { name: "Keanu Reeves", profession: "Actor" },
        { name: "Lionel Messi", profession: "Football Player" },
        { name: "Cristiano Ronaldo", profession: "Football Player" },
        { name: "Jack Nicklaus", profession: "Golf Player" },
      ]
    }
  }

  exportPDF = () => {
    const unit = "pt";
    const size = "A4"; // Use A1, A2, A3 or A4
    const orientation = "portrait"; // portrait or landscape

    const marginLeft = 40;
    const doc = new jsPDF(orientation, unit, size);

    doc.setFontSize(15);

    const title = "My Awesome Report";
    const headers = [["NAME", "PROFESSION"]];

    const data = this.state.people.map(elt=> [elt.name, elt.profession]);

    let content = {
      startY: 50,
      head: headers,
      body: data
    };

    doc.text(title, marginLeft, 40);
    doc.autoTable(content);
    doc.save("report.pdf")
  }

  render() {
    return (
      <div>
        <button onClick={() => this.exportPDF()}>Generate Report</button>
      </div>
    );
  }
}

export default App;
Comment

PREVIOUS NEXT
Code Example
Javascript :: js string to charcode array 
Javascript :: remove last element from an array 
Javascript :: generate string from regex javascript 
Javascript :: The JavaScript Apply() Function 
Javascript :: react header 
Javascript :: update password using comparePassword() Method 
Javascript :: react native on refresh change color flat list 
Javascript :: javascript remove item from url 
Javascript :: django csrf failed ajax 
Javascript :: how to check url with port is valid or not regex javascript 
Javascript :: modulenamemapper not working 
Javascript :: programmatically create a custom cron job drupal 7 
Javascript :: promise catch javascript 
Javascript :: getusermedia close stream 
Javascript :: emergency food 
Javascript :: javascript remove elements from array with value 
Javascript :: sort array of objects based on another array javascript 
Javascript :: Javascript swap old and new method 
Javascript :: javascript make title blink 
Javascript :: how to set image width and height dynamically in javascript 
Javascript :: vue slice words 
Javascript :: Half or Right Triangle star pattern in JavaScript 
Javascript :: js currency converter 
Javascript :: rest parameters 
Javascript :: google scripts string split 
Javascript :: sort array javascript 
Javascript :: angular blank page no errors 
Javascript :: How to remove CSS file using JavaScript 
Javascript :: object method in javascript 
Javascript :: how to navigate with navintems for react-bootstrap without refreshing the whole page 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =