Reputation: 2412
I have tried to print the table of data within one page. Which having 200 or 300 rows. It should be printed in in single page like Online MS Excel print option -> Fit Sheet on One Page. How can i achieve this it is possible?
I have tried the below code,
var tble = document.createElement("table");
tble.id = "tble";
document.body.appendChild(tble);
var trtd = "<tr><td>123</td></tr>"
for (var i = 0; i < 300; i++) {
$("#tble").append(trtd);
}
var divs = "<div id='printing'></div>";
$("#printing").append($("#tble"));
$("#printData").click(function() {
var printToDiv = $("#printing");
var newWin = window.open("", "print-window");
newWin.document.open();
newWin.document.write('<html><body onload="window.print()">' + printToDiv.innerHTML + '</body></html>');
newWin.document.close();
setTimeout(function() {
newWin.close();
}, 10);
})
Printed content should be on one single page. Is there any option?
Upvotes: 1
Views: 1812
Reputation: 1075755
JavaSript code has no control whatsoever over the printing process other than initiating it. From that point forward, it's browser-specific and out of the control of your code.
The only way of doing this that I can see is to ask the user what page size they're using and with what margins (since the browser won't tell you), and then ensure that your table, when rendered, fits within the area inside the margins (e.g., by reducing/enlarging font size and such). You can, after all, get the rendered size in pixels via jQuery's outerWidth
and outerHeight
functions, and in general you can assume 96dpi (so pixels / 96 = inches).
Upvotes: 2