0
I use java script to print only the contents of a div, but I need you not to use the browser print dialog box.
Reason: I created a formatting for the text, when it goes to print by the dialog box this losing my formatting.
How could I hit this ?
Follow below 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>
As you open a new window, the style is not assigned to it. Try to place the style inside the content.
– LF Ziron
@LF Ziron More the style is already inside the content.
– Chefe Druida
No, the contents variable has only the innerHTML of div #print.
– LF Ziron
I got it, but there’s a way to pass the head in Java script ?
– Chefe Druida
Yes, apply an id to your style tag, say you use myStyle and use it in the script: content = Document.getElementById("myStyle"). outerHTML; content += Document.getElementById('print'). innerHTML;
– LF Ziron
Perfect, it worked, Thank you.
– Chefe Druida