I am trying to apply css for print page.
I have tried with several options like
<style type = "text/css">
@media print
{
body {
color : #eee !important;
font-size : 2em;
}
}
</style>
Here is my html code inside JS
newWin.document.write('<html><body onload="window.print()"><div style="border-bottom : 1px solid #000;margin-bottom :20px;"><div style="" ><p><lable>Name :</lable><span><b>'+name+'</b></span></p><p><lable>Mobile Number :</lable><span><b>'+mobile_number+'</b></span></p><p><lable>Age :</lable><span><b>'+age+'</b></span></p><p><lable>Gender :</lable><span><b>'+gender+'</b></span></p></div><div class="col-md-4"><p><lable>Prescribed By :</lable><span><b>'+doctor+'</b></span></p><p><lable>Prescribed Date :</lable><span><b>'+date+'</b></span></p></div></div><div class="col-md-12"><p><lable>Illness :</lable><span><b>'+illness+'</b></span></p><p><lable>Allergy :</lable><span><b>'+allergy+'</b></span></p><p><lable>Social History :</lable><span><b>'+social_history+'</b></span></p><p><lable>Old Medicine :</lable><span><b>'+old_medicine+'</b></span></p><p><lable>Old Diagnosis :</lable><span><b>'+old_diagnosis+'</b></span></p><p><lable>New Medicine :</lable><span><b>'+new_medicine+'</b></span></p><p><lable>New Diagnosis :</lable><span><b>'+new_diagnosis+'</b></span></p></div></body></html>');
But didn't worked. Anybody help please ?
asked Feb 14, 2019 at 8:57
Arafat Rahman
1,0175 gold badges22 silver badges50 bronze badges
1 Answer 1
add your css in your js surely it will works. check here https://jsfiddle.net/Udhaytitus/ou054d27/3/
function printData()
{
/*newWin= window.open("");
newWin.document.write('<html><style>@media print{body {color : #eee !important;font-size : 12em; padding:10px;}}</style><body onload="window.print()"><div>This is test print page.</div></body></html>');
newWin.print();
newWin.close();*/
window.document.write('<html><style>@media print{body {color : #eee !important;font-size : 12em;border:1px solid #000; padding:77px;}}</style><body onload="window.print()"><div>This is test print page.</div></body></html>');
window.print();
window.close();
}
$('#btnPrint').on('click',function(){
printData();
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="button" value="print" id="btnPrint"/>
answered Feb 14, 2019 at 9:35
Udhay Titus
5,8674 gold badges26 silver badges42 bronze badges
Sign up to request clarification or add additional context in comments.
5 Comments
Peter B
window.open("") does not work in stack snippet (for me), and returns null. Better use window.document.write() and window.print()Arafat Rahman
@UdhayTitus...Ur's one is correct. But one ques.
float, border, padding...aren't these attribute working for print css ? In my code it isn't workingArafat Rahman
@UdhayTitus... it works....I accept the answer...But my problem is not solved yet... Cz all css attributes are not work may b. Cz I applied
border-bottom attribute...but not works properly...some other attributes also didn't work.Arafat Rahman
@UdhayTitus....Please another help. I want to print where two
div with two different color, background-color and padding.....Arafat Rahman
Yes it works perfectly now....Use
overflow:hidden and all the attributes work correctly.lang-html
!importantin my style. But didn't work