How do you save a PDF with FDPF using parameters?

时间:2016-04-15 15:00:39

标签: php jquery ajax pdf fpdf

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.

1 个答案:

答案 0 :(得分:1)

如果要保存PDF,请将dest参数设置为F: 所以,

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

必须是:

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

将您的写行重新格式化为:

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

根据documentation,目的地发送文件。它可以是以下之一:

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.

默认值为I.