Last active
October 29, 2020 14:45
-
-
Save smarteist/2056a0ed53663bc2cdad5471cd0cd468 to your computer and use it in GitHub Desktop.
create printable window with java script
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
| 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