How can a PHP script be used to prompt a user to download a PDF document instead of automatically opening it in the browser?

When a PDF document is accessed through a web browser, it typically opens within the browser itself. To prompt the user to download the PDF instead, you can use PHP to set the appropriate headers before outputting the file content. This forces the browser to download the file instead of displaying it.

<?php
// Path to the PDF file
$pdfFilePath = 'path/to/your/file.pdf';

// Set the appropriate headers to force download
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="' . basename($pdfFilePath) . '"');
header('Content-Length: ' . filesize($pdfFilePath));

// Output the PDF file
readfile($pdfFilePath);