如何使用 JavaScript 将 pdf 文件直接发送到打印机?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7074070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:44:12  来源:igfitidea点击:

How to send a pdf file directly to the printer using JavaScript?

javascripthtmlpdfprinting

提问by Jignesh Manek

How to send a PDF file directly to the printer using JavaScript?

如何使用 JavaScript 将 PDF 文件直接发送到打印机?

I found two answers in a forum:

我在论坛上找到了两个答案:

<embed src="vehinvc.pdf" id = "Pdf1" name="Pdf1" hidden>
<a onClick="document.getElementById('Pdf1').printWithDialog()" style="cursor:hand;">Print file</a>

and

<OBJECT id = "Pdf2" name="Pdf2" CLASSID="clsid:CA8A9780-280D-11CF-A24D-444553540000" WIDTH="364" HEIGHT="290">
     <PARAM NAME='SRC' VALUE="file.pdf">
</OBJECT>
<a onClick="document.Pdf2.printWithDialog()">Print file</a> 

But my problem is that it just works on IE, and doesnt work in Firefox or Chrome.

但我的问题是它只适用于 IE,不适用于 Firefox 或 Chrome。

Is there any solution for this?

有什么解决办法吗?

回答by zsubzwary

I think this Library of JavaScript might Help you:

我认为这个 JavaScript 库可能会帮助你:

It's called Print.js

它叫做Print.js

First Include

首先包含

<script src="print.js"></script>
<link rel="stylesheet" type="text/css" href="print.css">

It's basic usage is to call printJS()and just pass in a PDF document url: printJS('docs/PrintJS.pdf')

它的基本用法是调用printJS()并传入一个 PDF 文档 url:printJS('docs/PrintJS.pdf')

What I did was something like this, this will also show "Loading...." if PDF document is too large.

我所做的是这样的,如果PDF文档太大,这也会显示“正在加载....”。

<button type="button" onclick="printJS({printable:'docs/xx_large_printjs.pdf', type:'pdf', showModal:true})">
    Print PDF with Message
</button>

However keep in mind that:

但是请记住:

Firefox currently doesn't allow printing PDF documents using iframes. There is an open bugin Mozilla's website about this. When using Firefox, Print.js will open the PDF file into a new tab.

Firefox 目前不允许使用 iframe 打印 PDF 文档。Mozilla 的网站上有一个关于此的公开错误。使用 Firefox 时,Print.js 会在新选项卡中打开 PDF 文件。

回答by Doctor Eval

There are two steps you need to take.

您需要执行两个步骤。

First, you need to put the PDF in an iframe.

首先,您需要将 PDF 放入 iframe。

  <iframe id="pdf" name="pdf" src="document.pdf"></iframe>

To print the iframe you can look at the answers here:

要打印 iframe,您可以在此处查看答案:

Javascript Print iframe contents only

Javascript 仅打印 iframe 内容

If you want to print the iframe automatically after the PDF has loaded, you can add an onload handler to the <iframe>:

如果您想在 PDF 加载后自动打印 iframe,您可以添加一个 onload 处理程序到<iframe>

  <iframe onload="isLoaded()" id="pdf" name="pdf" src="document.pdf"></iframe>

the loader can look like this:

加载器看起来像这样:

function isLoaded()
{
  var pdfFrame = window.frames["pdf"];
  pdfFrame.focus();
  pdfFrame.print();
}

This will display the browser's print dialog, and then print just the PDF document itself. (I personally use the onload handler to enable a "print" button so the user can decide to print the document, or not).

这将显示浏览器的打印对话框,然后只打印 PDF 文档本身。(我个人使用 onload 处理程序来启用“打印”按钮,以便用户可以决定是否打印文档)。

I'm using this code pretty much verbatim in Safari and Chrome, but am yet to try it on IE or Firefox.

我在 Safari 和 Chrome 中几乎逐字使用这段代码,但我还没有在 IE 或 Firefox 上尝试它。

回答by Dhruv Murarka

Try this: Have a button/link which opens a webpage (in a new window) with just the pdf file embedded in it, and print the webpage.

试试这个:有一个按钮/链接,可以打开一个网页(在新窗口中),其中嵌入了 pdf 文件,然后打印网页。

In head of the main page:

在主页的头部:

<script type="text/javascript">
function printpdf() 
{
myWindow=window.open("pdfwebpage.html");
myWindow.close;  //optional, to close the new window as soon as it opens
//this ensures user doesn't have to close the pop-up manually
}
</script>

And in body of the main page:

在主页的正文中:

<a href="printpdf()">Click to Print the PDF</a>

Inside pdfwebpage.html:

在 pdfwebpage.html 里面:

<html>
<head>    
</head>

<body onload="window.print()">
<embed src="pdfhere.pdf"/>

</body>
</html>

回答by Joe Solutions

a function to house the print trigger...

一个容纳打印触发器的函数......

function printTrigger(elementId) {
    var getMyFrame = document.getElementById(elementId);
    getMyFrame.focus();
    getMyFrame.contentWindow.print();
}

an button to give the user access...

一个按钮,让用户访问...

(an onClick on an a or button or input or whatever you wish)

<input type="button" value="Print" onclick="printTrigger('iFramePdf');" />
an iframe pointing to your PDF...

<iframe id="iFramePdf" src="myPdfUrl.pdf" style="dispaly:none;"></iframe>

More : http://www.fpdf.org/en/script/script36.php

更多:http: //www.fpdf.org/en/script/script36.php

回答by Joe Solutions

<?php

$browser_ver = get_browser(null,true);
//echo $browser_ver['browser'];

if($browser_ver['browser'] == 'IE') {
?>

<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>pdf print test</title>
<style>
    html { height:100%; }
</style>
<script>
    function printIt(id) {
        var pdf = document.getElementById("samplePDF");
        pdf.click();
        pdf.setActive();
        pdf.focus();
        pdf.print();
    }
</script>
</head>

<body style="margin:0; height:100%;">

<embed id="samplePDF" type="application/pdf" src="/pdfs/2010/dash_fdm350.pdf" width="100%" height="100%" />
<button onClick="printIt('samplePDF')">Print</button>


</body>
</html>

<?php
} else {
?>
<HTML>
<script Language="javascript">

function printfile(id) { 
    window.frames[id].focus();
    window.frames[id].print();
} 

</script>
<BODY marginheight="0" marginwidth="0">

<iframe src="/pdfs/2010/dash_fdm350.pdf" id="objAdobePrint" name="objAdobePrint" height="95%" width="100%" frameborder=0></iframe><br>

<input type="button" value="Print" onclick="javascript:printfile('objAdobePrint');">

</BODY>
</HTML>
<?php
}
?>