简体   繁体   中英

How to get a page printed with its css intact in ruby on rails?

在此处输入图片说明 I am using ruby-on-rails-3.2 app, trying to get print-out of a page but when it gets printed its doesnt have it css. Here is my code

   <%= link_to("Print this Page", "#", :onclick =>"PrintElem('.PrintElem','News Details')") %>


    function PrintElem(news_heading, reporters_details, full_news_text, title)
    {
    heading = jQuery('head').html();
    body_data = jQuery('body').html();
    Popup(heading, body_data);
    return true;
    }

    function Popup(news_heading, body_data)
    {//start
    // var mywindow = window.open('','printwindow');
    mywindow = window.open('#','printing','height=900,width=900,scrollbars=1');
    mywindow.document.write('<html><head>'+heading+'</head><body onload="window.print();">');
    mywindow.document.write(body_data);
    mywindow.document.write('</body></html>')
    mywindow.onload = function() { mywindow.print(); }
    mywindow.document.close();
    return true;
    //end
    }

Here problem is that page is printed before loading javascripts and css. This page should be print after all javascript and css is loaded. Thanks in Advance.

Try creating a document with the css and the javacstcipt basically the

<html>
<head>
//Your css
//Your scripts
</head>
<body>
</dody>
</html>

Then open

window.open('/somefile.html', 'Yout Title maybe from the document', 'width=900');

Then use jQuery to populate the dummy print page(body area);

$(body).prepend(body_data);

Maybe you are not loading the css and javascript correctly while writing all the html after the page is loaded with javascript.

Keep in mind you need some css rules for pages to print correctly (colors, styles)

-webkit-print-color-adjust:exact; // For chrome browser

Tell me if it works for you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM