Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

php - using AJAX and Jquery to create pdf using FPDF

I am trying to create pdf file using FPDF.

What I right now have is an HTML page with various lines of data and a print button next to it. When someone clicks Print, I am sending corresponding data using Jquery by making AJAX call..

This is my JQUERY code:

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

this is 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();

 ?>

But I am not getting PDF in return. Whats wrong?? In fact, nothings happening.. I want to retreive the pdf file and send it to print

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should open a new page using <a> tag with your PHP page print.php with your variables.

<a href="print.php?data" target="_blank">click me to download the file</a>

In the PHP page, add headers

// 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');

Try to play with header header('Content-type: application/force-download'); to download automatically your file.

You could also display your PDF data like if the file has been saved readfile('original.pdf');


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...