【问题标题】:How to achieve print with "Fit sheet on one page" using javascript如何使用javascript实现“在一页上适合纸张”的打印
【发布时间】:2017-12-08 08:46:32
【问题描述】:

我试图在一页内打印数据表。其中有 200 或 300 行。它应该像在线 MS Excel 打印选项 -> 将表格放在一页上一样打印在单页中。我怎样才能做到这一点?

我试过下面的代码,

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);
})

印刷的内容应该在一页上。有什么办法吗?

【问题讨论】:

  • 我会搜索类似css fit din a4 的内容,Google 会在那里找到一些有趣的 Stackoverflow 帖子。
  • 也许您可以从您的数据中生成 PDF 或 Excel 文件并让用户自己打印出来?

标签: javascript jquery css excel printing


【解决方案1】:

JavaSript 代码除了启动它之外,对打印过程没有任何控制权。从那时起,它是特定于浏览器的,并且不受您的代码的控制。

我能看到的唯一方法是询问用户他们使用的页面大小和边距(因为浏览器不会告诉你),然后确保你的表格在呈现时,适合边距内的区域(例如,通过减小/放大字体大小等)。毕竟,您可以通过 jQuery 的 outerWidth 和 outerHeight 函数获取以像素为单位的渲染大小,通常您可以假设为 96dpi(即像素 / 96 = 英寸)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-21
    • 1970-01-01
    • 2020-04-08
    • 1970-01-01
    • 2018-12-13
    • 2012-09-04
    • 2016-01-03
    相关资源
    最近更新 更多