【发布时间】:2017-10-29 20:39:23
【问题描述】:
我有 pdf 文件的网址,exa 网址是“test.example.com/incoice/1/download?auth_token="some_token",当我访问这个网址时,网址会在浏览器中显示 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/ 在这个 plunker 中,我在新窗口中打开 pdf,但我想直接打印该 pdf。我该怎么做?
任何建议将不胜感激,如果我错了,您可以纠正。 谢谢
【问题讨论】:
标签: javascript angular pdf