Wednesday, October 5, 2022
HomeWordPress Developmenttemplate redirect - Creating FPDF immediately within the browser utilizing template_rediect

template redirect – Creating FPDF immediately within the browser utilizing template_rediect


I’m making an attempt to create a pdf utilizing fpdf, which opens in a browser window. In the end, the info will probably be handed from a kind undergo a non-existent WP web page referred to as pdf.

I’m checking for this in template_redirect hook, so whether it is pdf being requested, it should generate the pdf, and exit, in any other case will proceed for regular WP processes.

This works in Firefox, however in Chrome, an alert : Did not open PDF doc. If I name a web page with simply this code in, it additionally works in Chrome.

add_action("template_redirect", "checkRedirect");

operate 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,'Hi there World!');
        $pdf->Output(); 
        die(); // to cease WP processing
    } 
}

Utilizing the template_redirect hook, I consider WP has not output any knowledge, so undecided what the issue is, and it really works in Firefox.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments