Print a very wide HTML table without clipping right hand side

后端 未结 4 1499
野趣味
野趣味 2020-12-08 15:12

I have a table with several columns.

THE SPECIFIC PROBLEM

When you print such table, the columns on right will not print, not even when you print in landsc

4条回答
  •  北荒
    北荒 (楼主)
    2020-12-08 16:10

    The answer provided by @(Salman A) worked well for me on Chrome and Firefox. I modified it to break at column boundaries with the following:

    var acSplitTable = function() {
        var table = $(".ac-print .ac-grid>table"),
            tableWidth = table.outerWidth(),
            pageWidth = 600,
            pageCount = Math.ceil(tableWidth / pageWidth),
            printWrap = $("
    ").insertAfter(table), i, printPage; $(".ac-print .ac-grid>table .ac-hidden").remove(); var positions = []; var lastOuterWidth = 0; $(".ac-print .ac-grid>table th").each(function() { positions.push($(this).position().left); lastOuterWidth = $(this).outerWidth; }); var pageWidths = []; var endColumns = []; var lastPosition = 0; for (i = 1; i < positions.length; i++) { if ((positions[i] - lastPosition) > pageWidth) { pageWidths.push(positions[i - 1] - lastPosition); lastPosition = positions[i - 1]; endColumns.push(i - 1); } if (i == (positions.length - 1)) { pageWidths.push(positions[i] + lastOuterWidth - lastPosition); lastPosition = positions[i]; endColumns.push(i); } } pageCount = pageWidths.length; var lastEndColumn = 0; for (i = 0; i < pageCount; i++) { var thisPageWidth = pageWidth; //pageWidths[i]; var styleString = "overflow: hidden; width:" + thisPageWidth + "px; page-break-before: " + (i === 0 ? "auto" : "always") + ";"; var newTable = table.clone().attr("id", "ac-print-page-" + i); newTable.attr("style", styleString); newTable.appendTo("#formpoint"); //remove columns either side of our page for (var j = positions.length - 1; j >= 0; j--) { if (j > endColumns[i] || j <= lastEndColumn) { var index = j + 1; var heading = $(newTable).find("tr th:nth-child(" + index + ")"); $(newTable).find("tr th:nth-child(" + index + ")").remove(); $(newTable).find("tr td:nth-child(" + index + ")").remove(); } } lastEndColumn = endColumns[i]; } table.hide(); $(this).prop("disabled", true); window.print(); setTimeout(window.close, 0); };

提交回复
热议问题