blob收到的pdf文件未使用iframe/ embed出现

发布于 2025-02-03 02:38:25 字数 1698 浏览 5 评论 0原文

我是完整堆栈开发的新手,并且在检索和显示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预览。

问题是:

  1. 这是获得和预览PDF的推荐方法吗?
  2. 如果不是这样,请指导我了解最好的方法。
  3. 如果这种方法还可以,那么我缺少什么。

谢谢您提前的时间。

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:

  1. Is this the recommended approach to get and preview pdfs?
  2. If not then please guide me to what is the best way.
  3. If this approach is ok then what am i missing.

Thank you for your time in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文