NoReceipt4Panda NoReceipt4Panda - 2 years ago 65
PHP Question

How do you save a PDF with FDPF using parameters?

I have this button, that is tied to this function:

$('#genPDF').click(function () {

var str = "hText=something" +
"&cText=also something";

url: "/wp-content/themes/mytheme/indexpdf.php",
data: str,
cache: false,
success: function (result) {
$("#pdfobject").attr("src", "/wp-content/themes/mytheme/flyer.pdf");
var container = document.getElementById("pdfContainer");
var content = container.innerHTML;
container.innerHTML = content;

To explain what the successful ajax code does, first outputs "success!" in the console, which the browser does, then replaces a certain div on the page with a revised link (refreshing a certain part of the page).

This above code works, and makes it's way over to indexpdf.php, which is:


$hText = trim(isset($_GET['hText']) ? $_GET['hText'] : '');
$cText = trim(isset($_GET['cText']) ? $_GET['cText'] : '');


// initiate FPDI
$pdf = new FPDI();



$tplIdx = $pdf->importPage(1);

$pdf->useTemplate($tplIdx, 10, 10, 100);

$pdf->SetTextColor(255, 0, 0);
$pdf->SetXY(30, 30);
$pdf->Write(0, $hText.$cText);


The problem is, it's supposed to take testflyer.pdf, load it's first page and write my passed in arguments into it. THEN, save itself as flyer.pdf.

It's not saving, I don't know what's doing on or what the problem is.

All PDF's and PHP files above are in the /mytheme/ folder.

Answer Source

If you want to save the PDF, set the dest parameter as F: So,


must be:


And reformat your write line as:

$pdf->Write(0, "$hText $cText");

According to the documentation, destination where to send the document. It can be one of the following:

I: send the file inline to the browser. The PDF viewer is used if available.
D: send to the browser and force a file download with the name given by name.
F: save to a local file with the name given by name (may include a path).
S: return the document as a string.

The default value is I.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download