How can PHP be used to ensure that text and images are displayed clearly and legibly when printed?

To ensure that text and images are displayed clearly and legibly when printed, you can use PHP to generate a printer-friendly version of your content. This can be achieved by creating a separate CSS file specifically for printing, which includes styles that optimize the layout for printing, such as removing unnecessary elements, adjusting font sizes, and setting appropriate margins.

<?php
// Create a separate CSS file for printing
$css = "
    @media print {
        /* Add styles for printing here */
        body {
            font-size: 12pt;
        }
        img {
            max-width: 100%;
            height: auto;
        }
    }
";

// Save the CSS to a file
file_put_contents('print.css', $css);

// Link the CSS file in your HTML document
echo '<link rel="stylesheet" type="text/css" href="print.css" media="print">';
?>