Skip to content

Instantly share code, notes, and snippets.

@w3farid
Created March 28, 2017 02:16
Show Gist options
  • Save w3farid/7301d40d92995cc2f85e534416ac5590 to your computer and use it in GitHub Desktop.
Save w3farid/7301d40d92995cc2f85e534416ac5590 to your computer and use it in GitHub Desktop.
<html>
<head>
<title>Print Test Page</title>
<script>
printDivCSS = new String ('<link href="myprintstyle.css" rel="stylesheet" type="text/css">')
function printDiv(divId) {
window.frames["print_frame"].document.body.innerHTML=printDivCSS + document.getElementById(divId).innerHTML;
window.frames["print_frame"].window.focus();
window.frames["print_frame"].window.print();
}
</script>
</head>
<body>
<h1><b><center>This is a test page for printing</center></b><hr color=#00cc00 width=95%></h1>
<b>Div 1:</b> <a href="javascript:printDiv('div1')">Print</a><br>
<div id="div1">This is the div1's print output</div>
<br><br>
<b>Div 2:</b> <a href="javascript:printDiv('div2')">Print</a><br>
<div id="div2">This is the div2's print output</div>
<br><br>
<b>Div 3:</b> <a href="javascript:printDiv('div3')">Print</a><br>
<div id="div3">This is the div3's print output</div>
<iframe name="print_frame" width="0" height="0" frameborder="0" src="about:blank"></iframe>
</body>
</html>
var prtContent = document.getElementById("your div id");
var WinPrint = window.open('', '', 'left=0,top=0,width=800,height=900,toolbar=0,scrollbars=0,status=0');
WinPrint.document.write(prtContent.innerHTML);
WinPrint.document.close();
WinPrint.focus();
WinPrint.print();
WinPrint.close();
@w3farid
Copy link
Author

w3farid commented Mar 28, 2017

https://github.com/etimbo/jquery-print-preview-plugin/tree/master/src
https://www.sitepoint.com/5-jquery-print-page-options/

myself try:

function openPrintDialogue(){
$('<iframe>', {
name: 'myiframe',
class: 'printFrame'
})
.appendTo('body')
.contents().find('body')
.append(<h1>Our Amazing Offer</h1> <img src='coupon.png' />);

window.frames['myiframe'].focus();
window.frames['myiframe'].print();

setTimeout(() => { $(".printFrame").remove(); }, 1000);
};

$('button').on('click', openPrintDialogue);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment