web-dev-qa-db-ja.com

CSSスタイルとともに新しいjavascriptウィンドウ(.open)を開きます

私は、この機能を、現在取り組んでいるプロジェクトのWebサイトで機能させようとしています。この関数の目的は、セレクター#contentと呼ばれる子divのコンテンツのみを(物理的に)印刷することです。

ここに、私が今まで持っている少しを示します。

<script>
    function printContent() {
        window.open().document.write($("#content").html());
        window.print();
        window.close();
    }
</script>

この機能は、ユーザーが「印刷」ハイパーリンクをクリックすると起動されます。新しいウィンドウは、別のHTMLドキュメントから解析された#content divのコンテンツをロードします。

<div id="content">
    <br/>
    <div id="Algemeen">
        <h3>Algemene informatie</h3>
        <fieldset id="profile">
            <img id="male" src="./images/Pixers/male_icon.jpg"></img>
            <img id="female" src="./images/Pixers/female_icon1.jpg"></img>
        </fieldset>
    </div>
    <div id ="leftbox">   
        <div id ="veldbox"><label>BSN:</label>$!person.bsn</div>
        <div id ="veldbox"><label>Voornaam: </label>$!person.first_name</div>
        <div id ="veldbox"><label>Achternaam:</label>$!person.name_prefix $!person.last_name</div>
        <div id ="veldbox"><label>Geslacht:</label>$!person.gender</div>  
        <div id ="veldbox"><label>Woonadres:</label>$!person.address</div>
        <div id ="veldbox"><label>Plaatsnaam:</label>$!person.location</div>
        <div id ="veldbox"><label>Provincie:</label>$!person.province</div>
        <div id ="veldbox"><label>Postcode:</label>$!person.zipcode</div>
        <div id ="veldbox"><label>Tel. nummer thuis:</label>$!person.h_number</div>
        <div id ="veldbox"><label>Mobiel nummer:</label>$!person.mobile_nr</div>
        <div id ="veldbox"><label>Burgerlijke Stand:</label>$!person.m_status</div>
        <div id ="veldbox"><label>land van herkomst:</label>$!person.Origin</div>
    </div>
    <div id="rightbox">
        <div id ="veldbox"><label>Naam instantie:</label></div>
        <div id ="veldbox"><label>Adres instantie:</label></div>
        <div id ="veldbox"><label>Postcode instantie:</label></div>
        <div id ="veldbox"><label>Tel. instantie:</label></div>
        <div id ="veldbox"><label>Fax instantie:</label></div>
        <div id ="veldbox"><label>E-mail instantie:</label></div>
        <div id ="veldbox"><label>Website instantie:</label></div>
        <div id ="veldbox"><label>-:</label></div>
        <div id ="veldbox"><label>-:</label></div>
        <div id ="veldbox"><label>-:</label></div>  
    </div>
</div>

それだけでスタイルをロードしません。すべてのコンテンツは、左上隅ですべて切り取られます。 CSSをJS経由でリンクするか、別のページで提案されているようにページの先頭に配置するだけでした。もちろんこれは間違っているかもしれません。私はまだJQueryでこれを理解しようとはしていませんので、私の問題を解決する他の解決策があれば、喜んで喜んでアドバイスを受けます。

前もって感謝します!

23
Fariz Fakkel

開いたウィンドウで完全なHTMLページを作成し、そこでCSSファイルを参照します。

var win = window.open('','printwindow');
win.document.write('<html><head><title>Print it!</title><link rel="stylesheet" type="text/css" href="styles.css"></head><body>');
win.document.write($("#content").html());
win.document.write('</body></html>');
win.print();
win.close();
35
Reeno

上記の例と似たようなことをしましたが、すべてのブラウザーで機能するとは限りません。以下はすべての主要なブラウザでテストされ、私のために機能します。 (一部のスタイルは親要素に依存する可能性があるため、divがそれらの要素内にネストされている場合、スタイルは印刷ウィンドウで親ページとまったく同じに見えない場合があります)

function printWithCss() {
        //Works with Chome, Firefox, IE, Safari
        //Get the HTML of div
        var title = document.title;
        var divElements = document.getElementById('printme').innerHTML;
        var printWindow = window.open("", "_blank", "");
        //open the window
        printWindow.document.open();
        //write the html to the new window, link to css file
        printWindow.document.write('<html><head><title>' + title + '</title><link rel="stylesheet" type="text/css" href="/Css/site-print.css"></head><body>');
        printWindow.document.write(divElements);
        printWindow.document.write('</body></html>');
        printWindow.document.close();
        printWindow.focus();
        //The Timeout is ONLY to make Safari work, but it still works with FF, IE & Chrome.
        setTimeout(function() {
            printWindow.print();
            printWindow.close();
        }, 100);
    }
11
cmartin

親ページとprintのすべてのスタイルが必要でした。だから私はすべてのcsslinks[〜#〜] head [〜#〜]

私のソリューションはjQueryを使用します。

(function print() {
    // getting the tag element I want to print
    // cloning the content so it doesn't get messed
    // remove all the possible scripts that could be embed
    var printContents = $('body').clone().find('script').remove().end().html();

    // get all <links> and remove all the <script>'s from the header that could run on the new window
    var allLinks = $('head').clone().find('script').remove().end().html();

    // open a new window
    var popupWin = window.open('', '_blank');
    // ready for writing
    popupWin.document.open();

    // -webkit-print-color-adjust to keep colors for the printing version
    var keepColors = '<style>body {-webkit-print-color-adjust: exact !important; }</style>';

    // writing
    // onload="window.print()" to print straigthaway
    popupWin.document.write('<html><head>' + keepColors + allLinks + '</head><body onload="window.print()">' + printContents + '</body></html>');

    // close for writing
    popupWin.document.close();
})();
8
Pere Pages

また、ページのレンダリング後のCSSのロードにも問題があったため、解決策はcssファイルのコンテンツを読み取り、新しいドキュメントに書き込むことでした。

    var w = window.open();
    jQuery.get('/styles.css', function (data) {
        w.document.write('<html><head><style>');
        w.document.write(data);
        w.document.write('</style></head><body>');
        w.document.write($('.print-content').html());
        w.document.write('</body></html>');
        w.print();
        w.close();
    });
5
Mirko

同じ問題がありました。私が見つけているのは、CSSがページにレンダリングする時間がある前に印刷されているということです。これを見つけるために行ったのは、印刷呼び出しでsetTimeoutを実行し、印刷されたドキュメントにすべてのスタイルが表示されることでした。タイムアウトを行わなかった場合、印刷呼び出しでスタイルが選択されていないことがわかりました。

1
tbone78

いずれかがAngularでこれを実現しようとしている場合に備えて、CSSファイルをassetsフォルダーに入れてください。

0
rgantla