使用PHP DOMPDF和AJAX下载PDF文件 [英] Using PHP DOMPDF and AJAX to download a PDF File
问题描述
我有一个名为print.php的PHP文件:
I have this PHP File called print.php:
<?php
require_once("modules/pdf/dompdf_config.inc.php");
$html = $_POST["toRender"];
$number = $_POST["number"];
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream($number.".pdf");
如您所见,HTML和数字是通过POST接收的.
As you can see, the HTML and a Number are received via POST.
JavaScript文件如下:
The JavaScript file looks like this:
$("#btnViewPDF").click(function() {
var theHtml = $("#printable").html();
var theNumber = $("#invoiceNumber").val();
$.ajax({
type : "post",
url : "views/print.php",
data : {toRender : theHtml, number : theNumber},
beforeSend: function() {
$(".ajax-loader-bg").fadeIn();
}
}).always(function() {
$(".ajax-loader-bg").fadeOut();
});
return false;
});
基本上,它将DIV内的所有内容都称为可打印",但接下来我要显示的是print.php中生成的PDF,但我无法弄清楚如何制作此PDF.
Basically it takes all the content inside a DIV called 'printable' but what I want next is the PDF that has been generated in print.php to be displayed, I haven't been able to figure out how can I make this.
当我在print.php内生成测试HTML时,我使它起作用,然后我输入URL mysite.com/print.php ...它呈现PDF,并允许我下载或在另一个PDF中查看浏览器标签.
I've made it work when I generate test HTML inside print.php and then I type in the url mysite.com/print.php ... it renders the PDF and allows me to download it or see it in another browser tab.
如何通过AJAX实现此目标?
How can I achieve this via AJAX?
推荐答案
您无法通过AJAX下载某些内容,可以使用隐藏的iframe来模拟行为.
You can't download something via AJAX, you could simulate the behavior using an hidden iframe.
并不是说您不能下载它,但是它永远不会最终保存在文件系统中,因为JavaScript出于安全原因不能这样做.
Not that you can't download it, but, it will never end up in the filesystem for saving purpose because javascript can't do that for security reasons.
无论如何,人们总是会找到解决方案,您可以尝试以下方法: https://stackoverflow.com/a/23797348/1131176
Anyway people always find solutions, you can try this: https://stackoverflow.com/a/23797348/1131176
这篇关于使用PHP DOMPDF和AJAX下载PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!