对于现有的非IE浏览器(Chrome、Firefox、Opera、Safari),我希望能够根据PDF的URL将PDF文档发送到打印机。
为了避免出现多余的弹窗,我目前使用
以下是我目前想到的解决方案(为了简单起见,使用了lodash和jQuery):
有时候使用Google Chrome打印对话框会正确地显示PDF文件,但当用户选择打印机并确认打印意图后,实际上会将帧的父页面内容发送到打印机而不是PDF本身。
这里有一个链接Mozilla页面上的建议,但该文档目前似乎已经过时了。我能找到的最好的例子是通过反向工程亚马逊Web服务中的发票打印对话框,但那会弹出一个窗口。
我考虑的一个替代方案是Google Cloud Print,但显然这需要安装额外的软件或配置Google账户,除非必要,否则我不愿意强加给用户。
有没有其他例子可以在给定URL的情况下打印PDF,特别是使用Javascript且不使用超量的浏览器附加组件或类似弹出窗口之类的工具?
为了避免出现多余的弹窗,我目前使用
<iframe>
实现此功能,但我希望在打印完成后关闭iframe,否则某些浏览器会在尝试离开页面时弹出对话框。以下是我目前想到的解决方案(为了简单起见,使用了lodash和jQuery):
var _print_url, _remove_iframe, _set_print;
_print_url = function(src_url) {
$("<iframe src='" + src_url + "' type='application/pdf'>").css({
visibility: 'hidden',
position: 'fixed',
right: '0',
bottom: '0'
}).on('load', _set_print).appendTo(document.body);
};
_remove_iframe = function(iframe) {
return $(iframe).parent().find(iframe).detach();
};
_set_print = function() {
this.contentWindow.print();
/*
* Where we could do @contentWindow.close() with a window, we must remove the
* print iframe from the DOM. We have to engage in some async madness
* it seems, for no apparent reason other than this won't work
* synchronously (@cw.print above must be async, it seems) - even though
* window.close() appears to work synchronously.
*/
_.delay(_.partial(_remove_iframe, this), 100);
};
有时候使用Google Chrome打印对话框会正确地显示PDF文件,但当用户选择打印机并确认打印意图后,实际上会将帧的父页面内容发送到打印机而不是PDF本身。
这里有一个链接Mozilla页面上的建议,但该文档目前似乎已经过时了。我能找到的最好的例子是通过反向工程亚马逊Web服务中的发票打印对话框,但那会弹出一个窗口。
我考虑的一个替代方案是Google Cloud Print,但显然这需要安装额外的软件或配置Google账户,除非必要,否则我不愿意强加给用户。
有没有其他例子可以在给定URL的情况下打印PDF,特别是使用Javascript且不使用超量的浏览器附加组件或类似弹出窗口之类的工具?
print()
之前调用this.contentWindow.focus()
会发生什么? - Jordan Gray