自动打开PDF文件的打印机对话框

tec*_*tre 5 php printing pdf

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

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

还有什么我可以添加到这个代码,将自动触发打印对话框打开,以便他们只需要点击打印?在这种情况下,Google CloudPrint不是一个选项,也不是用户端需要"特殊设置"的其他东西......因为这将被各种用户使用.

dtb*_*rne 3

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

<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>
Run Code Online (Sandbox Code Playgroud)

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

function printPage() {
    print();
}
Run Code Online (Sandbox Code Playgroud)