Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force Background Color Printing on Firefox

I'm using the Barby ruby gem which adds a handy way to render barcodes as HTML.

Barby renders a table with td backgrounds on or off based on the code passed to it. This works pretty well because I don't want to generate and store an image file for every record I need a barcode for.

The major browsers don't print background colors by default and I need the barcode to print without making the user change a print option on their local system.

I'm not sure how to accomplish this with Firefox. With webkit (Chrome and Safari), it's pretty easy:

td { 
  background: #000 !important;
  -webkit-print-color-adjust: exact;
}

Feverish Googling hasn't really gotten me anywhere: This question is a few years old and I haven't found anything newer so I figure I'd pose the question again. Fat borders also won't really work because if the relationship between the bars change, it'll change the data contained in the code.

like image 367
Derek Povah Avatar asked Jan 25 '16 23:01

Derek Povah


People also ask

How do I get rid of the white background in Firefox?

You can check or uncheck "Allow pages to use their own colors . . ." You could try Options>Content>Fonts and Colors>Colors>Background.


2 Answers

This is beginning to work in Firefox (at least version 48.0.2) with the "color-adjust" property.

td { 
  background: #000 !important;
  -webkit-print-color-adjust: exact;
  color-adjust: exact;
}

I see a minor bug or two in my particular project, but the background colors are showing up!

like image 103
RustyToms Avatar answered Sep 30 '22 14:09

RustyToms


This works for me:

@media print {
    body {
        -webkit-print-color-adjust: exact; /*Chrome, Safari */
        color-adjust: exact; /*Firefox*/
    }
}
like image 25
Khanh Vo Avatar answered Sep 30 '22 14:09

Khanh Vo