我有一个 PDF 文件的 URL,例如: "test.example.com/incoice/1/download?auth_token="some_token",当我访问这个 URL 时,浏览器会在页面中显示 PDF。
现在我想使用打印功能打开此 PDF,也就是说,用户不必按 CTRL+P
,我希望可以从我的代码中实现。
我尝试使用 iframe,但出现了跨域错误。以下是我使用的演示代码:
//first try
let _printIframe;
var iframe = _printIframe;
if (!_printIframe) {
iframe = _printIframe = document.createElement('iframe');
document.body.appendChild(iframe);
iframe.style.display = 'none';
iframe.id = "printf";
iframe.name = "printf";
iframe.onload = function() {
setTimeout(function() {
iframe.focus();
iframe.contentWindow.print();
}, 1);
};
}
// second try
// SRC of pdf
iframe.src = "Some API URl " + "/download?access_token=" +
this.authenticationService.currentTokenDetails.access_token;
let url = iframe.src + "&output=embed";
window.frames["printf"].focus();
window.frames["printf"].print();
var newWin = window.frames["printf"];
newWin.document.write('<body onload="window.print()">dddd</body>');
newWin.document.close();
我在 Plunker 上创建了一个演示程序以打印 PDF。http://embed.plnkr.co/WvaB9HZicxK6tC3OAUEw/ 在这个演示中,我在新窗口中打开了 PDF,但我想直接打印这个 PDF。我该怎么做?
任何建议都会受到赞赏,如果我有错误,您可以进行更正。 谢谢。