← Back toHTML DOM

Print an image

Written byPhuoc Nguyen
Category
Level 2 — Intermediate
Created
08 Apr, 2020
You can print a web page by clicking the Print menu of browser or pressing the shortcut `Ctrl+P` (or `command+P` on macOS). Calling the `window.print()` function provides the same result which prints the entire page.
In order to print an image, we can insert the image element to a fake `iframe` element, and call the `print()` function on the iframe's window. Assume that the page has an image element and a print button as below:
<img id="image" src="/path/to/image.jpg" /> <button id="print">Print</button>
The printing image could be handled inside the button's `click` event:
// Query the element
const printBtn = document.getElementById('print');

printBtn.addEventListener('click', function() {
...
});

Create a fake iframe

// Create a fake iframe
const iframe = document.createElement('iframe');

// Make it hidden
iframe.style.height = 0;
iframe.style.visibility = 'hidden';
iframe.style.width = 0;

// Set the iframe's source
iframe.setAttribute('srcdoc', '<html><body></body></html>');

document.body.appendChild(iframe);

Insert the image when the iframe is ready

Despite the fact that the iframe source is a simple HTML, not a remote path as defined by the `src` attribute, we have to wait for the iframe to be loaded completely:
iframe.addEventListener('load', function () {
// Clone the image
const image = document.getElementById('image').cloneNode();
image.style.maxWidth = '100%';

// Append the image to the iframe's body
const body = iframe.contentDocument.body;
body.style.textAlign = 'center';
body.appendChild(image);
});
Invoke the `print()` function on the iframe's window as soon as the image is loaded:
iframe.addEventListener('load', function() {
...
image.addEventListener('load', function() {
// Invoke the print when the image is ready
iframe.contentWindow.print();
});
});
Tip

Remove the iframe

The dynamic iframe will be removed when user starts printing the image or closes the print window:
iframe.contentWindow.addEventListener('afterprint', function () {
iframe.parentNode.removeChild(iframe);
});
Pressing the Print button below to see it in action!
Photo by Rod Long on Unsplash

Demo

See also

Questions? 🙋

Do you have any questions? Not just about this specific post, but about any topic in front-end development that you'd like to learn more about? If so, feel free to send me a message on Twitter or send me an email. You can find them at the bottom of this page.
I have a long list of upcoming posts, but your questions or ideas for the next one will be my top priority. Let's learn together! Sharing knowledge is the best way to grow 🥷.

Recent posts ⚡

Newsletter 🔔

If you're into front-end technologies and you want to see more of the content I'm creating, then you might want to consider subscribing to my newsletter.
By subscribing, you'll be the first to know about new articles, products, and exclusive promotions.
Don't worry, I won't spam you. And if you ever change your mind, you can unsubscribe at any time.
Phước Nguyễn