printing div content with css applied

后端 未结 8 821
北海茫月
北海茫月 2020-12-28 17:13

I am working on a project and I want to print div content.The code which I am using is fulfilling my requirements,but I am getting simple output without Css applied to it an

8条回答
  •  生来不讨喜
    2020-12-28 17:21

    Pass the div to be printed, as an input to print function,

    
    

    Then clone the element,

    function printDiv(elem)
    {
       renderMe($('
    ').append($(elem).clone()).html()); }

    Pass the cloned element to render, include the stylesheet in it.

    function renderMe(data) {
        var mywindow = window.open('', 'invoice-box', 'height=1000,width=1000');
        mywindow.document.write('invoice-box');
        mywindow.document.write('');
        mywindow.document.write('');
        mywindow.document.write(data);
        mywindow.document.write('');
    
    
        setTimeout(function () {
        mywindow.print();
        mywindow.close();
        }, 1000)
        return true;
    }
    

    If innerHTML is passed, it will ignore the styles.

提交回复
热议问题