html2canvas and @media print: Print Fully Styled HTML Without Print CSS
Raheel Shan
January 13th, 2025 · 6 min read
If you searched for how to use @media print with html2canvas, here is the short answer: html2canvas does not apply @media print rules, and it has no option to switch media type. It draws the page the way the screen shows it. So you either emulate the print layout yourself, or you skip print CSS and print an image of the finished layout. This post shows both, and where each one breaks.
Quick answer
- html2canvas reads the screen styles of your page, never the
@media printblock. - To get a print layout inside the canvas, add a class to the cloned document with the
oncloneoption and style that class. - To print exactly what you see on screen, render the element to an image, hide the page and show the image inside
@media print.
Why html2canvas ignores @media print
html2canvas does not take a screenshot. It rebuilds the page on a canvas from the computed styles of your elements. Those computed styles belong to the screen media type, so rules inside @media print are never part of the result. There is no media: 'print' option, even though many people look for one.
Option 1: Emulate print styles with onclone
onclone gives you the cloned document before it is drawn. Add a class there and write your "print" rules against it.
const canvas = await html2canvas(document.getElementById('content-to-print'), {
scale: 2,
useCORS: true,
backgroundColor: '#ffffff',
onclone: (clonedDocument) => {
clonedDocument.body.classList.add('print-mode');
},
});
.print-mode .no-print { display: none !important; }
.print-mode #content-to-print { width: 190mm; font-size: 12pt; }
Your original page is untouched; only the copy that html2canvas draws changes.
Option 2: Print the rendered image (no print CSS for the content)
This is the approach I used. Render the content to an image once, keep the image hidden, and show only that image when the browser prints.
#### Step 1: HTML structure Two siblings in the body: your page, and an empty image that will hold the result.
<div id="root-container">
<div id="content-to-print">
<!-- the content you want on paper -->
</div>
</div>
<img src="" alt="Printable version" id="printed-image" />
#### Step 2: Convert the content to an image
async function generateImage() {
const canvas = await html2canvas(document.getElementById('content-to-print'), {
useCORS: true, // allow cross-origin images
scale: 3, // sharper text on paper
backgroundColor: '#ffffff',
scrollX: 0,
scrollY: 0,
windowWidth: document.documentElement.scrollWidth,
windowHeight: document.documentElement.scrollHeight,
});
document.getElementById('printed-image').src = canvas.toDataURL('image/png');
}
document.addEventListener('DOMContentLoaded', generateImage);
function printPage() {
window.print();
}
You do not need jsPDF for this. The first version of this post created a PDF object that was never used.
#### Step 3: Show only the image when printing
#printed-image { display: none; }
@media print {
#root-container { display: none !important; }
#printed-image {
display: block !important;
margin: auto;
max-width: 100%;
}
}
Yes, this still uses one small @media print block. What goes away is the work of restyling every element for print, because the layout is already baked into the image.
Limits you should know about
- Text becomes pixels. It cannot be selected or searched, and it looks soft if
scaleis too low. Usescale: 2or3. - Canvas size limits. Browsers cap canvas dimensions (around 16,000 pixels on one side, and less on some mobile devices). Very long pages fail or print blank.
- No automatic page breaks. The image is one block. A long document needs to be rendered in slices, one per page.
- CORS and fonts. Cross-origin images need
useCORS: trueand proper headers on the image server. Web fonts must be loaded before you call html2canvas. - Memory. A large element at
scale: 3uses a lot of memory.
When to use real print CSS instead
If the content is text, tables or invoices, plain print CSS gives sharper output and real page breaks:
@page { size: A4; margin: 12mm; }
@media print {
body { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
.no-print { display: none !important; }
table, figure { break-inside: avoid; }
}
Use the image approach when the layout is complex, uses lots of backgrounds or charts, or must look identical everywhere. For a downloadable PDF, see Skip the Complexity: HTML to PDF Made Simple, or render the PDF on the server.
FAQ
Does html2canvas support @media print? No. It renders the screen styles. Use onclone to add a class, or print an image of the rendered element.
Is there a media option in html2canvas? No. The available options include scale, useCORS, backgroundColor, onclone, ignoreElements and the window size settings.
Why is my printed html2canvas image blurry? The scale is too low. Raise scale to 2 or 3, and set the image to max-width: 100% so the browser does not stretch it.
Can I print more than one page? Render the content in slices that match your page height, or use real print CSS with @page and page-break rules.
Get a second pair of eyes on your code
We open your repository together. I review structure, queries, security and naming.
You leave with fixes you can apply the same day.
Found this useful?
Support the work that goes into these posts.