What potential security risks should be considered when generating PDFs in PHP, especially in relation to injection attacks?

When generating PDFs in PHP, especially when dealing with user input, it's important to consider the potential security risks of injection attacks. This includes preventing malicious users from injecting scripts or code into the PDF file that could harm the system or compromise sensitive data. To mitigate this risk, always sanitize and validate user input before including it in the PDF generation process.

// Sanitize and validate user input before generating PDF
$user_input = $_POST['user_input'];

// Example of sanitizing user input using htmlspecialchars
$clean_user_input = htmlspecialchars($user_input, ENT_QUOTES, 'UTF-8');

// Generate PDF with sanitized user input
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10, $clean_user_input);
$pdf->Output();