[Simplified version of this question:]
Why do iFrames sometimes refuse to print? For example, hit print preview on these two pages:
On print-preview, the iFrames are visible in the first example, but hidden in the second. The ONLY code difference between first and second iFrames is a CSS property (margin: 20px;) has been added to the iFrames in the second example.
[Long (original) version:]
I am building a preview/print page that assembles a bunch of independent HTML files referencing their own style-sheets and javascript files.
The idea is that all files are displayed in iFrames for preview, and then when printed, each iFrame (which likely will span multiple pages) prints out as if it were it’s own document.
However, when I print (or print-preview) the iFrames either show no content, or they only print the first page, truncating the remaining pages of the iFrame.
- http://fiddle.jshell.net/GJKkm/16/show/ (use this link to print-preview, not the one above)
The iFrames printed (and print-previewed) just fine before I made them multiple pages:
What is the reason for this behavior?
You can print the documents in the individual iframes by calling the
print()function on their windows….So something like this to print file 2:
And here a jsfiddle where I added buttons which trigger the individual prints…
http://jsfiddle.net/sg3s/GJKkm/17/
As for why the browser doesn’t print the content inside iframes when printing their parent document. Probably has to do something with the fact that those documents aren’t actually part of the document they’re shown in… Think of it like a separate window (one you may generate with
window.open()in js) but then skillfully hacked into the main document with the viewport limited to whatever is specified on the iframe with some magic.