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
}
}