如何打印通过ajax请求获取的PDF? [英] How can I print a PDF fetched via an ajax request?
问题描述
我有一个表单,一旦提交,PHP生成一个PDF文件并将其发送到客户端。到目前为止一切正常。我遇到的麻烦是我需要在包含收到的pdf的窗口上触发window.print()。有没有办法让收到的pdf文件显示打印向导?
I'm having a form that once submitted, the PHP generates a PDF file and sends it to the client. Everything works fine so far. What I'm having trouble with is that I need to trigger window.print() on the window containing the received pdf. Is there a way to make the printing wizard appear for the received pdf file?
这是我的代码
//The #options is a form that once submitted is sends the requested PDF to the browser
$('#options').on('submit', function(e){
if($(this).find('[name="action"]').val() == 'print')
{
var url = $(this).attr('action') || document.URL;
e.preventDefault();
$.post(url, $(this).serializeArray(),function(pdf){
// Open the printing wizard for the requested document
});
}
else
{
// The PDF is displayed normally
return true;
}
});
我甚至不确定我想做什么是可能的。例如,可以在新选项卡中打开PDF并调用 window.print()
吗?
I'm not even sure if what I want to do is possible. Is is possible for example to open the PDF in a new tab and call window.print()
there?
推荐答案
一种简单的方法是将PDF文件放在新的iFrame中。
然后你可以使用 window.print();
function打印iframe中的完整内容。
One easy approach for this is to put the PDF file in a new iFrame.
Then you can print the complete content inside the iframe using window.print();
function.
<html>
<head>
<title>Print Test Page</title>
<script>
function printPDF() {
window.frames["print_frame"].window.focus();
window.frames["print_frame"].window.print();
}
</script>
</head>
<body>
Some content here
<iframe name=print_frame width=0 height=0 frameborder=0 src=about:blank>
Your PDF is loaded here
</iframe>
Some more content here
</body>
</html>
现在调用 window.print();
当您想要打印PDF格式时起作用。
Now call window.print();
function when you want to print your pdf.
这篇关于如何打印通过ajax请求获取的PDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!