Last active
February 5, 2020 18:13
-
-
Save MauricioMoraes/d3b235e35c83b2cf481a57f9e9aba07a to your computer and use it in GitHub Desktop.
Print the contents of a div with a given stylesheet
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
<html> | |
<body> | |
<div id='print-wrapper'> | |
<h1>CONTENT TO PRINT</h1> | |
<p>CONTENT TO PRINT</p> | |
<p>CONTENT TO PRINT</p> | |
<p>CONTENT TO PRINT</p> | |
<p>CONTENT TO PRINT</p> | |
<p>CONTENT TO PRINT</p> | |
<p>CONTENT TO PRINT</p> | |
<p>CONTENT TO PRINT</p> | |
</div> | |
<br> | |
<button id="print" onclick="doPrint()">PRINT</button> | |
<script> | |
function printElem(elem, titleText, headerText, stylesheet_url){ | |
var mywindow = window.open('', '_blank', "width="+screen.availWidth+",height="+screen.availHeight); | |
mywindow.document.write('<html><head><title>' + titleText + '</title>'); | |
mywindow.document.write('</head><body >'); | |
mywindow.document.write('<h1>' + headerText + '</h1>'); | |
mywindow.document.write(document.getElementById(elem).innerHTML); | |
var scriptTag = "<script>setTimeout(function(){window.print()}, 300)<\/script><\/html>" | |
mywindow.document.write('</body><link href="' + stylesheet_url + '" media="all" rel="stylesheet" />' + scriptTag); | |
mywindow.document.close(); // necessary for IE >= 10 | |
mywindow.focus(); // necessary for IE >= 10*/ | |
return true; | |
} | |
function doPrint() { | |
printElem('print-wrapper', 'Título', 'Header text', 'https://ordexa.com.br/assets/css/style.css'); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment