Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用AJAX和Jquery使用FPDF创建pdf_Php_Jquery_Ajax_Pdf - Fatal编程技术网

Php 使用AJAX和Jquery使用FPDF创建pdf

Php 使用AJAX和Jquery使用FPDF创建pdf,php,jquery,ajax,pdf,Php,Jquery,Ajax,Pdf,我正在尝试使用FPDF创建pdf文件 我现在拥有的是一个HTML页面,页面上有各种数据行,旁边还有一个打印按钮。当有人单击Print时,我通过AJAX调用使用Jquery发送相应的数据 这是我的JQUERY代码: $('.printbtn').live('click', function(){ var printdata = 'name=' + name_t + '&address=' + address_t; $.post('print.php', pr

我正在尝试使用FPDF创建pdf文件

我现在拥有的是一个HTML页面,页面上有各种数据行,旁边还有一个打印按钮。当有人单击Print时,我通过AJAX调用使用Jquery发送相应的数据

这是我的JQUERY代码:

$('.printbtn').live('click', function(){
    var printdata = 'name=' + name_t + '&address=' + address_t;        
    $.post('print.php', printdata, function(){
    });
    return false;
 });
这是print.php
 $name = $_POST['name'];
 $address = $_POST['address'];

 require ("fpdf17/fpdf.php");

 $pdf = new FPDF('P','mm',array(90,100));

 $pdf->AddPage();

 $pdf->SetFont('Arial','B',12);

 $pdf->Cell(0,10,'name: '.$name);
 $pdf->Cell(0,10,'address: '.$address);

 $pdf->Output();

 ?>
但我并没有得到PDF的回报。怎么了??事实上,什么也没有发生。。
我想检索pdf文件并将其发送到print

要通过ajax加载pdf,您可以尝试:

  • 使用一些PDF javascript库直接从 没有插件的javascript,如或。 并尝试查看这些库是否允许您直接从 二进制数据(在ajax中接收的数据)

  • 另一个选项是接收base64编码的pdf数据并使用 a要将
    window.location.href
    设置为该数据uri, 尽管在这种情况下,PDF可能作为下载提供 对话框,而不是直接在页面中加载,您必须进行测试 那个此外,IE8和IE中对pdf的数据uri支持非常有限 如需更多详细信息,请参阅维基百科

  • 另请参阅如何无法加载pdf 直接从ajax和什么其他选项,你必须尝试做什么 您需要(主要是将pdf保存为服务器上的临时文件)并使用它 使用
    window.location.href
    window.open


您应该使用

在PHP页面中,添加标题

// Send Headers
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="myPDF.pdf');

// Send Headers: Prevent Caching of File
header('Cache-Control: private');
header('Pragma: private');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
尝试玩header
header('Content-type:application/force-download')
自动下载您的文件


您还可以显示PDF数据,如文件是否已保存
readfile('original.PDF')

比公认的答案快得多。非常感谢。