Skip to content

Instantly share code, notes, and snippets.

@smarteist
Last active October 29, 2020 14:45
Show Gist options
  • Select an option

  • Save smarteist/2056a0ed53663bc2cdad5471cd0cd468 to your computer and use it in GitHub Desktop.

Select an option

Save smarteist/2056a0ed53663bc2cdad5471cd0cd468 to your computer and use it in GitHub Desktop.
create printable window with java script
jQuery(document).ready(function ($) {
$("#printme").click(function () {
var bodyContent = '';
$('body').find('.printable').each(function (index, object) {
bodyContent += $(object).html();
});
var headStyles = '';
$('head').find('link[rel="stylesheet"]').each(function (index, object) {
headStyles += '<link href="' + $(object).attr('href') + '" rel="stylesheet"/>';
});
var html = $('html');
var dir = html.attr('dir') ? html.attr('dir') : 'ltr';
var printWindow = window.open(
"about:blank",
"PrintWindow",
"top=100,left=100,width=800,height=600,toolbar=0,scrollbars=1,resizable=1"
);
printWindow.document.write('<!DOCTYPE html>');
printWindow.document.write('<html dir="' + dir + '">');
printWindow.document.write('<head>');
printWindow.document.write(headStyles);
printWindow.document.write('<title>Print</title>');
printWindow.document.write('</head>');
printWindow.document.write('<body style="text-align: ' + (dir === 'rtl' ? 'right' : 'left') + '">');
printWindow.document.write(bodyContent);
printWindow.document.write('</body>');
printWindow.document.write('<script>setTimeout(function(){window.print();},1000)</script>');
printWindow.document.write('</html>');
printWindow.document.close();
printWindow.focus();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment