在新窗口中打开 PDF 并在 Firefox 中打印
我正在使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了一种让它在 Firefox 和 Safari 上运行的方法。但我不知道是否还有另一种不使用
setTimeout()
的方法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()