17

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 hopping 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. This way it works, and it prints with the correct formatting.

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

camilo
  • 1,451
  • 3
  • 20
  • 31
  • Possible duplicate of [Print Background colours in Chrome](https://stackoverflow.com/questions/2392366/print-background-colours-in-chrome) – Omri Luzon Jun 16 '17 at 10:30
  • Just yesterday saw the same [question too](https://stackoverflow.com/questions/44571567/print-angular-webpage-with-css-styling-applied-to-it) – Omri Luzon Jun 16 '17 at 10:30
  • It is not. But thanks anyway. – camilo Jun 17 '17 at 13:01
  • 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. – Omri Luzon Jun 17 '17 at 15:11
  • Again, it does not solve my problem. Thanks anyway. – camilo Jun 19 '17 at 10:48
  • Try to use it as a media query for print (`@media print`) – Omri Luzon Jun 19 '17 at 16:48
  • Nope, still the same problem, I'm afraid. :-/ I'll try some sort of sacrifice next... – camilo Jun 21 '17 at 11:52
  • Up-voted, but still can't reproduce it. I tried in chrome 59.0.3071.109 (Official Build) (64-bit). [image showcase](https://i.stack.imgur.com/d5pVB.png) – Omri Luzon Jun 21 '17 at 17:24
  • 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). – camilo Jun 28 '17 at 08:47
  • I am having the same problem and haven't found a solution. It was working before chrome updated to Version 59.0.3071.115 – BGecko Jul 18 '17 at 20:00
  • 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). – mxmoss Aug 31 '17 at 18:55
  • And using GWT? because I can't recreate it any other way either... – camilo Sep 03 '17 at 13:09
  • 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 ? – Yvonne Aburrow Sep 18 '17 at 13:34
  • Your final edit should be posted as an answer since it's a workaround, not as an edit to the question. – BSMP Sep 18 '17 at 14:32

4 Answers4

0

Have you tried the style directly inside the table tag?

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

D.

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>
  • you can use like this just border="1" on table tag – mahesh Alakunta Nov 16 '22 at 07:49
  • 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](/help/how-to-answer). – Community Nov 18 '22 at 06:41
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.*/

AMUTHAN
  • 1
  • 1
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".

Jason Bruce
  • 309
  • 1
  • 9