自动打开PDF文件的打印机对话框 [英] Open Printer Dialog for PDF file Automatically

查看:344
本文介绍了自动打开PDF文件的打印机对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有一些方法可以将PDF打印到与服务器位于同一网络上的网络打印机,但是由于服务器是远程的,所以这对我没有帮助.在我的情况下,用户单击指向打印标签"的链接,该链接然后生成并输出为其设置格式的PDF文件.我目前将文件输出流式传输"到浏览器,以便Adobe Reader使用以下代码自动打开它:

I know that there are ways to print a PDF to a network printer located on the same network as the server, but that does not help me as the server is remote. In my situation a user clicks a link to "print labels" which then generates and outputs a PDF file formatted for them. I currently "stream" the file output to the browser such that Adobe Reader automatically opens it using the following code:

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Content-type: application/pdf");
header('Content-Disposition: attachment; filename="labels.pdf"');
readfile($ServerPathToFile);

我可以在此代码中添加其他内容,以自动触发打印对话框打开,以便他们只需单击打印"吗?在这种情况下,不能使用Google CloudPrint,也不需要在用户端进行特殊设置"的其他事物……因为各种用户都将使用它.

Is there something else I can add to this code that will automatically trigger the print dialogue box to open so that they only have to click print? In this case, Google CloudPrint is not an option, nor are other things that require "special setup" on the user end...as this will be used by a variety of users.

推荐答案

您可以将PDF输出到同一域的子窗口(<iframe>),然后在该窗口上调用window.print().

You could output the PDF to a child window (<iframe>) on the same domain and then call window.print() on that window.

<p>Don't forget to print your document!</p>
<iframe src="/path/to/your/pdfgenerator.php" id="mypdf"></iframe>

<script>
function printIframe(id) {
    var iframe = document.frames ? document.frames[id] : document.getElementById(id);
    var ifWin = iframe.contentWindow || iframe;
    iframe.focus();
    ifWin.printPage();
    return false;
}
</script>

在iframe页面中,添加以下内容:

In the iframe page, add this:

function printPage() {
    print();
}

这篇关于自动打开PDF文件的打印机对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