How can I force weasyprint to use the colors in PDF?

Viewed 2082

When I use

$ weasyprint table.html table.pdf

on https://jsfiddle.net/MartinThoma/0hL29mcc/ it generates a PDF which looks like this:

enter image description here

As you can see, the heading is black, not red. Also, the table is not striped anymore.

Somehow weasyprint removes all colors. How can I prevent this behaviour / keep the colors? (I don't necessarily need to use weasyprint, but I don't know any better alternative)

I use WeasyPrint version 0.40.

What I've tried

  • pandoc table.html -o table.pdf: Does not apply CSS page rotation; does not apply CSS
  • wkhtmltopdf table.html out.pdf: Does not apply CSS page rotation; (but applies some CSS - not the color, though)
  • import pdfkit;pdfkit.from_file('table.html', 'outpdfkit.pdf'): Does not apply CSS page rotation (looks the same as wkhtmltopdf)
2 Answers

The main problem is that Bootstrap 3 contains the following:

@media print{
    *,:after,:before {
        color:#000!important;
        text-shadow:none!important;
        background:0 0!important;
        -webkit-box-shadow:none!important;
        box-shadow:none!important
    }
}

So weasyprint is actually behaving as intended. Removing this leads to the expected results.

I ran into this issue as well and I have a suitable workaround. At least in my case, it appears to be a conflict between WeasyPrint and Bootstrap. I'm using WeasyPrint from within Python. I reordered my list of CSS files to put my custom CSS first - I thought it needed to go last. Then I added the !important modifier to my color and background-color tags.

For example:

@media print {
    bg-blue {
        background-color: #1e77b4 !important;
    }
}

I hope that helps.

Related