如何使用React-PDF从React中的Firebase存储中查看PDF DOC?
我想从我的React应用中的Firebase存储中查看PDF文件,问题是我想在加载页面后查看该文件,并且“ GetDownloadurl” Firebase方法是异步。
我试图做这样的事情:
const MyPage = (props) => {
const [numPages, setNumPages] = useState(null)
const [pageNumber, setPageNumber] = useState(1)
const [url, setURL] = useState('')
const showFile = async () => {
return await storage.ref(props.props.sheetMusicRef).getDownloadURL()
}
return <Document file={showFile}>
<Page pageNumber={pageNumber} />
</Document>
}
没有成功。
I would like to view a pdf file from firebase storage in my React app, the problem is I would like to view the file after the page is loaded and the 'getDownloadURL' firebase method is async.
I tried to do something like this:
const MyPage = (props) => {
const [numPages, setNumPages] = useState(null)
const [pageNumber, setPageNumber] = useState(1)
const [url, setURL] = useState('')
const showFile = async () => {
return await storage.ref(props.props.sheetMusicRef).getDownloadURL()
}
return <Document file={showFile}>
<Page pageNumber={pageNumber} />
</Document>
}
with no success.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
今天我做了类似的事情,我想简单地从firebase存储中获取PDF,同时显示加载程序,然后渲染文件。我决定不要为自己的用例中使用React-PDF,因为我想在整个视口上嵌入文件。如果您想专门查看我建议的解决方案,请跳到下一个标题。
我的解决方案
此组件从Firebase存储中获取PDF,然后将文件嵌入整个视口上。为了确保我没有能够滚动或下面滚动的能力,可以模仿浏览器中直接打开文件的行为,我使用了NPM的“身体旋转锁” - 包装。我还将MUI用于样式组件。因此,如果您任何人都来此帖子并想要一个工作的全屏PDF浏览器,从firebase存储中获取PDF并显示加载程序,那么您就可以了:
解决问题的解决方案,具体来说,
我已经修改了您提供的代码,并且提出了一些可以按照您想要的方式奏效的更改。这显示了一个加载程序,同时获取文件,从firebase存储中获取文件,并显示出错误消息是否出了问题,或使用React-PDF加载PDF。
I did something similar today, where I wanted to simply fetch a pdf from Firebase Storage, show a loader in the meantime, and render the file. I decided to not go for react-pdf for my own use case, as I wanted to embed the file over the whole viewport. Skip to the next header if you want to see my proposed solution to your code specifically.
My solution
This component fetches the pdf from Firebase Storage, before embedding the file over the whole viewport. For making sure that I do not get the ability to scroll over or under, mimicing the behavior of opening a file straight in the browser, I utilize the "body-scroll-lock"-package from npm. I also use MUI for styled components. So, if you anyone comes onto this post and want a working full screen pdf-viewer, fetching the pdf from Firebase Storage and showing a loader, here you go:
The solution to your problem specifically
I have modified the code you have provided, and proposed some changes that would work the way you want it to. This shows a loader while fetching the file, fetches the file from Firebase Storage, and shows either an error message if something went wrong, or loads the pdf using react-pdf.