Print directly without going through the php dialog box

0

I use java script to print only the contents of a div, but I do not need to use the browser print dialog box.  

Reason: I created a formatting for the text, when it goes to print by the dialog box is losing my formatting.  

How could I get this right?  

Here are my codes:

  

DIV:

<style type="text/css">
  body {
    width:100px;

  }
  #div1{
    border:solid 1px;
    box-shadow: 10px 10px 10px 5px black;
    font-size: 8px;
    font-weight: bold;
    font-family: Arial;
  }
  </style>
  

JS:

<script>
function cont(){
   var conteudo = document.getElementById('print').innerHTML;
   tela_impressao = window.open('about:blank');
   tela_impressao.document.write(conteudo);
   tela_impressao.window.print();
   tela_impressao.window.close();
}
</script>
  

CONTENT:

<div class="container">
  <div class="row">
<div id="div1" style="width:400px; margin:0 auto;"> 
<?php
echo"<div id='print' class='conteudo'>";
      echo"<center><img src='logo.png' width='200';margin:0 auto;></center>";
      echo"<center><b>COMPROVANTE DE TROCA DE VASILHAME</center></b>";
      echo"<hr>";
      echo "<p>COMPROVANTE N°: $v_cupom   </p>";
      echo "<p>LOJA: $v_loja_num - $v_loja_desc </p>";
      echo "<p>VASILHAME: $v_cod_ean - $v_vasilhame </p>";
      echo "QUANTIDADE: $v_quantidade";
      echo"<hr>";
      echo"<center>DATA: $v_data HORA: $v_hora  </center>";
 echo "</div>";
?>
</div>
</div>
<br>
<center><a href="index.php" class="btn btn-danger" onclick="cont();" value="Imprimir">IMPRIMIR</a></center>
<br>
</div>
</div>
    
asked by anonymous 02.05.2016 / 22:59

2 answers

4

Good evening, Otácio.

First, you can not print directly without the dialog box, just imagine how many sites would print things on your printer without your permission! It would be a mess!

Secondly, the problem with printing is because it is opening a new window with the content, but the style sheet is not going together. Make this change in your javascript code and try again:

<script>
function cont(){
    window.print();
}
</script>

Hug.

    
03.05.2016 / 00:23
0

Solution:  So with indicated by our colleague @LF Ziron insert the css along with the print function.  Here is the code:

<script>
    function cont(){
       var conteudo = conteudo = document.getElementById("div1").outerHTML; 
                                 document.getElementById('print').innerHTML;
       tela_impressao = window.open('about:blank');
       tela_impressao.document.write(conteudo);
       tela_impressao.window.print();
       tela_impressao.window.close();

    }
</script>
    
03.05.2016 / 04:18