1

I am trying to create a pdf using fpdf, which opens in a browser window. Ultimately, the data will be passed from a form submit to a non-existent WP page called pdf.

I am checking for this in template_redirect hook, so if it is pdf being requested, it will generate the pdf, and exit, otherwise will continue for normal WP processes.

This works in Firefox, but in Chrome, an alert : Failed to open PDF document. If I call a page with just this code in, it also works in Chrome.

add_action("template_redirect", "checkRedirect");

function checkRedirect() {
    $url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    $slug = strtolower(basename($url));

    if ($slug == "pdf") {
        require_once(getPath() . "fpdf.php");

        $pdf = new \FPDF;
        $pdf->AddPage();
        $pdf->SetFont('Arial','B',16);
        $pdf->Cell(40,10,'Hello World!');
        $pdf->Output(); 
        die(); // to stop WP processing
    } 
}

Using the template_redirect hook, I believe WP has not output any data, so not sure what the problem is, and it works in Firefox.

2 Answers 2

0

Use ob_start();, ob_clean(); before outputting the PDF to make any uwanted content is not written while cretaing PDF.

add_action("template_redirect", "checkRedirect");

function checkRedirect() {
    $url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    $slug = strtolower(basename($url));

    if ($slug == "pdf") {
        ob_start();
        require_once(getPath() . "fpdf.php");
        $pdf = new \FPDF;
        $pdf->AddPage();
        $pdf->SetFont('Arial','B',16);
        $pdf->Cell(40,10,'Hello World!');
        ob_clean();
        $pdf->Output(); 
        exit; // this better then die
    } 
}
5
  • Thanks, but adding this (actually the flush), makes fpdf throw an exception : FPDF error: Some data has already been output, can't send PDF file (output started at :0) Commented Oct 5, 2022 at 10:53
  • Try with ob_clean() remove the flush(), chrome doesn't open the email most of time due some extra info passed to email template which invaild.
    – Onkar
    Commented Oct 5, 2022 at 10:59
  • I did try that, and back to the original issue. Commented Oct 5, 2022 at 11:00
  • Try like this, update the answer, I hope it works for you.
    – Onkar
    Commented Oct 5, 2022 at 11:09
  • No change I'm afraid, I also tried hooking into an earlier action (wp) and still has the same problem. Commented Oct 5, 2022 at 11:42
0

It is not ideal, but I have found a workaround. Instead of outputting directly, I am saving the pdf file, and then doing a redirection to it.

I will write a process to remove the files created so that they do not build up uncontrollably.

add_action("template_redirect", "checkRedirect");

function checkRedirect() {
    $url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    $slug = strtolower(basename($url));

    if ($slug == "pdf") {
        require_once(getPath() . "fpdf.php");

        $filePath = getPath() . "pdf/test.pdf";
        $fileUrl = getUrl() . "pdf/test.pdf";

        $pdf = new \FPDF;
        $pdf->AddPage();
        $pdf->SetFont('Arial','B',16);
        $pdf->Cell(40,10,'Hello World!');
        $pdf->Output("F",$filePath); 

        header('Location: '. $fileUrl);

        exit(); // to stop WP processing
    } 
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.