Print HTML page keeping the page CSS

Asked

Viewed 5,957 times

3

Good afternoon guys, I have a report that is generated on a page aspx with bootstrap and CSS. The content of the report is within a div <div id="pdf2htmldiv">...</div> where I call javascript for printing. But the page is presented for printing without the style page. Have some way to print this report as it is presented with the style CSS?

Javascript code:

<script>
        function printDiv(divName) {
            var printContents = document.getElementById(divName).innerHTML;
            var originalContents = document.body.innerHTML;

            document.body.innerHTML = printContents;

            window.print();

            document.body.innerHTML = originalContents;
        }
    </script>

Button call:

<input type="button" onclick="printDiv('pdf2htmldiv')" value="Imprimir" />

Content of the page:

<div id="pdf2htmldiv">
\\conteúdo do relatório
</div>

Report: inserir a descrição da imagem aqui

Report in print: inserir a descrição da imagem aqui

The Stylo of the table zebrada not even the report borders. I can print according to the first image?

  • You want to do this without going into browser options to print background graphics?

  • Look, it may be a case of print options as the @dvd comment indicates, or it may be that the css you are using cannot be printed. Post css code and it will be easier

  • @dvd even with the options imprimir gráficos de segundo plano Zebra table Stylo does not appear.

  • @Diegosanths css is the bootstrap standard for tables class="table table-striped"

  • So this is probably the case that @dvd commented on. Have you tried this?

  • @Diegosantos Yes, I’ve scored imprimir gráficos de segundo plano in print options.

  • 1

    Dude, so in this case I believe that within @media print, you should put the colors like bootstrap does. You can’t reuse, because they must be using something that is not compatible. You think you can?

  • @Diegosantos I will do new tests with @media print and set the table css class="table table-striped"

  • 1

    I think maybe I don’t roll like this. Maybe I have to do it by hand using :Nth-Child(2n+1) or something like that. Good Tips?

  • @Diegosantos No manjo! You could post some practical example?

  • 1

    I put an example. Qqr thing speaks

Show 6 more comments

2 answers

2

I will give an answer that can help you without you having to depend on the user choose to print the "Background Graphics"

inserir a descrição da imagem aqui

As already discussed in these two questions the printer by Default I didn’t print anything background in CSS, neither images, colors, nor anything.

Print page with Background

Apply watermark without affecting text

But there are techniques that can solve this problem. As I will show below.

The first step is to create your unique CSS that will only use @print

See the example below working, the color will turn red in the print. The technique is to apply a box-shadow into the cell, so you can put the color you want and it will appear in the printing smoothly.

You can test right here by giving the Ctrl+P that will work!

html, body {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
}

table thead tr th {
    background: greenyellow; /* cor antes da impressão */
}

@media print{
  table thead tr th{
    box-shadow: 0 0 0 1000px red inset;  /* cor para impressão */
  }
}
<div class="container">
    <table border="1">
        <thead>
            <tr>
                <th>Item 1</th>
                <th>Item 2</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Texto 1</td>
                <td>Texto2</td>
            </tr>
        </tbody>
    </table>
</div>

Print result with the above code, note the red box!

inserir a descrição da imagem aqui

If you want to simulate how your print will go straight on the page without having to press Ctrl+P all the time just enable the "Print Preview" direct by Chrome Dev Tools so press F12:

inserir a descrição da imagem aqui

  • I’m gonna run some new tests.

  • Evandro uses @print to overwrite the styles you want to only appear in the print. Then in dodos the places that have background-color vc puts the box-shadow with inset as in the example of my answer you will not have problems with color of bg in printing.

1

Take the example you asked for. Note: If it doesn’t work here in the stack, take the code and put it in a local html. It will work...

@media print{
  table tbody tr:nth-child(2n+1){
    background: #CCC;
  }
}
<table>
  <thead>
    <tr>
      <th>Id</th>
      <th>Nome</th>
      <th>E-mail</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1</td>
      <td>Fulano 1</td>
      <td>[email protected]</td>
    </tr>
    <tr>
      <td>2</td>
      <td>Fulano 2</td>
      <td>[email protected]</td>
    </tr>
    <tr>
      <td>3</td>
      <td>Fulano 3</td>
      <td>[email protected]</td>
    </tr>
    <tr>
      <td>4</td>
      <td>Fulano 4</td>
      <td>[email protected]</td>
    </tr>
    <tr>
      <td>5</td>
      <td>Fulano 5</td>
      <td>[email protected]</td>
    </tr>
  </tbody>
</table>

  • Diego by default printers do not print "Background Graphics" including images and colors. In fact nothing that is background in CSS goes into printing. Most likely your code will not work unless you (manually) have chosen this option at the time of printing, which the normal user would not do! Test your code with the unchecked option and you will see this!

  • So that’s what the comments on his question included. He tried to mark the option to print graphics and it wasn’t going anyway. I tested my code and checked it works. Now it’s obvious, with the chart disabled it won’t work. But this has already been discussed above @hugocsl

  • 1

    @Diegosantos I’ll do new tests.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.