Skip to content

Instantly share code, notes, and snippets.

@teknotica
Last active October 7, 2015 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save teknotica/bd06ddfa3460f9b3f207 to your computer and use it in GitHub Desktop.
Save teknotica/bd06ddfa3460f9b3f207 to your computer and use it in GitHub Desktop.
Dynamically loading iframe to print
function prepareIframe(src) {
var iframeDocument, iframeContent;
var iframe = document.getElementById("iframe-print");
if (!iframe || !src) {
return;
}
iframe.src = src;
// Apply delay to let iframe load content
$timeout(function() {
if (iframe.contentDocument) {
iframeDocument = iframe.contentDocument;
} else if (iframe.contentWindow) {
iframeDocument = iframe.contentWindow.document; // IE
}
// Print iframe content
if (iframeDocument) {
iframeContent = iframeDocument.getElementsByTagName('html')[0].innerHTML;
}
// Send iframe content for print
printIframe(iframeContent);
}, 4000);
}
// Print content from iframe
function printIframe(iframeHtml) {
var openWindow = window.open("", "Print chapter", "attributes");
openWindow.document.write(iframeHtml);
openWindow.document.close();
openWindow.focus();
openWindow.print();
openWindow.close();
}
/**
* [Print - delegated call to browser page print]
*
*/
$scope.printClickHandler = function($event, a)
{
$event.preventDefault();
$event.stopPropagation();
// Print current page by default
var action = a || 'current';
if (a == 'current') {
$window.print();
} else {
var iframeUrl = "http://url.for.iframe/";
prepareIframe(iframeUrl);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment