0

I want to link css file on printing specific div but it dose not get stylesheet on printing area. How can i fix this. Here is my source code:

function print_area(){

    var win = window.open('', '', 'left=0,top=0,width=800,height=900,toolbar=0,scrollbars=0,status=0');
    win.document.write('<html><head><title>Print it!</title><link rel="stylesheet" type="text/css" href="css/thinking_aloud_test - Copy.css" media="print"></head><body>');
    win.document.write($("#article_main_wrapper").html());
    win.document.write('</body></html>');
    win.print();
    win.close();


  }
7
  • 2
    You probably think that writing the HTML like you do in notepad will work, but it won't (The browser will not wait, it will auto-close the missing closing tags). You need to write that whole HTML in a single win.document.write Commented May 12, 2017 at 19:13
  • Possible duplicate of How do I write content to another browser window using Javascript? Commented May 12, 2017 at 19:18
  • You should also look at stackoverflow.com/q/802854/215552 Commented May 12, 2017 at 19:18
  • @MikeMcCaughan That duplicate don't answer this specific issue, it's just demonstre it, but doesn't mention anything about multiple document.write as in this question Commented May 12, 2017 at 19:21
  • It does more than demonstrate the problem, it solves it: stackoverflow.com/a/169843/215552. Commented May 12, 2017 at 19:23

3 Answers 3

0
var win = window.open('', '', 'left=0,top=0,width=800,height=900,toolbar=0,scrollbars=0,status=0');

var html = '<html><head><title>Print it!</title><link rel="stylesheet" type="text/css" href="css/thinking_aloud_test - Copy.css" media="print"></head><body>';
html += $("#article_main_wrapper").html();
html += '</body></html>';

win.print();
win.close();

win.document.write(html); // You need to append the `html` in a single element
// Write it at once to a single win.document.write
Sign up to request clarification or add additional context in comments.

1 Comment

Still it can't take stylesheet.
0

Here is an example of injecting css via javascript.

(function(d, s, id) {
  var css, icss = d.getElementsByTagName(s)[0] || d.getElementsByTagName('head')[0];
  if (d.getElementById(id)) {
    return;
  }
  css = d.createElement(s);
  css.id = id;
  css.href = "http://getbootstrap.com/dist/css/bootstrap.min.css";
  css.rel = "stylesheet";
}(document, 'link', 'bootstrap'));
<html>
    <head>
    </head>
    <body>
        <button class="btn btn-success">normal button to bootstrap button by adding css</button>
     </body>
</html>

if it's for printing with a new tab where you want to append content with css and then print, just go with there steps

var content = document.createTextNode("Some HTML content you want to print");    
var css = document.createElement('link');
css.rel  = 'stylesheet';
css.type = 'text/css';
css.href = 'css url';
css.media = 'all';
printwindow.focus(); // if its a new tab where you want to do print operation, assuming printwindow is your new tab/popup opened by window.open()
printwindow.document.head.appendChild(css);
printwindow.document.body.appendChild(content);
printwindow.print();
printwindow.document.close();

Comments

0

Another possibility is to read the content of the stylesheet and write it directly into the HTML. Here an example using jQuery:

function print_area(){
    var win = window.open('', '', 'left=0,top=0,width=800,height=900,toolbar=0,scrollbars=0,status=0');
    $.get('main.css', function (data) {
        console.log(data);

        win.document.write('<html><head><title>my div</title><style>');
        win.document.write(data);
        win.document.write('</style></head><body >');
        win.document.write('<div id="article_main_wrapper">My DIV</div>');
        win.document.write('</body></html>');
        win.print();
        win.close();
    });
}

Comments

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.