I have a documentation type page with an iframe inside. I'm trying to override standard browser print (Ctrl + p) to print contents of an iframe only.
I know how to print an iframe content using javascript:
window.frames['webcontent'].focus();
window.frames['webcontent'].print();
I know how to do run javascript before printing e.g. as described here: Check for when a user has selected to print using javascript
Any advise?
Thanks
It's not possible (using Javascript). There is some experimental support for user-initiated print events in modern browsers, but those are not cancelable ("simple events") so the entire page will still print even if you interject custom code to print the frame of interest.
Given this limitation, your best bet is probably to offer users a large button that fires your custom frame printing function (see
printContentFrameOnly
below, fire it without arguments) and hope that they'll use the button instead of ctrl-p.If it would be possible, this would be the way to do it (based on this answer):
You can define a css file for printing:
EDIT
Mybad didnt tested it.
But this is working like a charm
The only way i can think of is hiding all the content in the document except for the iframe and making it fit the whole document.
I used timeout ( nasty i know ) because at least chrome 38 does not send the keyup event after ctrl+p
Hi might be this code will help you..
and call the function PrintElem('iframe') on you page.
The idea is to set the iframe content somewhere on the page, and print ONLY that content, by hiding the original content.
This can be done by getting the iframe content when Ctrl+P event is being initiated (via JavaScript), and print only its content (via CSS @media type).
HTML Code:
CSS Code:
JavaScript Code:
Brother, this can be easily achieved through CSS: See thisJSfiddle: Tested
Let an HTML File be: