Printing a document in JavaScript usually means triggering the browser’s print dialog and controlling what gets printed (a page, a section, or dynamically generated content). JavaScript itself doesn’t “print” directly to printers—it delegates printing to the browser via the Print API.
Here’s a clear guide to doing it properly.
Get the Military Open Source Intelligence (OSINT) Guide
window.print()The simplest way to print a page:
function printPage() {
window.print();
} This opens the browser’s print dialog and prints the entire page.
You can trigger it with a button:
<button > Often you don’t want the whole page printed—just a document, invoice, or report.
<div id="printArea">
<h1>Invoice #123</h1>
<p>Customer: John Doe</p>
<p>Total: ₦50,000</p>
</div>
<button > function printSection() {
const content = document.getElementById("printArea").innerHTML;
const printWindow = window.open("", "", "width=800,height=600");
printWindow.document.write(`
<html>
<head>
<title>Print</title>
</head>
<body>
${content}
</body>
</html>
`);
printWindow.document.close();
printWindow.print();
printWindow.close();
}
To control how printed pages look, use @media print.
@media print {
body {
font-size: 12px;
color: black;
}
.no-print {
display: none;
}
#printArea {
width: 100%;
}
}
<button class="no-print" > Anything with .no-print will be hidden when printing.
Instead of opening a new window, temporarily replace page content:
function printDiv() {
const originalContent = document.body.innerHTML;
const printContent = document.getElementById("printArea").innerHTML;
document.body.innerHTML = printContent;
window.print();
document.body.innerHTML = originalContent;
} ⚠️ Use carefully—this can reset JavaScript state on the page.
Example: generating a report before printing.
function printReport(data) {
let html = `
<h1>Sales Report</h1>
<ul>
`;
data.forEach(item => {
html += `<li>${item.name}: ₦${item.amount}</li>`;
});
html += "</ul>";
const win = window.open("", "", "width=800,height=600");
win.document.write(html);
win.document.close();
win.print();
}
If you want better control (recommended for invoices, receipts), generate a PDF first using libraries like:
Example with jsPDF:
import jsPDF from "jspdf";
const doc = new jsPDF();
doc.text("Hello World", 10, 10);
doc.save("document.pdf");
Then the user prints the downloaded file.
@media print for layout controlwindow.print() → simplest way to print everything@media print → controls layoutLatest tech news and coding tips.
Why borrowing code is a skill—when you understand what you're copying. For years, "copy-paste developer"…
Interactive forms rarely keep every field active all the time. Sometimes an input should only…
A modern JavaScript API for working with dates, times, time zones, and calendars without the…
Understanding What Happens Behind the Scenes Node.js looks simple from the outside—you write JavaScript, call…
The need for absolute certainty is the greatest disease the engineering mind faces. The moment…
The software industry is one of the most competitive places to build a career. Every…