1

I'm using printelement jQuery. Searching resource in stackoverflow and many others find some solution but still not working.

<script type="text/javascript">
function PrintElem(elem)
{
    Popup($(elem).html());
}

function Popup(data)
{
    var mywindow = window.open('', 'PRINT', 'height=400,width=600');
    mywindow.document.write('<html><head><title></title>');
    mywindow.document.write('<link rel="stylesheet" href="assets/css/neon-theme.css" type="text/css" />');
    mywindow.document.write('<link rel="stylesheet" href="assets/js/datatables/responsive/css/datatables.responsive.css" type="text/css" />');
    mywindow.document.write('</head><body >');
    mywindow.document.write(data);
    mywindow.document.write('</body></html>');

    mywindow.print();
    mywindow.close();

    return true;
}
illeas
  • 300
  • 3
  • 18
babu
  • 21
  • 1
  • 5

3 Answers3

2

Consider this answer from a similar thread

https://stackoverflow.com/a/33735423/3514785

The code looks something like this:

var divToPrint=document.getElementById('DivIdToPrint');
var newWin=window.open('','Print-Window');
newWin.document.open();
newWin.document.write('<html><body onload="window.print()">'+divToPrint.innerHTML+'</body></html>');
newWin.document.close();
setTimeout(function(){newWin.close();},10);
Community
  • 1
  • 1
Zuks
  • 1,247
  • 13
  • 20
  • Then I suspect something else is an issue. It works for me. What do you get? – Zuks Jan 19 '17 at 06:54
  • previously I used the html print whenever user prints the data it is generating webpartextension="full" with head tags at some point it throwing error (it is not printing )newWin.document.write(''+divToPrint.innerHTML+''); – karthik Oct 16 '17 at 18:19
0

Thats right. Use jquery html like so $('#divelement').html(yourehtmlcode).

HolgerT
  • 62
  • 1
  • 10
0

I think your css linking problem, Please try it :

<script type="text/javascript">
function PrintElem(elem)
{
    Popup($(elem).html());
}
function Popup(data)
{
    var myWindow = window.open('', 'PRINT', 'height=400,width=600');
    myWindow.document.write('<html><head><title></title>');
    myWindow.document.write('</head><body >');
    myWindow.document.write(data);
    myWindow.document.write('</body></html>');

    myWindow.print();
    myWindow.close();

    return true;
}
illeas
  • 300
  • 3
  • 18