Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
173 views
in Technique[技术] by (71.8m points)

HTML browser print no respect css style

I have a table where I fixed the columns and the font to the text. The table is optimized by bootstrap.

I try to print the table through the print menu of the browser and it does not respect the style from css the table that is generated on the sheet

Code for js:

function myFunction() {
            
            
            var css = '@page { size: landscape;} }',
                head = document.head || document.getElementsByTagName('head')[0],
                style = document.createElement('style');

            style.type = 'text/css';
            style.media = 'print';

            if (style.styleSheet){
              style.styleSheet.cssText = css;
            } else {
              style.appendChild(document.createTextNode(css));
            }

            head.appendChild(style);

            window.print();
        }
        
        
    myFunction();

Code css:

 @media print;
    .table th:nth-child(1),
    .table td:nth-child(1) {
      width: 5px;
    }
    
    .table th:nth-child(2),
    .table td:nth-child(2) {
      width: 5px;
    }
    .table th:nth-child(3),
    .table td:nth-child(3) {
      width: 5px;
    }
    
    .table th:nth-child(4),
    .table td:nth-child(4) {
      width: 5px;
    }
    
    .table th:nth-child(5),
    .table td:nth-child(5) {
      width: 5px;
    }
    .table th:nth-child(6),
    .table td:nth-child(6) {
      width: 5px;
    }
    .table th:nth-child(7),
    .table td:nth-child(7) {
      width: 5px;
    }
    .table th:nth-child(8),
    .table td:nth-child(8) {
      width: 5px;
    }
    .table th:nth-child(9),
    .table td:nth-child(9) {
      width: 5px;
    }
    .table th:nth-child(10),
    .table td:nth-child(10) {
      width: 5px;
    }
    .table th:nth-child(11),
    .table td:nth-child(11) {
      width: 5px;
    }
    
    
    .table th:nth-child(12),
    .table td:nth-child(12) {
      font-size: 20px;
      width: 120px;
    }
    
    .table th:nth-child(13),
    .table td:nth-child(13) {
      font-size: 10px;
      width: 5px;
    }
   
    .table th:nth-child(14),
    .table td:nth-child(14) {
      font-size: 10px;
      width: 5px;
    }
    
    .table th:nth-child(15),
    .table td:nth-child(15) {
      font-size: 10px;
      width: 5px;
    }
    .table th:nth-child(16),
    .table td:nth-child(16) {
      font-size: 10px;
      width: 5px;
    }

I would like to respect the css style as in the html page and to generate the same thing on the sheet, thank you!

question from:https://stackoverflow.com/questions/65856967/html-browser-print-no-respect-css-style

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The browser (by default) never prints the background color. You would need to enable that option in your browser.

If you are using a print-specific stylesheet, make sure it includes media="screen, print":

<link rel="stylesheet" type="text/css" href="print.css" media="screen, print" />

Also, remember to enable the printing settings in your browser:

-webkit-print-color-adjust: exact;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...