Stackblitz: https://stackblitz.com/edit/angular-xvdy1q
Я хочу напечатать конкретные div
.
<div id="forPrint" (click)="onPrint('23')">
<div id="printable">
<div class="box">
</div>
</div>
</div>
Javascript код:
onPrint(url) {
this.mode = 'print';
const mywindow = window.open('', 'PRINT', 'height=400,width=600');
mywindow.document.write(`<html><head><title>${document.title}</title>`);
mywindow.document.write('</head><body >');
mywindow.document.write(`<h1> www.${url}</h1>`);
mywindow.document.write(document.getElementById('forPrint').innerHTML);
mywindow.document.write('</body></html>');
mywindow.document.close(); // necessary for IE >= 10
mywindow.focus(); // necessary for IE >= 10*/
mywindow.print();
mywindow.close();
return true;
}
Стайлс:
@media print {
html,
body {
height: 100% !important;
width: 100%!important;
h1 {
color: #1c7430;
}
}
#printable {
display: flex!important;
justify-content: center!important;
align-items: center!important;
height: 100% !important;
width: 700px!important;
}
.box {
width: 100px!important;
height: 100px!important;
background: green!important;
}
}
Но после открытия диалогового окна печати поле не отображается. Поэтому не применяются стили, такие как h1
, .box
. В чем проблема? Заранее спасибо.