简体   繁体   中英

ReactPDF: parsing html string to html and displaying

I am currently parsing the HTML string to HTML the problem is when I am trying to display it to a pdf. I am using PDFDownloadLink

this is the array of items I have to parse

const unit = [
{
_id: 1,
text: '<p>hello there</p>',
},
{
_id: 2,
text: '<h1>hello there</h1>',
}
{
_id: 3,
text: '<p>hello there</p><br/> <p>Hello World</p>',
}
] 

JSX

(units.map((unit) => {
 let text = <p>hello</p>
                  if (unit.text) {
                    const parser = new DOMParser()
                    const doc = parser.parseFromString(unit.text, 'text/html')
                    // doc.body.firstChild

                  }

                  return (
                      {/* <div dangerouslySetInnerHTML={{ __html: unit.text }} /> */}
                      <Text>
                        <div>
                          {text}
                        </div>
                      </Text>
                  )

})

When I set the text to be doc.body.firstChild

I get this error message:

Error: Objects are not valid as a React child (found: [object HTMLParagraphElement]). If you meant to render a collection of children, use an array instead.

对于将来遇到相同问题的任何人,我发现了一个包( https://www.npmjs.com/package/react-pdf-html ),因为它读取它将 HTML 呈现为 reactpdf

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM