2014-10-04 110 views
0

我需要你的帮助或来自输出文件pdf与jQuery的建议。我有输出文件的问题,因为pdf文件没有出来。这个脚本jQuery和HTML:用Jquery输出文件PDF

<script> 
$("#msg").hide(); 
$("button").click(function() { 
    var subject = $("#subject").val(); 
    var client = $("#client").val(); 

    $.ajax ({ 
     url: "http://localhost/report/fpdf_report.php", 
     type: "POST", 
     data: { subject: subject, client: client }, 
     success: function(data) { 
      $("#msg").html(data); 
      $("#msg").fadeIn("slow").delay(2000); 
     }, 
     error: function(xhr) { 
      alert(xhr+"error"); 
     } 
    }); 
}); 
</script> 

<input type="text" id="subject" /> <input type="text" id="client" /> <input type="text" id="client" /> <button>PDF</button> <br /> <div id="msg"></div>

所以对于PHP脚本转换PDF:

<?php 
$subject = $_POST['subject']; 
$client = $_POST['client']; 
require ('fpdf/fpdf.php'); 
$pdf = new FPDF(); 
$pdf -> AddPage(); 
$pdf -> SetFont('Arial','B',20); 
$pdf -> Cell(40,10,$subject); 
$pdf -> Cell(50,20,$client); 
$pdf -> Output(); 
?> 

回答

0

你的PHP脚本的结果是PDF文件,所以你不能使用DIV。

你需要像一个object元素:

<object id="msg" type="application/pdf" data="test1.pdf"> 
Unable to open the PDF file. 
</object> 

,你需要保存在一个文件中的PDF结果做这样的事情:

$filename = '/path/of/my/file/test.pdf'; 
$pdf -> Output($filename, 'F'); 
echo $filename; 

所以使用jQuery,你需要更改对象元素的de数据属性,以及Ajax POST的结果(结果将成为PDF文件的名称)。

$.ajax ({ 
     url: "http://localhost/report/fpdf_report.php", 
     type: "POST", 
     data: { subject: subject, client: client }, 
     success: function(data) { 
      $("#msg").attr("data", data); 
     }, 
     error: function(xhr) { 
      alert(xhr+"error"); 
     } 
    }); 
+0

亲爱的Tenhsor,感谢您的帮助:D – akashi 2014-10-09 13:05:11