简体   繁体   English

CSS 如何打印带有背景颜色的表格(不更改打印设置)

[英]CSS How to print a table with background color (without Print settings changes)

The aim is to print table with colored td-s.目的是用彩色 td-s 打印表格。 I need a way correct for all types of browsers.我需要一种适用于所有类型浏览器的方法。

Is there any way?有什么办法吗?

For webkit browsers (Chrome and Safari): 对于webkit浏览器(Chrome和Safari):

body{
  -webkit-print-color-adjust:exact;
}

Quote from here: CSS @media print issues with background-color; 从这里引用: CSS @media打印背景颜色问题;

IF a user has "Print Background colours and images" turned off in their print settings, no CSS will override that, so always account for that. 如果用户在其打印设置中关闭了“打印背景颜色和图像”,则没有CSS会覆盖它,因此始终考虑到这一点。 This is a default setting. 这是默认设置。

Once that is set so it will print background colours and images, what you have there will work. 一旦这样设置,它将打印背景颜色和图像,你有什么工作。

It is found in different spots. 它存在于不同的地方。 In IE9beta it's found in Print->Page Options under Paper options 在IE9beta中可以在Paper选项下的Print-> Page Options中找到它

In FireFox it's in Page Setup -> [Format & Options] Tab under Options. 在FireFox中,它位于选项下的页面设置 - > [格式和选项]选项卡中。

一个特别难看的解决方案是在表格单元格中放置一个.gif(或任何形式的矢量图形以便大小可以改变)图像,其高度和宽度为100%,然后是负z-index属性。

There is no solution that isn't horribly ugly, like positioning image or ridiculously large border underneath the table. 没有任何解决方案不是非常丑陋,比如在桌子下面定位图像或可笑的大边框。

It depends what you need those backgrounds for. 这取决于你需要那些背景。 If it's decorative, then it may be better not to force it. 如果它是装饰性的,那么最好不要强迫它。

If it's to highlight some table cells, you can use color border in addition to the background. 如果要突出显示某些表格单元格,除了背景外,还可以使用颜色border Borders are printed. 边框是印刷的。

If it's very important that backgrounds are printed, then you may give users a PDF to print. 如果打印背景非常重要,那么您可以为用户提供PDF打印。

I found @willert's solution a bit consistent and easy. 我发现@ willert的解决方案有点一致且容易。 I have developed it further, hope it helps you guys... 我进一步发展了它,希望它可以帮助你们......

table {
    margin-top: 20px;
    border-collapse: separate;
    border-spacing: 0px;
    font:13px/1.5 'Helvetica Neue', Arial, 'Liberation Sans', FreeSans, sans-serif;
}

table td,
table th {
    background-color: transparent;
    z-index: 1;
    border-right: 0;
    border-bottom: 0;
    width: 150px;
    padding-left: 5px;
    overflow: hidden;
    height: 12px;
}

table th:before,
table td:before {
    content: "";
    padding: 0;
    height: 1px;
    line-height: 1px;
    width: 1px;
    margin: 10px -994px -996px -2px;
    display: block;
    border: 0;
    z-index: -1;
    position:relative;
    top: -6px;
}

table th:before {
    border-top: 999px solid #D6D6D6;
    border-left: 999px solid #D6D6D6;
}

table td:before {
    border-top: 999px solid #F9F9F9;
    border-left: 999px solid #F9F9F9;
}
td div.grid_3{
    overflow: hidden;
}
table .row td{
    border-bottom: #d6d6d6 1px solid;
    width: 110px;
}

Judging from my research on this, you're not likely to find a CSS solution that covers everything. 从我对此的研究来看,你不太可能找到涵盖所有内容的CSS解决方案。 You might be able to apply a jQuery solution to this. 您可以将jQuery解决方案应用于此。

<style>
.cell_pos {
    position: relative;
}
.cell_cont {
    position: absolute;
    top: 0;
    left: 0;
    z-index: 10;
}
.img_color {
    position: absolute;
    top: 0;
    left: 0;
    z-index: 9;
    width: 100%;
}
</style
<table>
    <tr class="row1">
        <td class="selected">
            <div class="cell_pos">
                <div id="cell_cont">Hello</div>
            </div>
        </td>
    </tr>
</table>

