Last active
October 7, 2015 11:18
-
-
Save teknotica/bd06ddfa3460f9b3f207 to your computer and use it in GitHub Desktop.
Dynamically loading iframe to print
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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