0

I have a function to print my page as follows:

$("#btn_print").click(function () {

    var divToPrint = document.getElementById('printdiv');
    var newWin = window.open('', 'Print-Window');

    newWin.document.open();
    newWin.document.write('<html><body onload="window.print()" style="font-family:consolas;@page { size:8.5in 11in; margin: 3cm };" >' + divToPrint.innerHTML + '</body></html>');

    newWin.document.close();
    setTimeout(function () {
        newWin.close();
    }, 10);

});

I need 3cm margin space on all sides I mean Top,Right, Bottom and Left.

What I doing is above as you can see:

@page { 
    size: 8.5in 11in;
    margin: 3cm;
};

But it is not working.

Leo
  • 665
  • 2
  • 9
  • 25
Santhucool
  • 1,656
  • 2
  • 36
  • 92

1 Answers1

0

Try to use this in your <style>:

body {
    margin-top: 3cm;
    margin-bottom: 3cm;
    margin-right: 3cm;
    margin-left: 3cm;
}

Or:

body {
    margin: 3cm 3cm 3cm 3cm;
}

If the above doesn't work try:

body {
    padding: 3cm 3cm 3cm 3cm !important;
}
Leo
  • 665
  • 2
  • 9
  • 25
  • it not working for me buddy. margin-left , margin-right working. But top, bottom not working!! – Santhucool Nov 18 '15 at 12:47
  • printdiv is what I rendered in my page and using print method it prints to another window. So I must add style to that not to my printdiv. – Santhucool Nov 18 '15 at 12:59
  • @Santhucool try using `padding` or `padding-top` `padding-bottom`. – Leo Nov 18 '15 at 13:07
  • @Santhucool check this out: http://stackoverflow.com/questions/5958699/difference-between-margin-and-padding – Leo Nov 18 '15 at 13:11
  • thanks for your valuable links. But sadly I tried padding also nothing got changed!! – Santhucool Nov 18 '15 at 13:17
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/95446/discussion-between-santhucool-and-leo). – Santhucool Nov 18 '15 at 13:23