<script type="text/javascript">
    $(document).ready(function(){
        $('#row1 .selected).append('<img class="img_color" src="path/to/img.jpg/>');
    )};
</script>

The question was a little vague so you would probably need some work around with the css with overflow and setting cell/column widths. 问题有点模糊,所以你可能需要一些解决css overflow和设置单元格/列宽度的工作。 Hope this helps. 希望这可以帮助。

只需使用!important的CSS,它将在Chrome中打印。

I've been playing around to solve this problem, and CSS content generation and modern browsers seem to provide a nice solution to this problem. 我一直在努力解决这个问题,CSS内容生成和现代浏览器似乎为这个问题提供了一个很好的解决方案。 NOT WIDELY TESTED, FEEDBACK WELCOME! 没有广泛的测试,反馈欢迎!

table {
  border-collapse: separate;
  border-spacing: 0px;
}

table td,
table th {
    background-color: transparent;
    overflow: hidden;
    z-index: 1;
    border-right: 0;
    border-bottom: 0;
}

table th:before,
table td:before {
    content: "";
    padding: 0;
    height: 1px;
    line-height: 1px;
    width: 1px;
    margin: -4px -994px -996px -6px;
    display: block;
    border: 0;
    z-index: -1;
    position:relative;
    top: -500px;
}

table th:before {
    border-top: 999px solid #c9c9c9;
    border-left: 999px solid #c9c9c9;
}

table td:before {
    border-top: 999px solid #eeeeee;
    border-left: 999px solid #eeeeee;
}

You will need to handcraft the details to fit your needs. 您需要手工制作细节以满足您的需求。

HTH HTH

You can do a little coloring the background by putting the contents in as an input-tag of type submit and set value to John Doe. 您可以通过将内容作为提交类型的输入标记并将值设置为John Doe来对背景进行一些着色。 then use class or style to manipulate the coloring. 然后使用类或样式来操纵着色。 Never the less the browser will change the forecoler to black so only use light backgrounds but it works (a little). 从来没有那么浏览器会将forecoler改为黑色,所以只使用浅色背景,但它可以工作(一点点)。

This works for me and wont affect your other CSS code: 这适用于我,不会影响您的其他CSS代码:

@media print {
    div {
       background: #333333 !important;
    }
}
            <td><img src="your_image" /></td>

This silly solution worked for me on Firefox and Chrome (Ubuntu). 这个愚蠢的解决方案适用于Firefox和Chrome(Ubuntu)。

Firefox: Open the print dialog box (shortcut: Ctrl + P ) and goto Options tab, and make sure that the option Print Background Colors is ticked. Firefox:打开打印对话框(快捷键: Ctrl + P )并转到“ Options卡,并确保选中“ Print Background Colors ”选项。

Firefox的打印设置

Chrome: Open the print dialog box (shortcut: Ctrl + P ) and expand More settings and tick Background graphics . Chrome:打开打印对话框(快捷方式: Ctrl + P ),然后展开More settings并勾选Background graphics

Chrome的打印设置

As far as I have tested, -webkit-print-color-adjust:exact; 据我测试, -webkit-print-color-adjust:exact; doesn't work for Firefox and is honored in Chrome. 不适用于Firefox,并在Chrome中受到尊重。 But in both the browsers the final printing option overrides all that. 但在两种浏览器中,最终的打印选项都会覆盖所有这些选项。 So you can completely do away without it. 所以你可以完全没有它。

Try printing the following sample HTML page with and without the various settings described here and you will know. 尝试使用和不使用此处描述的各种设置打印以下示例HTML页面,您将知道。

 <!DOCTYPE html> <html> <head> <style> body { -webkit-print-color-adjust: exact; } </style> </head> <body> <table style="border-width:1px;width:50%;"> <tr style="background-color:#999999"> <td>First Row</td> </tr> <tr style="background-color:#CCCCCC"> <td>Second Row</td> </tr> </table> </body> </html> 

For chrome you can use this: 对于chrome,您可以使用:

-webkit-print-color-adjust:exact;

Or in print preview, tick the More settings->Background Graphics 或者在打印预览中,勾选更多设置 - >背景图形

For Firefox, you can't enable it using any CSS. 对于Firefox,您无法使用任何CSS启用它。 You can enable it as follow (If you don't see the File, press Alt key once). 您可以按照以下方式启用它(如果没有看到文件,请按Alt键一次)。 File->Page setup and tick the Print Background(color & images) 文件 - >页面设置并勾选打印背景(颜色和图像)

I needed to use this in the weasyprint library that generates pdf's from python.我需要在从 python 生成 pdf 的 weasyprint 库中使用它。 if all you want to do is print a pdf then use the @print property with !important如果您只想打印 pdf 文件,请使用带有 !important 的@print 属性

eg例如

