NoReceipt4Panda NoReceipt4Panda - 5 months ago 9
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";

$.ajax({
url: "/wp-content/themes/mytheme/indexpdf.php",
data: str,
cache: false,
success: function (result) {
console.log("Success!");
$("#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:

<?php

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

require_once('fpdf.php');
require_once('fpdi.php');

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

$pdf->AddPage();

$pdf->setSourceFile("TestFlyer.pdf");

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

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

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

$pdf->Output("D","flyer.pdf");
?>


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

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

 $pdf->Output("D","flyer.pdf");

must be:

 $pdf->Output("F","flyer.pdf");

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.

Comments