1

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
5
  • refer this stackoverflow.com/a/28596805/6108882 Commented Feb 14, 2019 at 9:00
  • @UdhayTitus...I have already use !important in my style. But didn't work Commented Feb 14, 2019 at 9:06
  • 2
    your code is working very well can you post your html here. or check the fiddle here jsfiddle.net/ou054d27 Commented Feb 14, 2019 at 9:17
  • 1
    Udhay Titus.... I have updated my post with html code Commented Feb 14, 2019 at 9:24
  • I too can confirm that the jsfiddle given by Udhay Titus is working on Latest Firefox Deskop Browser. If just the color part isn't working then please check your print options. Print might have been set to black & white. It's just a hunch that I have. Commented Feb 14, 2019 at 9:29

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

5 Comments

window.open("") does not work in stack snippet (for me), and returns null. Better use window.document.write() and window.print()
@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 working
@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.
@UdhayTitus....Please another help. I want to print where two div with two different color, background-color and padding.....
Yes it works perfectly now....Use overflow:hidden and all the attributes work correctly.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.