admin管理员组文章数量:1289845
I am trying to view a pdf file inside react pdf
but I cannot do that because it failed to load the file. I don't know why, and I have tried these CDNs:
.js/2.7.570
.js/2.7.570/pdf.min.js
One of them keeps loading and the other keeps on giving me an error. Either CORS or fake worker failed
error. I need help, thanks in advance.
This is my code:
import { Document, Page, pdfjs } from "react-pdf";
pdfjs.GlobalWorkerOptions.workerSrc =
".js/2.7.570/pdf_viewer.js.map";
const DocsViewer = ({ link }) => {
const [pages, setPages] = useState(null);
const [pageNumber, setPageNumber] = useState(1);
function onDocumentLoadSuccess({ numPages }) {
console.log("object:", numPages, pages);
setPages(numPages);
setPageNumber(1);
}
return (
<div className="property-overview-container property-flex-half">
<div className="description-overview">
<Document
file={
".pdf"
}
// link={link}
onLoadSuccess={onDocumentLoadSuccess}
onLoadError={(error) => console.log("Inside Error", error)}
>
<Page pageNumber={pageNumber} style={{ display: "none" }} />
</Document>
</div>
</div>
);
};
export default DocsViewer;
I am trying to view a pdf file inside react pdf
but I cannot do that because it failed to load the file. I don't know why, and I have tried these CDNs:
https://cdnjs./libraries/pdf.js/2.7.570
https://cdnjs.cloudflare./ajax/libs/pdf.js/2.7.570/pdf.min.js
One of them keeps loading and the other keeps on giving me an error. Either CORS or fake worker failed
error. I need help, thanks in advance.
This is my code:
import { Document, Page, pdfjs } from "react-pdf";
pdfjs.GlobalWorkerOptions.workerSrc =
"https://cdnjs.cloudflare./ajax/libs/pdf.js/2.7.570/pdf_viewer.js.map";
const DocsViewer = ({ link }) => {
const [pages, setPages] = useState(null);
const [pageNumber, setPageNumber] = useState(1);
function onDocumentLoadSuccess({ numPages }) {
console.log("object:", numPages, pages);
setPages(numPages);
setPageNumber(1);
}
return (
<div className="property-overview-container property-flex-half">
<div className="description-overview">
<Document
file={
"https://yahuda.s3.us-east-2.amazonaws./4d0970da-f0c7-4a37-9ce4-5c48619a996b.pdf"
}
// link={link}
onLoadSuccess={onDocumentLoadSuccess}
onLoadError={(error) => console.log("Inside Error", error)}
>
<Page pageNumber={pageNumber} style={{ display: "none" }} />
</Document>
</div>
</div>
);
};
export default DocsViewer;
Share
Improve this question
edited Jul 28, 2021 at 7:40
Costa
2,9642 gold badges17 silver badges22 bronze badges
asked Jul 27, 2021 at 14:32
Haseeb AhmedHaseeb Ahmed
311 gold badge1 silver badge3 bronze badges
2 Answers
Reset to default 6I think most people hitting this answer will probably not realize that they are getting CORS errors, especially developing locally. https://developer.mozilla/en-US/docs/Web/HTTP/CORS
here is a public URL that has CORS opened https://cdn.filestackcontent./wcrjf9qPTCKXV3hMXDwK
You can find more info in another post on the subject: react-pdf loads file from disk but not url
You need to use the parameter as follow:
<Document
file={{url:'https://yahuda.s3.us-east-2.amazonaws./4d0970da-f0c7-4a37-9ce4-5c48619a996b.pdf'}}
onLoadSuccess={onDocumentLoadSuccess}
onLoadError={(error) => console.log("Inside Error", error)}
>
本文标签: javascriptreactpdf failed to view pdf file from remote urlStack Overflow
版权声明:本文标题:javascript - react-pdf failed to view pdf file from remote url - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741428854a2378237.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论