使用Jquery输出PDF文件


Output file PDF with Jquery

我需要你的帮助或建议,从jquery的输出文件pdf。我的输出文件有问题,因为pdf文件没有输出。这个脚本jquery和html:

<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();
?>

php脚本的结果是一个PDF文件,所以不能使用div。

您需要一个对象元素,如:

<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-data属性,并使用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");
        }
    });