<!DOCTYPE html>
<html>
    <head>
        <style>
            @media print {
                .a {
                    background: orange !important;
                }
                .b {
                    background: blue !important;
                }
            }
        </style>
    </head>

    <body>

        <table>
            <tr>
                <td class="a">This cell has an orange background.</td>
                <td class="b">This cell has a green background.</td>
            </tr>
        </table>
    </body>

</html>

You will notice that if you open this in a web browser you wont see the colors, put if you print it as a pdf you will.您会注意到,如果您在网络浏览器中打开它,您将看不到颜色,如果您将其打印为 pdf,您将看到。 because those styles are applied on printing因为这些样式应用于印刷

if you wanted to also view the colors in the browser you would add the same styles as normal outside of print如果您还想在浏览器中查看颜色,您可以在打印之外添加与正常相同的样式

<!DOCTYPE html>
<html>
    <head>
        <style>
                .a {
                    background: orange;
                }
                .b {
                    background: blue;
                }

            @media print {
                .a {
                    background: orange !important;
                }
                .b {
                    background: blue !important;
                }
            }
        </style>
    </head>

    <body>

        <table>
            <tr>
                <td class="a">This cell has an orange background.</td>
                <td class="b">This cell has a green background.</td>
            </tr>
        </table>
    </body>

</html>

for printing PDF's you only need the @print property要打印 PDF,您只需要 @print 属性

Apply a class to the background with setting background image and.important.将 class 应用于设置背景图像和背景。重要。 The method does not require setting background printing.该方法不需要设置后台打印。

HTML HTML

<td class="red"></td> <td class="红色"></td>

CSS CSS

.red { background-image: url('images/bg-red.jpg');important; .red { background-image: url('images/bg-red.jpg'); 重要; } }

2023, maybe it's quite late. 2023年,也许已经很晚了。

I tried all suggested solutions here (and in many other questions), such as applied background-color: #000;important;我在这里(以及许多其他问题)尝试了所有建议的解决方案,例如 applied background-color: #000;important; both in stylesheet and inline, or set在样式表和内联中,或设置

@media print {
  * {
    color-adjust: exact !important;
    -webkit-print-color-adjust: exact !important;
    print-color-adjust: exact !important;
  }
}

, even combined them together, but nothing worked. ,甚至将它们组合在一起,但没有任何效果。

After hours of researching without any results, I recognized that the " bug " lost background-color only appears on table (th, td), but other HTML elements (div,...) or other CSS attributes (border-color,...) still work.经过数小时的研究但没有任何结果,我认识到丢失背景颜色的“错误”只出现在表(th,td)上,但其他 HTML 元素(div,...)或其他 CSS 属性(边框颜色,. ..) 仍然有效。

Therefore, I came up with a " hack " to wrap-up anything inside <th> or <td> with a <div> (you can adjust padding to make it display same as prior).因此,我想出了一个“ hack ”来用<div>包裹<th><td>中的任何内容(您可以调整填充以使其显示与之前相同)。

Here I used React and makeStyles of @material-ui/core .这里我使用了 React 和@material-ui/core makeStyles

JSX: JSX:

<Table bordered>
  <thead className={classes.thead}>
    <tr>
      <th><div>Col 1</div></th>
      <th><div>Col 2</div></th>
      <th><div>Col 3</div></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td className={classes.tdGreen}><div>Row 1 - Col 1</div></td>
      <td><div>Row 1 - Col 2</div></td>
      <td><div>Row 1 - Col 3</div></td>
    </tr>
    <tr>
      <td><div>Row 2 - Col 1</div></td>
      <td className={classes.tdBlue}><div>Row 2 - Col 2</div></td>
      <td><div>Row 2 - Col 3</div></td>
    </tr>
  </tbody>
</Table>

Styles: Styles:

const useStyles = makeStyles(() => ({
  thead: {
    textAlign: 'center',

    '& th:not(:last-child)': {
      padding: '0',
      '& > div': {
        padding: '.75rem',
        backgroundColor: '#D8D8D8 !important',
      }
    }
  },
  tdGreen: {
    padding: '0 !important',
    '& > div': {
      padding: '.75rem',
      color: 'white',
      backgroundColor: 'green !important',
    }
  },
  tdBlue: {
    padding: '0 !important',
    '& > div': {
      padding: '.75rem',
      color: 'white',
      backgroundColor: 'blue !important',
    }
  }
}));

You can take the idea and convert it into pure HTML/CSS solutions.您可以接受这个想法并将其转换为纯 HTML/CSS 解决方案。

Hope this can help anyone struggled with this issue!希望这可以帮助任何为这个问题而苦苦挣扎的人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM