ReactPDF:解析HTML字符串到HTML并显示

发布于 2025-02-09 13:46:11 字数 1346 浏览 3 评论 0原文

我目前正在解析HTML字符串以HTML的问题,问题是当我试图将其显示给PDF时。我正在使用 pdfdownloadlink

时必须分析的项目阵列

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>',
}
] 

文本

(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>
                  )

})

这是我在设置文本时必须放在 be doc.body.firstchild

我收到此错误消息:

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盛装女皇 2025-02-16 13:46:11

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

For anyone who has the same issue in the future, I found a package (https://www.npmjs.com/package/react-pdf-html) as it reads it renders HTML into reactpdf

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文