我正在尝试使用 react-pdf 库来显示 base64 编码的 PDF 文件。B64 数据是准确的,因为它在 iframe 中加载如下:
<iframe
className={css.previewIframe}
src={`data:application/pdf;base64,${
this.props.encodedPdf
}`}
/>
但是,如果我将数据插入到 react-pdf 文档中,则会出现错误:
<Document file={this.props.encodedPdf} />
文档呈现错误消息Failed to load PDF file.
,控制台记录以下错误:InvalidPDFException {name: "InvalidPDFException", message: "Invalid PDF structure"}
我也收到很多警告,如下所示:Warning: Ignoring invalid character "109" in hex string
文档明确指出文件道具可以是 B64 编码的内容。关于问题可能是什么的任何想法?如果需要,这里是 B64 数据:
JVBERi0xLjMKMSAwIG9iago8PAovS2lkcyBbIDMgMCBSIF0KL1R5cGUgL1BhZ2VzCi9Db3VudCAxCj4+CmVuZG9iagoyIDAgb2JqCjw8Ci9Qcm9kdWNlciAoUHlQREYyKQo+PgplbmRvYmoKMyAwIG9iago8PAovQ29udGVudHMgNSAwIFIKL1JvdGF0ZSAwCi9UeXBlIC9QYWdlCi9SZXNvdXJjZXMgPDwKL1Byb2NTZXQgWyAvUERGIC9UZXh0IF0KPj4KL0Nyb3BCb3ggWyAwIDAgNjEyIDc5MiBdCi9QYXJlbnQgMSAwIFIKL01lZGlhQm94IFsgMCAwIDYxMiA3OTIgXQo+PgplbmRvYmoKNCAwIG9iago8PAovVHlwZSAvQ2F0YWxvZwovUGFnZXMgMSAwIFIKPj4KZW5kb2JqCjUgMCBvYmoKPDwKL0xlbmd0aCAxMAovRmlsdGVyIC9GbGF0ZURlY29kZQo+PgpzdHJlYW0KSIkCCDAAAAAAAQplbmRzdHJlYW0KZW5kb2JqCnhyZWYKMCA2CjAwMDAwMDAwMDAgNjU1MzUgZiAKMDAwMDAwMDAwOSAwMDAwMCBuIAowMDAwMDAwMDY4IDAwMDAwIG4gCjAwMDAwMDAxMDggMDAwMDAgbiAKMDAwMDAwMDI3MyAwMDAwMCBuIAowMDAwMDAwMzIyIDAwMDAwIG4gCnRyYWlsZXIKPDwKL1NpemUgNgovUm9vdCA0IDAgUgovSW5mbyAyIDAgUgo+PgpzdGFydHhyZWYKNDAzCiUlRU9GCg==
如果我添加data:application/pdf;base64,
到字符串的开头,则没有错误,但也不会呈现任何内容。