打印HTML表格时没有边框

20

我正在制作一个打印表格的网站。

我遇到的一个问题是,有些表格边框在屏幕上正确显示,但在打印时却无法打印出来。

我尝试了Firefox和Chrome浏览器,它们在屏幕上都显示了所有的表格边框,但在打印时却省略了一些边框。

我需要怎么做才能打印出它们呢?

编辑1:增加了jsFiddle:

http://jsfiddle.net/Ax4qU/

代码:

JavaScript:

function printDiv()
{
  var divToPrint=document.getElementById('table');
  newWin= window.open("");
  newWin.document.write(divToPrint.outerHTML);
  newWin.print();
  newWin.close();
}

CSS:

<style type="text/css">

    html, body, div, span, object, iframe,
    h1, h2, h3, h4, h5, h6, p, blockquote, pre,
    abbr, address, cite, code,
    del, dfn, em, img, ins, kbd, q, samp,
    small, strong, sub, sup, var,
    b, i,
    dl, dt, dd, ol, ul, li,
    fieldset, form, label, legend,
    table, caption, tbody, tfoot, thead, tr, th, td {
        margin: 0;
        padding: 0;
        border: 0;
        outline: 0;
        font-size: 100%;
        vertical-align: baseline;
        background: transparent;
    }

    body {
        margin: 0;
        padding: 0;
        font: 12px/15px "Helvetica Neue", Arial, Helvetica, sans-serif;
        color: #555;
        background: #f5f5f5 url(bg.jpg);
    }

    a {
        color: #666;
    }

    #content {
        width: 65%;
        max-width: 690px;
        margin: 6% auto 0;
    }

    table {
        overflow: hidden
        border: 1px solid #d3d3d3;
        background: #fefefe;
        width: 70%;
        margin: 5% auto 0;
        -moz-border-radius: 5px; /* FF1+ */
        -webkit-border-radius: 5px; /* Saf3-4 */
        border-radius: 5px;
        -moz-box-shadow: 0 0 4px rgba(0, 0, 0, 0.2);
        -webkit-box-shadow: 0 0 4px rgba(0, 0, 0, 0.2);
    }

    th, td {
        padding: 18px 28px 18px;
        text-align: center;
    }

    th {
        padding-top: 22px;
        text-shadow: 1px 1px 1px #fff;
        background: #e8eaeb;
    }

    td {
        border-top: 1px solid #e0e0e0;
        border-right: 1px solid #e0e0e0;
    }

    tr.odd-row td {
        background: #f6f6f6;
    }

    td.first, th.first {
        text-align: left
    }

    td.last {
        border-right: none;
    }

    /*
    Background gradients are completely unnecessary but a neat effect.
    */

    td {
        background: -moz-linear-gradient(100% 25% 90deg, #fefefe, #f9f9f9);
        background: -webkit-gradient(linear, 0% 0%, 0% 25%, from(#f9f9f9), to(#fefefe));
    }

    tr.odd-row td {
        background: -moz-linear-gradient(100% 25% 90deg, #f6f6f6, #f1f1f1);
        background: -webkit-gradient(linear, 0% 0%, 0% 25%, from(#f1f1f1), to(#f6f6f6));
    }

    th {
        background: -moz-linear-gradient(100% 20% 90deg, #e8eaeb, #ededed);
        background: -webkit-gradient(linear, 0% 0%, 0% 20%, from(#ededed), to(#e8eaeb));
    }

    tr:first-child th.first {
        -moz-border-radius-topleft: 5px;
        -webkit-border-top-left-radius: 5px; /* Saf3-4 */
    }

    tr:first-child th.last {
        -moz-border-radius-topright: 5px;
        -webkit-border-top-right-radius: 5px; /* Saf3-4 */
    }

    tr:last-child td.first {
        -moz-border-radius-bottomleft: 5px;
        -webkit-border-bottom-left-radius: 5px; /* Saf3-4 */
    }

    tr:last-child td.last {
        -moz-border-radius-bottomright: 5px;
        -webkit-border-bottom-right-radius: 5px; /* Saf3-4 */
    }

</style>

2
当您在不同的窗口中打印DIV内容时,您确定所有CSS样式也被复制了吗? - K D
@KD。我不这么认为。我只是打开一个新窗口来打印表格。 - user2496520
7个回答

30
作为表格被复制到新窗口时,您的CSS不会被保留。 您可以通过在document.write()方法中传递一些相关的CSS来解决此问题。 您还需要提供一小部分填充来引入边框。 请参见以下JSFiddle以了解其操作方式:http://jsfiddle.net/826Zm/3/
function printDiv() {
    var divToPrint = document.getElementById('table');
    var htmlToPrint = '' +
        '<style type="text/css">' +
        'table th, table td {' +
        'border:1px solid #000;' +
        'padding:0.5em;' +
        '}' +
        '</style>';
    htmlToPrint += divToPrint.outerHTML;
    newWin = window.open("");
    newWin.document.write(htmlToPrint);
    newWin.print();
    newWin.close();
}

1
这非常好。它可以完美地适用于任何类型的表格。 - Ashwin Balani

3
尝试使用window.print()而不是printDiv(),因为您没有加载CSS。
或者,更新您的CSS为以下内容:
table {
    border: solid #000 !important;
    border-width: 1px 0 0 1px !important;
}
th, td {
    border: solid #000 !important;
    border-width: 0 1px 1px 0 !important;
}

或者这样。
@media print {
    table {
        border: solid #000 !important;
        border-width: 1px 0 0 1px !important;
    }
    th, td {
        border: solid #000 !important;
        border-width: 0 1px 1px 0 !important;
    }
}

使用 !important 是糟糕的 CSS 设计方式,应该尽量避免。 - Mike Koch
3
@MikeK 谢谢你的提示 :) 有很多东西我在网页上不会使用,但有时为了让事情正常工作,我只能使用它们。 - hex494D49

2

我认为另一个关于如何打印内联CSS样式的stackoverflow问题,How to print inline CSS styles?,可能对你的问题有答案。

另外一个尝试的方法是使用标准的<link rel="stylesheet" href="stylefile.css" type="text/css" media="print" >语法来设置样式表,这样你可以指定一个或多个媒体目标(只需要用逗号分隔即可)。


1
所以对我有效的方法是:
<div style = 'background-color: white; width: 800PX; border-style: solid ; border-color:  black ; padding: 12px;  margin: 10px; 'class = box>

我在 div 标签内使用了 style 标签。

1

回复“K D”评论,看起来你没有将CSS复制到新窗口中。我猜测你这样做是因为只想打印整个页面上的特定表格。有一种更简单的方法,定义一个打印样式表,排除除了要打印的元素以外的所有元素。不需要JavaScript、新窗口或复制任何内容。

<link rel="stylesheet" ref="myPrintStylesheet.css" type"text/css" media="print" />

myPrintStylesheet.css:

* {
    display: none;
}

#table {
    display: block;
}

我尝试了你的代码,但边框不可见 @MattK - user2496520
你是在同一个页面上尝试还是在单独的窗口中尝试?让我们看看你的代码。 - Matt K
我在同一页和同一个窗口上尝试它。我正在使用新标签页打印表格。 - user2496520
不确定您使用新标签打印表格的意思。它应该在同一页上,您能否发布一下您目前的进展? - Matt K

0

试试这个,用你的链接替换YOUR.css:

function printDiv() {
var strHtml = "<html>\n<head>\n <link rel=\"stylesheet\" type=\"text/css\" href=\"YOUR.css\">\n</head><body><div style=\"testStyle\">\n" + document.getElementById('table').innerHTML + "\n</div>\n</body>\n</html>";
newWin = window.open("");
newWin.document.writeln(strHtml);
newWin.print();
newWin.close();
}

document.getElementById('printbtn').addEventListener('click', printDiv);

0

在我的情况下,添加table-layout: fixed;解决了这个问题。最终我得到了以下结果:

table {
    border-collapse: collapse;
    table-layout: fixed;
    width: 100%;
}

th,
td {
    border: 1px solid #ddd;
    padding: 8px;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接