如何使用JavaScript直接打开和打印PDF而不进行预览?

如何使用JavaScript直接打开和打印PDF而不进行预览?,javascript,pdf,printing,cross-browser,Javascript,Pdf,Printing,Cross Browser,我已将文件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/ht

我已将文件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>

MDN示例
函数closePrint(){
document.body.removeChild(这个容器);
}
函数setPrint(){
this.contentWindow.\uuuuu容器\uuuu=this;
this.contentWindow.onbeforeunload=closePrint;
this.contentWindow.onafterprint=closePrint;
this.contentWindow.focus();//是IE所必需的
console.log(this.contentWindow);
这个.contentWindow.print();
}
函数打印页(sURL){
var oHiddFrame=document.createElement(“iframe”);
oHiddFrame.onload=设置打印;
oHiddFrame.style.visibility=“隐藏”;
ohidframe.style.position=“固定”;
oHiddFrame.style.right=“0”;
oHiddFrame.style.bottom=“0”;
oHiddFrame.src=sURL;
文档.body.appendChild(oHiddFrame);
}
加载和打印

如果有人投反对票,请指出原因,我将不胜感激。例如:为什么它被认为太宽了?这是一个非常具体的问题,对吧:打开一个PDF文件并通过JavaScript打印。这对你有帮助吗:@krnekires:chrome38和firefox33很抱歉,它不起作用。