1

I have been working for hours to figure out why my CSS is not being applied for printing an HTML page. I have created a working example of the markup I am rendering with dompdf:

https://jsfiddle.net/n7Lak0gr/1/

The HTML and CSS is a directory with multiple columns in a table. The rows have an alternating background color, and there are some other styles as well. But upon printing to a PDF, the styles do not show, even if I have media set to all.

Note that if you copy all of the code from this version and create your own local html file, you can see it better, since another issue is that the table overflows into multiple pages when the styles are not applied.

I have reproduced the issue in Chrome, Firefox, and Internet Explorer. And I have tried removing pieces of the CSS, but I cannot figure out why the styles don't show.

mbomb007
  • 3,788
  • 3
  • 39
  • 68
  • You need to link the CSS file to the HTML file... You can do this by something like `` – NerdOfCode Oct 05 '18 at 16:45
  • @NerdOfCode That is not the issue. JSFiddles do that automatically. See the second version that I linked to. The CSS is embedded in the HTML, and it has the same problem. – mbomb007 Oct 05 '18 at 16:46
  • 2
    Possible duplicate. https://stackoverflow.com/questions/14987496/background-color-not-showing-in-print-preview – NerdOfCode Oct 05 '18 at 16:47

2 Answers2

0

It is the default setting of most browsers not to print backgrounds. Obviously this is to save ink/toner.

You can change that setting in the browser preferences, but the bad thing is that you can't influence those via your website - it's purely a decision of the user who does the printing (and most users don't even know about this preference setting)

Johannes
  • 64,305
  • 18
  • 73
  • 130
  • This is a minimal working example for the sake of asking. I'm actually using [dompdf](https://github.com/dompdf/dompdf) to print markup to a PDF file. – mbomb007 Oct 05 '18 at 16:49
  • oh - I am using that too on a website of mine, and I've found that a lot of CSS handling with it doesn't work as expected - there's a lot of trial and error required... But concerning the printing of backgrounds I am pretty sure that this is deactivated by dompdf. – Johannes Oct 05 '18 at 16:52
  • It was working in 0.6.1, but I'm having issues after updating. Even the font-size isn't working. – mbomb007 Oct 05 '18 at 16:53
  • what I do in dompdf is to move all CSS into a 'style' tag in the head of the php file which is rendered by dompdf. So I have no external CSS, only internal - that solved * some* problems for me (not all of them, though). – Johannes Oct 05 '18 at 17:16
  • BTW: I think you should add the fact that you are using dompdf to your question, and also use the dompdf tag to "attract" people who have experience using it. – Johannes Oct 05 '18 at 17:17
0

I figured it out. I had to delete the font cache file dompdf_font_family_cache.php located in sites/all/libraries/dompdf/lib/fonts/.

I was pointed in the right direction after seeing some other errors and viewing this SO answer

mbomb007
  • 3,788
  • 3
  • 39
  • 68