Print an image
Written byPhuoc Nguyen
Created
08 Apr, 2020
Category
Level 2 — Intermediate
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:html
<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:js
// Query the element
const printBtn = document.getElementById('print');
printBtn.addEventListener('click', function() {
...
});
#Create a fake iframe
js
// 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:js
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:js
iframe.addEventListener('load', function() {
...
image.addEventListener('load', function() {
// Invoke the print when the image is ready
iframe.contentWindow.print();
});
});
TipThis post uses the Attach event handlers inside other handlers tip
#Remove the iframe
The dynamic iframe will be removed when user starts printing the image or closes the print window:
js
iframe.contentWindow.addEventListener('afterprint', function () {
iframe.parentNode.removeChild(iframe);
});
Pressing the Print button below to see it in action!
#Demo
#See also
Questions? 🙋
Do you have any questions about front-end development? If so, feel free to create a new issue on GitHub using the button below. I'm happy to help with any topic you'd like to learn more about, even beyond what's covered in this post.
While I have a long list of upcoming topics, I'm always eager to prioritize your questions and ideas for future content. Let's learn and grow together! Sharing knowledge is the best way to elevate ourselves 🥷.
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