Print a very wide HTML table without clipping right hand side

后端 未结 4 1460
野趣味
野趣味 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 15:58

    Here is my go at this:

    • Demo and Code
    • Article

    What I did was:

    • Create a fixed width div that fits nicely on an A4 size page
    • Copied the table inside that div
    • Scroll the table x pixels to bring the desired portion in "focus" using CSS positioning
    • Repeat this process y times

    Example: if the table is 2000px wide and page width is set to 600px then y should be 4 and x will be 0, 600, 1200 and 1800.

    0 讨论(0)
  • 2020-12-08 16:02

    use

    @media print{@page {size: landscape}}
    
    0 讨论(0)
  • 2020-12-08 16:08

    See this question: Print Stylesheets for pages with long horizontal tables

    The point is that you can't get a browser to print horizontal tables the same way as excel does. You have to switch to something that is at most one page wide.

    0 讨论(0)
  • 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 = $("<div></div>").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);
    
    };
    
    0 讨论(0)
提交回复
热议问题