Why is Google Chrome not printing table and cell borders, and cell background colors?
Asked Answered
I

6

19

I'm maintaining a Web Application created using GWT, and which historically was developed to work only in Google Chrome.

There are several "reports" that can be extracted from the system. The way this is implemented is as follows: first, the html for the report is created and displayed in a section of the screen (this works correctly). After that, there is a button which allows to print the report. This action generates the html with the information (in a html table) the same way it would to create the on-screen report, and uses it to create a html file that is then sent to print.

This is where the monster shows its face! (tum-dum-dummmm - dramatic music).

Since Google Chrome updated to verssion 59.0.3071.86, when printing, the table/cells lose their borders, and background colors.

I thought maybe it had comething to do with the generated html, but in fact if I force the html upon printing to be

<!DOCTYPE html>
<html>
   <head>
      <style>table, th, td {border: 1px solid black !important;}</style>
      <title>title</title>
   </head>
   <body>
      <h2>Add a border to a table:</h2>
      <table>
         <tr>
            <th>Firstname</th>
            <th>Lastname</th>
         </tr>
         <tr>
            <td>Peter</td>
            <td>Griffin</td>
         </tr>
         <tr>
            <td>Lois</td>
            <td>Griffin</td>
         </tr>
      </table>
   </body>
</html>

