Generating a PDF file from React Components

后端 未结 8 1977
無奈伤痛
無奈伤痛 2020-12-02 10:45

I have been building a polling application. People are able to create their polls and get data regarding the question(s) they ask. I would like to add the functionality to l

8条回答
  •  无人及你
    2020-12-02 11:04

    You can use ReactDOMServer to render your component to HTML and then use this on jsPDF.

    First do the imports:

    import React from "react";
    import ReactDOMServer from "react-dom/server";
    

    then:

    var doc = new jsPDF();
    doc.fromHTML(ReactDOMServer.renderToStaticMarkup(this.render()));
    doc.save("myDocument.pdf");
    

    Prefer to use:

    renderToStaticMarkup

    instead of:

    renderToString

    As the former include HTML code that react relies on.

提交回复
热议问题