I have several iframes in a page. I want to show in a print preview all the iframe contents as snapshots of iframes. I used window.print()
for individual iframes, and it's working fine, but how do I do it for multiple frames?

- 143,130
- 81
- 406
- 459

- 1,208
- 12
- 17
-
what do you mean by "snapshots of iframes"? – Michał Rybak Oct 22 '13 at 00:29
-
Just like print preview of the iframe contentwindow. – Srinath Thota Oct 22 '13 at 06:08
-
can this preview be a static image? – Michał Rybak Oct 22 '13 at 06:47
-
Yeah it can be it can be a pdf also – Srinath Thota Oct 22 '13 at 07:14
-
@ThotaSrinath, Hey, I'm also looking for the same functionality. But not able to find `print()` in `com.smartgwt.client.widgets.Window`. Can you help me? – RAS Oct 31 '13 at 06:03
5 Answers
You need to focus all frames one-by-one and merge in print report.
You can achieve it, with this code:
HTML:
<button id="printButton" onclick="javascript:printPage()" >Print this page</button>
<h1 id='header'><b>Awesome Print Report</b></h1>
<iframe id="ifr1" src="http://amazon.com"></iframe>
<iframe id="ifr2" src="http://amazon.com"></iframe>
<iframe id="ifr3" src="http://amazon.com"></iframe>
<iframe id="ifr4" src="http://amazon.com"></iframe>
<iframe id="ifr5" src="http://amazon.com"></iframe>
<iframe id="ifr6" src="http://amazon.com"></iframe>
JavaScript:
function printPage() {
window.print();
for (var k = 0; k < window.frames.length; k++) {
window.frames[k].focus();
window.frames[k].print();
}
}
CSS:
#header {
margin - top: 20px;
}
@media print {
#printButton {
display: none;
}
}
This CSS will hide print button on printed report.
Here is JsFiddle for you: http://jsfiddle.net/zur4ik/r7pvF/

- 6,072
- 9
- 52
- 78
-
this might be a good resource http://www.codingforums.com/javascript-programming/41713-print-preview-shows-only-1-first-print-page-iframe.html – konxie May 15 '14 at 13:42
-
The above answer doesn't take into account the content of the iframe. It leads to truncation and doesn't work for nested iFrames – jimgug Nov 24 '18 at 10:30
May this code helpful for you..
HTML:
<input type="submit" value="Print All"
onclick="javascript:printall()"
/>
<p>Hi! I'm a report!</p>
<iframe id="frame1" src="http://indiabix.com"></iframe>
<iframe id="frame2" src="http://indiabix.com"></iframe>
<iframe id="frame3" src="http://indiabix.com"></iframe>
<iframe id="frame4" src="http://indiabix.com"></iframe>
Script
function printall() {
window.print();
for (var i=0; i<window.frames.length; i++) {
window.frames[i].focus();
window.frames[i].print();
}
}
Fiddle : http://jsfiddle.net/ackMC/5/

- 20,190
- 6
- 38
- 35
If iframe
previews may be just static images, you can do the following:
- provide hidden image (with preview) next to each
iframe
- provide separate CSS file for printer using media queries
- in this file, hide the iframes and show the images
If you do it like this, you will have images with previews instead of iframes in print view.

- 8,648
- 3
- 42
- 54
-
Can u tell how could i convert the iframe content to an image? My idea is to convert the iframes into individual images and construct a pdf from those images. This pdf is easily printable. – Srinath Thota Oct 23 '13 at 12:34
-
Why not just do a printscreen of contents of `iframe`? generating PDFs is whole other topic. – Michał Rybak Oct 23 '13 at 12:38
-
Yeah i have multiple iframes in one web page and i want all the iframes in one print screen. – Srinath Thota Oct 27 '13 at 03:07
I think the biggest problem here is getting content from different frames into one view, which can be printed. Wether you want this in a .pdf or with window.print()
. Is it possible for you to get the content of all frames which need to be printed into one document (frame)? For instance With AJAX, cURL, or PHP-methods? I think that's the only way to make one printable document of it.
Good luck!

- 4,206
- 2
- 25
- 34
-
No this iframes contains cognos urls and they are drawn on the basis of urls. – Srinath Thota Oct 27 '13 at 03:10
YES it is possible to get the content of all iframes to be printed into the parent window. I achieved it with this code.
<script>
pages =[] // initiate an empty list here
function printPage() {
var frames = document.getElementsByTagName('iframe');
// get all the iframes and loop over them
// then push their innerHTML into the list
for (var i = 0; i < frames.length; i++){
pages.push(frames[i].contentWindow.document.body.innerHTML);
;
}
if (pages && pages.length) {
// this if statement, just checks to ensure our list is not empty before running the code.
// here is the magic, we now set the parent window to be equal to all the iframes innerHTML
window.content.document.body.innerHTML = pages;
// then we print this new window that contains all the iframes
window.print();
}
else {
// do nothing
}
}
with this solution you even avoid the problem of the iframe being cut off if it exceeds one page.
Plus, you get only one print dialogue box instead to print all the pages.
In your parent page HTML you will have the code below to call the printPage function.
<input type="submit" value="Print All"
onclick="javascript:printPage()"
/>

- 632
- 1
- 5
- 15