JavaScript print preview

后端 未结 10 1447
梦毁少年i
梦毁少年i 2020-12-28 19:46

How can I see print preview of my invoice generated from website. If I print with the script

print this page         


        
10条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-28 20:24

    This is what I used:

    printPage(evt: any) {
        // add this 'noprint' class to elements you want to hide from printing
        let hideElements = document.getElementsByClassName('noprint');  
        let arr = Array.prototype.slice.call(hideElements)
        arr.map(val => {
          val.style.visibility = 'hidden';
        })
        let w = window.open();
        // 'main' is the section I want to print
        w.document.write(document.getElementById('main').innerHTML);  
        w.print();
        w.close();
        arr.map(val => {
          val.style.visibility = 'visible';
        })
    }
    

提交回复
热议问题