我将文件print.htm部署到localhost,此解决方案仅适用于Chrome,不适用于IE和FF。
在Chrome中,它会很好地弹出此对话框:
在FF和IE中,没有任何反应。
我听说过PDF.js,但如果有原生解决方案,就不会想要它。 是否有人设法直接在FF / IE中打印PDF?
以下是我的代码:
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>MDN Example</title>
<script type="text/javascript">
function closePrint () {
document.body.removeChild(this.__container__);
}
function setPrint () {
this.contentWindow.__container__ = this;
this.contentWindow.onbeforeunload = closePrint;
this.contentWindow.onafterprint = closePrint;
this.contentWindow.focus(); // Required for IE
console.log(this.contentWindow);
this.contentWindow.print();
}
function printPage (sURL) {
var oHiddFrame = document.createElement("iframe");
oHiddFrame.onload = setPrint;
oHiddFrame.style.visibility = "hidden";
oHiddFrame.style.position = "fixed";
oHiddFrame.style.right = "0";
oHiddFrame.style.bottom = "0";
oHiddFrame.src = sURL;
document.body.appendChild(oHiddFrame);
}
</script>
</head>
<body>
<button id="idPrint1" onclick="printPage('file.pdf')">Load and Print </button>
</body>
</html>