ReactPDF:解析HTML字符串到HTML并显示
我目前正在解析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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于将来有相同问题的任何人,我找到了一个包( 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