Last active
November 10, 2016 21:01
-
-
Save schuyberg/14f4a117ff136377e3fe to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* AngularJS Print Service | |
thanks to Bahmni for the original code at https://github.com/Bhamni/openmrs-module-bahmniapps/blob/master/ui/app/common/ui-helper/printer.js | |
** modified here to work with ie10/11 */ | |
services.factory('printer', [ '$rootScope', '$compile', '$http', '$timeout', 'utility', function ($rootScope, $compile, $http, $timeout, utility) { | |
var printHtml = function (html) { | |
var hiddenFrame = $('<iframe name="printframe" style="position: absolute; left: -9999px;"></iframe>').appendTo('body')[ 0 ]; | |
var htmlContent = "<!doctype html>" + | |
'<head><meta http-equiv="X-UA-Compatible" content="IE=edge"></head>' + | |
"<html>" + | |
'<body>' + | |
html + | |
'</body>' + | |
"</html>"; | |
var doc = hiddenFrame.contentWindow.document.open("text/html", "replace"); | |
doc.write(htmlContent); | |
doc.close(); | |
console.log('done'); | |
$(hiddenFrame).load(function(){ | |
if (!hiddenFrame.contentDocument.execCommand('print', false, null)){ | |
hiddenFrame.contentWindow.focus(); | |
hiddenFrame.contentWindow.print(); | |
} | |
$(hiddenFrame).remove(); | |
}); | |
}; | |
var openNewWindow = function (html) { | |
var newWindow = window.open("printTest.html"); | |
newWindow.addEventListener('load', function () { | |
$(newWindow.document.body).html(html); | |
}, false); | |
}; | |
var print = function (templateUrl, data) { | |
$http.get(templateUrl).success(function (template) { | |
var printScope = $rootScope.$new(); | |
angular.extend(printScope, data); | |
var element = $compile($('<div>' + template + '</div>'))(printScope); | |
var waitForRenderAndPrint = function () { | |
if ( printScope.$$phase || $http.pendingRequests.length ) { | |
$timeout(waitForRenderAndPrint); | |
} else { | |
// Replace printHtml with openNewWindow for debugging | |
printHtml(element.html()); | |
printScope.$destroy(); | |
} | |
}; | |
waitForRenderAndPrint(); | |
}); | |
}; | |
return { | |
print: print | |
} | |
} ]); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment