What are the common pitfalls when setting path parameters in PHP for PDF generation?

Common pitfalls when setting path parameters in PHP for PDF generation include not properly sanitizing user input, using relative paths instead of absolute paths, and not checking if the directory exists before saving the PDF file. To solve these issues, always validate and sanitize user input to prevent directory traversal attacks, use absolute paths to ensure the file is saved in the correct location, and check if the directory exists before saving the file.

// Validate and sanitize user input for the file name
$filename = filter_var($_POST['filename'], FILTER_SANITIZE_STRING);

// Set the absolute path to save the PDF file
$directory = '/path/to/save/pdf/';
if (!file_exists($directory)) {
    mkdir($directory, 0777, true);
}

// Generate the PDF file using TCPDF library
$pdf = new TCPDF();
$pdf->Output($directory . $filename . '.pdf', 'F');