What best practices should be followed when creating buttons to print a webpage in PHP to avoid issues like missing elements in the print preview?

When creating buttons to print a webpage in PHP, it's important to ensure that all necessary elements are included in the print preview. This can be achieved by using CSS media queries to specify the styles for printing, such as hiding unnecessary elements and adjusting the layout for better print readability.

<button onclick="window.print()">Print</button>

<style>
@media print {
  /* Hide elements not meant for printing */
  header, footer, .sidebar {
    display: none;
  }
  
  /* Adjust layout for better print readability */
  .content {
    width: 100%;
    margin: 0;
  }
}
</style>