問題描述
我知道有多種方法可以將 PDF 打印到與服務(wù)器位于同一網(wǎng)絡(luò)的網(wǎng)絡(luò)打印機(jī),但這對(duì)我沒有幫助,因?yàn)榉?wù)器是遠(yuǎn)程的.在我的情況下,用戶單擊打印標(biāo)簽"的鏈接,然后生成并輸出為其設(shè)置格式的 PDF 文件.我目前將文件輸出流式傳輸"到瀏覽器,以便 Adob??e Reader 使用以下代碼自動(dòng)打開它:
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);
還有什么我可以添加到這個(gè)代碼中的東西,它會(huì)自動(dòng)觸發(fā)打印對(duì)話框打開,這樣他們只需要點(diǎn)擊打印?在這種情況下,Google CloudPrint 不是一種選擇,其他需要在用戶端進(jìn)行特殊設(shè)置"的東西也不是一種選擇……因?yàn)檫@將被各種用戶使用.
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 輸出到同一域上的子窗口(),然后調(diào)用
window.print()
在那個(gè)窗口上.
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();
}
這篇關(guān)于自動(dòng)打開 PDF 文件的打印機(jī)對(duì)話框的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!