自动打开 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 输出到同一域上的子窗口(),然后调用
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();
}
相关文章