How can PHP be used to optimize table printing for different page sizes like DIN A4?

When printing tables for different page sizes like DIN A4, it is important to optimize the layout so that the table fits nicely on the page without cutting off any content. One way to achieve this is by using CSS styles for printing and adjusting the table width based on the page size. In PHP, you can dynamically calculate the table width based on the page size and set the appropriate styles for printing.

<?php
$pageSize = "A4"; // Set the page size (e.g., A4)
$tableWidth = "100%"; // Default table width

// Calculate table width based on page size
if($pageSize == "A4") {
    $tableWidth = "80%"; // Adjust table width for A4 size
} else if($pageSize == "Letter") {
    $tableWidth = "90%"; // Adjust table width for Letter size
}

// Output table with optimized width for printing
echo "<table style='width: $tableWidth;'>";
// Add table content here
echo "</table>";
?>