Tamjid
Tamjid

Reputation: 5566

doc.fromHTML is not a function

I'm trying to use jspdf with my react app and in the docs I and in the types for jspdf I can see there is a function called fromHTML. But when I try to call the funciton I get the following error:

doc.fromHTML is not a function

Can someone please help me?

import { jsPDF } from 'jspdf'

const Prints = () => (
    <div>
      <h3>Time & Materials Statement of Work (SOW)</h3>
      <h4>General Information</h4>
      <table id='tab_customers' class='table table-striped'>
        <colgroup>
          <col span='1' />
          <col span='1'/>
        </colgroup>
        <thead>
          <tr class='warning'>
            <th>SOW Creation Date</th>
            <th>SOW Start Date</th>
            <th>Project</th>
            <th>Last Updated</th>
            <th>SOW End Date</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>Dec 13, 2017</td>
            <td>Jan 1, 2018</td>
            <td>NM Connect - NMETNMCM</td>
            <td>Dec 13, 2017</td>
            <td>Dec 31, 2018</td>
          </tr>
        </tbody>
      </table>
      <p>
        This is a Time and Materials Statement of Work between Northwestern Mutual
        Life Insurance Company and Infosys with all general terms and conditions
        as described in the current Master Agreement and its related documents
      </p>
    </div>
  )

function generatePdf() {
    const doc = new jsPDF( 'p', 'mm', 'a4' )
    let index = 0
    const htmlToConvert= renderToString(<Prints />)
    doc.fromHTML(htmlToConvert)
    doc.save('myPdf')
  }

The generatePdf function is called on clicking a button

Upvotes: 2

Views: 2674

Answers (1)

Ran Marciano
Ran Marciano

Reputation: 1531

Try changing the doc.fromHTML TO doc.html like this:

function generatePdf() {
    const doc = new jsPDF( 'p', 'mm', 'a4' )
    let index = 0
    const htmlToConvert= renderToString(<Prints />)
    doc.html(htmlToConvert, {
       callback: function (doc) {
                 doc.save();
                 }
        });
    }

Does this help you?

Upvotes: 1

Related Questions