在新窗口中打开 PDF 并在 Firefox 中打印

发布于 2025-01-16 22:57:55 字数 571 浏览 2 评论 0原文

我正在使用 Vue3.js。我需要在新窗口中打开 PDF,然后打开打印对话框,但这仅适用于 MS Edge 和 Google Chrome,但不适用于 Firefox 或 Safari。

    const getBlobPdf = async (url: string) => {
      const response = await axios.get(url, { responseType: "blob" });
      return new Blob([response.data], { type: "application/pdf" });
    };

    const printPdf = async ({ url }) => {
      const blob = await getBlobPdf(url);
      window.open(URL.createObjectURL(blob), "_blank")?.print();
    };

我尝试过 window.focus(); window.print(); 但没有工作。

I am using Vue3.js. I need to open a PDF in a new window, then open the print dialog but this only works on MS Edge and Google Chrome, but does not work on Firefox or Safari.

    const getBlobPdf = async (url: string) => {
      const response = await axios.get(url, { responseType: "blob" });
      return new Blob([response.data], { type: "application/pdf" });
    };

    const printPdf = async ({ url }) => {
      const blob = await getBlobPdf(url);
      window.open(URL.createObjectURL(blob), "_blank")?.print();
    };

I had tried window.focus(); window.print(); but didn't work.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦幻的味道 2025-01-23 22:57:55

我找到了一种让它在 Firefox 和 Safari 上运行的方法。但我不知道是否还有另一种不使用 setTimeout() 的方法

const printPdf = async ({ url }) => {
      const blob = await getBlobPdf(url);
      const printWin = window.open(URL.createObjectURL(blob), "_blank");
      if (printWin) {
        setTimeout(() => {
          printWin.print();
        }, 200);
      }
    };

I found a way to make it work on Firefox and Safari. But I don't know if there is another way without using setTimeout()

const printPdf = async ({ url }) => {
      const blob = await getBlobPdf(url);
      const printWin = window.open(URL.createObjectURL(blob), "_blank");
      if (printWin) {
        setTimeout(() => {
          printWin.print();
        }, 200);
      }
    };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文