Skip to content

Instantly share code, notes, and snippets.

@mjschutz
Last active November 9, 2019 23:09
Show Gist options
  • Save mjschutz/c920548d6d37ffcc1eb66f7eaff33cf9 to your computer and use it in GitHub Desktop.
Save mjschutz/c920548d6d37ffcc1eb66f7eaff33cf9 to your computer and use it in GitHub Desktop.
Function to print some HTML content on NW.js using a iFrame
function printR(htmlContent) {
let doc = nw.Window.get().window.document;
let iframe = doc.createElement("iframe");
iframe.style.display = 'none';
doc.body.appendChild(iframe);
iframe.onload = () => {
console.log('loaded');
if (typeof htmlContent === 'function') {
htmlContent(iframe);
} else {
iframe.contentDocument.body.innerHTML = htmlContent;
}
iframe.focus();
iframe.contentWindow.print({ autoprint: false });
iframe.contentWindow.onafterprint = function(){
doc.body.removeChild(iframe);
}
};
iframe.src = 'about:blank';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment