Jspdf: Unable to render react components into PDF

Created on 26 May 2017  路  9Comments  路  Source: MrRio/jsPDF

Thank you for submitting an issue to jsPDF. Please read carefully.

Are you using the latest version of jsPDF?
Yes

Have you tried using jspdf.debug.js?
No

What I saw
jsPDF does not render the react components into pdf. Is this functionality not provided or am I making any mistake in writing the code?

     <div>
        <div id="HTMLtoPDF">
            <HelloWorld name="Rambo"/>
            <HelloWorld name="Riya"/>
            <HelloWorld name="Rose"/>
            <h2>Is it converted?!</h2>
          <center>
            <h2>HTML to PDF</h2>
           <p>Lorem ipsum dolor sit amet, consectetur adipisicing </p>
          </center>
          <button>Hi, Click me</button>
          <img src={puppy} />
        </div>
        <button onClick={this.pdfToHTML}>Download PDF</button>
      </div>

When the above code is written in render() , all the components and tags are displayed in the browser. But when download the pdf, neither the buttons nor react components are getting converted.

Most helpful comment

@steric85 I've just came across this issue as well and it's actually possible to just use ReactDOMServer (imported from 'react-dom/server') and its function renderToStaticMarkup(component) to render html that jsPDF will accept.

All 9 comments

Too abstract. Please provide a test case on jsfiddle or jsbin. Thanks.

For the above code, the webpage I get is:
screen shot 2017-05-27 at 9 36 57 am
But when I download PDF , I get the following output:
html2pdf (7).pdf

By looking at the two, you can know that the button and react components HelloWorld are not present in the PDF.

I never worked with react so i dont know the deeper functionality behind it. But probably it doesnt work, because you use unsupported custom html-tags. You have to write your own renderer for your custom tags.

Oh, Ohkay. Thanks!

@steric85 I've just came across this issue as well and it's actually possible to just use ReactDOMServer (imported from 'react-dom/server') and its function renderToStaticMarkup(component) to render html that jsPDF will accept.

@steric85, can you show me an example with react? i did use renderToStaticMarkup but it only printed the

and etc ...

jspdf.text(ReactDOMServer.renderToStaticMarkup(<ResumeContent />), 10, 10, {'width': 180})

@iqbalsafian, you use .fromHTML() instead of .text()

jspdf.fromHTML(ReactDOMServer.renderToStaticMarkup(<ResumeContent />), 10, 10, {'width': 180})

I've tried and the result lost all the styles in the components. This is the normal behaviour?

If you mean, that you had CSS3 and used fromHTML, then yes.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tarekis picture tarekis  路  4Comments

Pinank picture Pinank  路  3Comments

centurianii picture centurianii  路  4Comments

0721Betty picture 0721Betty  路  4Comments

MelanieCroce picture MelanieCroce  路  4Comments