the table still does not get printed correctly (and this is seen also in the Chrome's print preview (yes, even with the option to print background images selected)). The same thing happens if I style the table upon the table's tag itself. Something like:

<table style="border: 1px solid black !important;">

I'm out of ideas, so I'm asking if anybody got into a similar issue, or has any idea about what can I try.

Thanks in advance.

And edit to say that this is not the same problem as the one in the "possible duplicates". I had the "background colors" problem in the past, and I've fixed it. In this case (and as the code-snippet shows) it seems some styles are not interpreted, like for instance the table border. And everything was working prior to chrome 59.0.3071.86, so it seems to be a new issue, not a "solved somewhere in a 5 years' span" one.

And yet another edit to explain a little further:

somewhere in my webapp, there's a little button that reads "print report". This generates an html file which is then sent to print. I replaced the generated html with a rather simplistic one that ilustrates my problem. So, everytime I press "Print Report", I "force" this to be the generated html:

<!DOCTYPE html>
<html>
   <head>
      <style>table, th, td {border: 1px solid black !important;} body { -webkit-print-color-adjust: exact; } </style>
      <title>title</title>
   </head>
   <body>
      <h2>Add a border to a table:</h2>
      <table>
         <tr>
            <th>Firstname</th>
            <th>Lastname</th>
         </tr>
         <tr>
            <td bgcolor="#FF0000">Peter</td>
            <td>Griffin</td>
         </tr>
         <tr>
            <td>Lois</td>
            <td>Griffin</td>
         </tr>
      </table>
   </body>
</html>

Notice how I use the

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

Also, I tried some variations like styling the table as:

<table style="border: 1px solid black !important;">

Or the body as

<body style="-webkit-print-color-adjust: exact;">

I'll just add an image from an "html testbed" to show how my omnipresent html should display:

Table with Borders and TD's Background

However, when the print preview "pops", this is what I get:

I preview some mistake

So, neither does the backgound color displays (as I believe happens to other SO users, but with their solution not working here) nor the table borders.

Other edit: I tried defining "print-specific css rules", as follows:

<!DOCTYPE html>
<html>
   <head>
      <style>
         @media print { 
             body { -webkit-print-color-adjust: exact; } 
             table, th, td {border: 1px solid black !important;} 
         }  
      </style>
      <title>title</title>
   </head>
   <body>
      <h2>Add a border to a table:</h2>
      <table>
         <tr>
            <th>Firstname</th>
            <th>Lastname</th>
         </tr>
        <tr>
           <td bgcolor=\"#FF0000\">Peter</td>
           <td>Griffin</td>
        </tr>
        <tr>
           <td>Lois</td>
           <td>Griffin</td>
        </tr>
    </table>
   </body>
</html>

but I get the same results as before. Also (and maybe this is relevant... and maybe it isn't) if I define, for instance, a table width, THAT styling is correctly displayed upon printing, whilst the table borders are still hidden/not present/magically removed (chose one).

FINAL EDIT - workaround: I was not able to solve the problem per se, and I really REALLY do not understand why it happens. What I was able to do was get a workaround working, and I'm posting it here not as an answer, but still hoping it could help somebody.

The way I did it was: when I pressed the print button, I open a new window having only the html that I want to print (exactly as I want it printed), and using javascript, I make it print after the page is loaded. Doing it this way, it works, and it prints with the correct formatting.

Some might say it is luck, others "black magic". I say "Done! Next!" (Although I'm not happy with the fact I don't understand the problem in the first place.)

Irrelevance answered 16/6, 2017 at 10:27 Comment(15)
Possible duplicate of Print Background colours in ChromeDiazotize
Just yesterday saw the same question tooDiazotize
It is not. But thanks anyway.Irrelevance
The second question was from yesterday not from 5 years ago and it solved his problem. body { -webkit-print-color-adjust: exact; }. Post an image of what chrome shows you in the print menu if I'm wrong about that.Diazotize
Again, it does not solve my problem. Thanks anyway.Irrelevance
Try to use it as a media query for print (@media print)Diazotize
Nope, still the same problem, I'm afraid. :-/ I'll try some sort of sacrifice next...Irrelevance
Up-voted, but still can't reproduce it. I tried in chrome 59.0.3071.109 (Official Build) (64-bit). image showcaseDiazotize
Thaks for your help once again. Still trying "Stuff", kind of blindly. Also, studying some alternatives (like turn the "printable html" into a pdf and see how it goes).Irrelevance
I am having the same problem and haven't found a solution. It was working before chrome updated to Version 59.0.3071.115Epact
For what it's worth, I can't recreate this issue with the sample HTML. I'm using Google Chrome 60.0.3112.113 (Official Build) (32-bit).Elemi
And using GWT? because I can't recreate it any other way either...Irrelevance
works for me, but I am using Google Chrome 60.0.3112.113 -- are you sure this isn't just a glitch in Version 59.0.3071.115 ?Prendergast
Your final edit should be posted as an answer since it's a workaround, not as an edit to the question.Minaminabe
Did you check chrome's printing settings for background colours ?Tactful
L
0

Have you tried the style directly inside the table tag?

<table border="1px" bgcolor="green">
<tr>
<td>ANYTHING RANDOM
</td>
</tr>
</table>

D.

Lure answered 29/9, 2017 at 14:9 Comment(0)
A
0
<table border="1" >
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
         <tr>
            <td bgcolor="#FF0000">Peter</td>
            <td>Griffin</td>
         </tr>
         <tr>
            <td>Lois</td>
            <td>Griffin</td>
         </tr>
</table>
Ayeshaayin answered 16/11, 2022 at 7:48 Comment(2)
you can use like this just border="1" on table tagAyeshaayin
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Polychromy
S
0
<table border="1" >
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
         <tr>
            <td bgcolor="green">Peter</td>
            <td>Griffin</td>
         </tr>
         <tr>
            <td>Lois</td>
            <td>Griffin</td>
         </tr>
</table>

/* you can add the table border with size 1 or your wish . In HTML table border tag helps to add this feature to the web application.*/

Schrimsher answered 9/1, 2023 at 10:11 Comment(0)
A
0

Others experiencing the same issue were able to resolve with "border: thin solid #000;", where the border thickness was expressed as "thin" rather than "1px".

Antonietta answered 24/2, 2023 at 16:56 Comment(0)
N
0

Try this:

<!DOCTYPE html>
<html>
   <head>
      <style>
         table, th, td {
  border: 1px solid black;
} 
      </style>
      <title>title</title>
   </head>
   <body>
      <h2>Add a border to a table:</h2>
      <table>
         <tr>
            <th>Firstname</th>
            <th>Lastname</th>
         </tr>
        <tr>
           <td bgcolor=\"#FF0000\">Peter</td>
           <td>Griffin</td>
        </tr>
        <tr>
           <td>Lois</td>
           <td>Griffin</td>
        </tr>
    </table>
   </body>
</html>
Naucratis answered 27/10, 2023 at 3:34 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Polychromy
R
0

Check these items:

  • Ensure All Elements Have Borders and Backgrounds Explicitly Set
  • Add !important to All CSS Properties
  • Use Inline Styles
  • Disable all of your extensions
Repurchase answered 16/7 at 10:43 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Polychromy

© 2022 - 2024 — McMap. All rights reserved.