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.