【问题标题】:I am trying to open pdf file in new window and open the print dialog after opening the window我正在尝试在新窗口中打开 pdf 文件并在打开窗口后打开打印对话框
【发布时间】:2020-06-19 01:23:12
【问题描述】:

我正在尝试在新窗口中打开 pdf 文件并在打开窗口后打开打印对话框,但是打开打印对话框的 print() 方法被跨域阻止,因为控制台返回此错误 DOMException : 阻止了来源为“http://localhost:4200”的框架访问跨域框架。我该如何解决这个问题?

  print() {
            var myWindow = window.open('https://mediasb.shiftinc.com/booking_pdfs/QALNHF.pdf', 'Booking', 'width=600,height=600');
       myWindow.print();
      myWindow.focus();
    }
<button (click)= "print()"> Print</button>

【问题讨论】:

标签: javascript angular


【解决方案1】:

用角度做这个:-

this.http.get('https://mediasb.shiftinc.com/booking_pdfs/QALNHF.pdf', {responseType: 'blob'}).subscribe((res) => {
        let url = window.URL.createObjectURL(res);
        var myWindow = window.open(url, 'Booking', 'width=600,height=600');
       myWindow.print();
      myWindow.focus();
});

【讨论】:

  • 如何在 Vue3 上做到这一点? const myWin = window.open(url, "_blank")?.print(); 这适用于 Edge 和 Chrome,但不能在 Firefox 或 Safari 中打印。
猜你喜欢
  • 1970-01-01
  • 2014-07-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-11
  • 2010-11-15
  • 1970-01-01
  • 2013-03-07
  • 2023-03-30
相关资源
最近更新 更多