W3docs

How to add a print button to a web page

To add a print button to a web page using PHP, you can use the following steps:

To add a print button to a web page, you can use the following steps:

  1. Create a button element in your HTML code with a unique id, such as "print-button".

Example of creating a button element in the HTML

<button id="print-button">Print</button>
  1. Use JavaScript to attach an event listener to the button element that listens for a click event.

Example of attaching an event listener to a button element with JavaScript

document.getElementById("print-button").addEventListener("click", function() {
    window.print();
});
  1. Optionally, you can add a print-specific stylesheet to your HTML and use CSS media queries to hide elements on the page that you don't want to print.

Example of adding a print stylesheet to a page

<link rel="stylesheet" type="text/css" media="print" href="print.css">

Syntax of @media print

@media print {
    .no-print {
        display: none;
    }
}

Finally, you should test the print button in different web browsers and make sure that it works as expected.