2011-08-16 49 views
22

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

,我发现在一个论坛上两个答案:

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

<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> 

但我的问题是,它仅仅适用于IE浏览器,和不工作的Firefox或Chrome。

有没有解决方案?

+1

是关于这个问题的Adobe阅读器的插件API? –

+0

我不认为这是关于Adobe Reader的插件..因为我在所有的浏览器中都有相同版本的Adobe Reader ..仍然这种JavaScript代码在Internet Explorer中不起作用.. –

+1

@Jignesh Manek:我确实不相信你会找到所有浏览器的解决方案。你可以期望的最好的方式是打开打印对话框,说实话,这应该足够好。作为用户,我想要的最后一件事就是让我的打印机开始打印某些东西,而无需检查我的设置,纸张尺寸,纸盒等。考虑一下......即使是大多数各种应用程序,文字处理程序,电子表格等等,只需调用打印对话框即可。 – Sparky

回答

0

容纳印刷触发器的功能...

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

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

(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> 

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

+0

它再次在Firefox和Chrome中不做任何事情..而在IE中测试时,主要的html文件内容被发送到打印机! –

-1
<?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><strong class="highlight">pdf</strong> <strong class="highlight">print</strong> test</title> 
<style> 
html { height:100%; } 
</style> 
<<strong class="highlight">script</strong>> 
function printIt(id){ 
var <strong class="highlight">pdf</strong> = document.getElementById("samplePDF"); 
<strong class="highlight">pdf</strong>.click(); 
<strong class="highlight">pdf</strong>.setActive(); 
<strong class="highlight">pdf</strong>.focus(); 
<strong class="highlight">pdf</strong>.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')"><strong class="highlight">Print</strong></button> 


</body> 
</html> 

<?php 
} else { 
?> 
<HTML> 
<<strong class="highlight">script</strong> 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="<strong class="highlight">Print</strong>" onclick="javascript<b></b>: printfile('objAdobePrint');"> 

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

OP没有要求PHP解决方案。 – Sparky

+0

代码在我的问题已与IE浏览器..我想要一个修复火狐和铬! –

4

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

在主要页面的头:

<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> 

,并在主网页的正文:

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

内pdfwebpage.html:

<html> 
<head>  
</head> 

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

</body> 
</html> 
+1

这种方法对我来说似乎没有任何作用。 Chrome浏览器,IE浏览器和FireFox都采用相同的方式 - 出现打印对话框,但它只是在网页上打印一个小的/不可见的嵌入式PDF文件。我需要触发PDF打印,而不是网页打印。我错过了什么吗? –

5

有两个步骤,您需要采取。

首先,您需要将PDF放在iframe中。

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

要打印的iframe你可以看看这里的答案:

Javascript Print iframe contents only

如果你要打印的iframe的PDF加载后自动,你可以添加一个onload处理的:

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

装载机可以是这样的:

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

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

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

+2

我在Chrome,IE和Firefox中尝试了这种方法,但它不像预期的那样工作。在Chrome中,我得到“pdfFrame.print不是函数”。在FF中,我得到“错误:拒绝访问属性的权限”print“”。 IE不给出错误,但不会触发打印对话框。 PDF位于相同的原点,X-Frame-Options设置为SAMEORIGIN。这种方法只是过时了吗? –

4

我想的JavaScript这个库可以帮助你:

这就是所谓的Print.js

首先包括

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

它的基本用法是调用printJS(),只是通过一个PDF文档中网址:printJS('docs/PrintJS.pdf')

我做的是s就像这样,如果PDF文档太大,它也会显示“加载....”。

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

但是请记住:

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

相关问题