When is the best time to adjust content layout for printing in PHP, during database retrieval or after generating the HTML document?

When adjusting content layout for printing in PHP, it is best to do so after generating the HTML document. This allows for easier manipulation of the HTML structure and styling before sending it to the printer. By separating the content layout adjustment from the database retrieval process, you can focus on optimizing the display specifically for printing purposes.

<?php
// Database retrieval process
$data = fetchDataFromDatabase();

// Generate HTML document
$html = '<html>';
$html .= '<head><title>Printable Content</title></head>';
$html .= '<body>';
$html .= '<div class="content">';
foreach ($data as $row) {
    $html .= '<p>' . $row['content'] . '</p>';
}
$html .= '</div>';
$html .= '</body>';
$html .= '</html>';

// Adjust content layout for printing
// Add CSS styles for printing, such as hiding unnecessary elements
$html .= '<style type="text/css">
@media print {
    .content {
        font-size: 12pt;
    }
    /* Add more styles for printing layout adjustments */
}
</style>';

// Output the final HTML document
echo $html;
?>