9

I have the following function which works great.

<script type="text/javascript">
function printDiv(printpage)
{
var headstr = "<html><head><title></title></head><body>";
var newstr = document.all.item(printpage).innerHTML;
var oldstr = document.body.innerHTML;
document.body.innerHTML = headstr+newstr;
window.print();
document.body.innerHTML = oldstr;
return false;
}
</script>

However my div is about half of the paper size. Is there a way to make my div stretch to a full paper size when the function is called?

Hwende
  • 599
  • 2
  • 10
  • 25
  • you can try set `height:100%` – Grundy Dec 07 '13 at 20:37
  • Duplicate of [Stretch div upon print function](http://stackoverflow.com/questions/20444860/stretch-div-upon-print-function) – Anto Jurković Dec 07 '13 at 20:42
  • possible duplicate of [is it possible to print the contents of a div on an A4 using its whole width?](http://stackoverflow.com/questions/7664093/is-it-possible-to-print-the-contents-of-a-div-on-an-a4-using-its-whole-width) – cbr Dec 07 '13 at 20:43

1 Answers1

32

You can set the div size by css to fit it in a A4 size paper CSS:

   body {
    margin: 0;
    padding: 0;
    background-color: #FAFAFA;
    font: 12pt "Tahoma";
}
* {
    box-sizing: border-box;
    -moz-box-sizing: border-box;
}
.page {
    width: 21cm;
    min-height: 29.7cm;
    padding: 2cm;
    margin: 1cm auto;
    border: 1px #D3D3D3 solid;
    border-radius: 5px;
    background: white;
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
}
.subpage {
    padding: 1cm;
    border: 5px red solid;
    height: 237mm;
    outline: 2cm #FFEAEA solid;
}

@page {
    size: A4;
    margin: 0;
}
@media print {
    .page {
        margin: 0;
        border: initial;
        border-radius: initial;
        width: initial;
        min-height: initial;
        box-shadow: initial;
        background: initial;
        page-break-after: always;
    }
}

HTML:

<div class="book">
<div class="page">
    <div class="subpage">Page 1/2</div>    
</div>
<div class="page">
    <div class="subpage">Page 2/2</div>    
</div>
</div>

DEMO:http://jsfiddle.net/2wk6Q/3/

souvickcse
  • 7,742
  • 5
  • 37
  • 64