blob收到的pdf文件未使用iframe/ embed出现
我是完整堆栈开发的新手,并且在检索和显示PDF的内容时面临这个问题。我正在使用ReactJ并表达。
问题声明: 我想从后端检索PDF文档并将其发送到前端。显示它时,我想在每个文档的第1页的预览中创建一个滚动列表。 目前,我使我的问题声明变得更简单,并正在从我的Express应用程序的“公共”文件夹中发送PDF,并在我的ReactJS应用程序中检索它。
快递代码: express代码
app.get("/pdf-templates", (req, res) => {
let pdf = fs.createReadStream("./public/templates/final_look.pdf");
pdf.pipe(res);
});
前端代码: reactjs code
function GetCertificateTemplates() {
const [pdfUrl, setPdfUrl] = useState("");
const getTemplateHandler = async () => {
axios("http://localhost:3030/pdf-templates", {
method: "GET",
responseType: "blob",
}).then((res) => {
const file = new Blob([res.data], { type: "application/pdf" });
const fileURL = URL.createObjectURL(file);
setPdfUrl(fileURL);
// console.log(res.data);
// window.open(
// fileURL,
// "pdfwindow",
// "left=100,top=100,width=300,height=300"
// );
});
};
return (
<div className="Get-Certificate-Template">
<button onClick={getTemplateHandler}>Get Templates</button>
{/* <iframe src={pdfUrl} />; */}
<embed src={pdfUrl} type="application/pdf" />;
</div>
);
}
在评论的行中,'window'命令''''''但是同一URL不适用于iframe或嵌入铬中。 iframe代码正在为Edge工作。 我没有使用“窗口”作为我的最终目标是在同一窗口中显示多个PDF预览。
问题是:
- 这是获得和预览PDF的推荐方法吗?
- 如果不是这样,请指导我了解最好的方法。
- 如果这种方法还可以,那么我缺少什么。
谢谢您提前的时间。
I am new to full stack development and am facing this problem while retrieving and displaying the contents of a pdf. I am using reactjs and express.
Problem statement:
I want to retrieve pdf documents from the backend and send it to the frontend. When displaying it I want to create a scroll list with the preview of page 1 of each document.
For now I have made my problem statement simpler and am sending a pdf from the 'public' folder of my express app and retrieving it in my reactjs app.
Express code:
express code
app.get("/pdf-templates", (req, res) => {
let pdf = fs.createReadStream("./public/templates/final_look.pdf");
pdf.pipe(res);
});
Frontend code:
reactjs code
function GetCertificateTemplates() {
const [pdfUrl, setPdfUrl] = useState("");
const getTemplateHandler = async () => {
axios("http://localhost:3030/pdf-templates", {
method: "GET",
responseType: "blob",
}).then((res) => {
const file = new Blob([res.data], { type: "application/pdf" });
const fileURL = URL.createObjectURL(file);
setPdfUrl(fileURL);
// console.log(res.data);
// window.open(
// fileURL,
// "pdfwindow",
// "left=100,top=100,width=300,height=300"
// );
});
};
return (
<div className="Get-Certificate-Template">
<button onClick={getTemplateHandler}>Get Templates</button>
{/* <iframe src={pdfUrl} />; */}
<embed src={pdfUrl} type="application/pdf" />;
</div>
);
}
In the commented lines, the 'window' command works and the pdf is rendered fine. But the same url is not working for iframe or embeded in chrome. iframe code is working for edge though.
I am not using 'window' as my final goal is to display multiple pdf previews in the same window.
The questions are the follows:
- Is this the recommended approach to get and preview pdfs?
- If not then please guide me to what is the best way.
- If this approach is ok then what am i missing.
Thank you for your time in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论