1

I need to print a particular div as it is ie whatever css style is there same as that. For styling I am using bootstrape.css and mycss.css file. But after clicking print all css vanished. Here is my try (implemented one):

function printProfile(){            
    var contents = $("#profileContent").html();
    var frame1 = $('<iframe />');

    frame1[0].name = "frame1";
    frame1.css({ "position": "absolute", "top": "-1000000px" }); 
    $("body").append(frame1);

    var frameDoc = frame1[0].contentWindow ? frame1[0].contentWindow : frame1[0].contentDocument.document ? frame1[0].contentDocument.document : frame1[0].contentDocument;

    frameDoc.document.open();
    frameDoc.document.write('<html><head><title>Profile Contents</title>');
    frameDoc.document.write('<link href="css/bootstrape.min.css" rel="stylesheet" type="text/css" />');
    frameDoc.document.write('<link href="css/mycss.css" rel="stylesheet" type="text/css" />');
    frameDoc.document.write('</head><body>');
    frameDoc.document.write(contents);
    frameDoc.document.write('</body></html>');
    frameDoc.document.close();

    setTimeout(function () {
        window.frames["frame1"].focus();
        window.frames["frame1"].print();
        frame1.remove();
    }, 100);
}

where I am going wrong?

3
  • try with media query css Commented Jun 7, 2016 at 11:06
  • i am not very fond of media query. But I tried this: @media print { p {background-color: green;} } for checking purpose. How to use this media query here? Commented Jun 7, 2016 at 11:22
  • Note that some browsers do not respect background colors. For example in Firefox the user hast to tick "Print background colors" to allow this. If not necessary use borders, saves on ink and works in all modern browsers. Commented Jun 7, 2016 at 11:29

1 Answer 1

1

try media="print" while loading css

<link href="css/mycss.css" rel="stylesheet" type="text/css" media="print"/>
Sign up to request clarification or add additional context in comments.

2 Comments

no its not effecting . if i commented .print() method and callinf .open() normally its effecting. But i need it to in print mode.
write your code for print in @media print { }

Your Answer

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.