I have a specific page the container some information with a specific design
this design exist in a separate page print.css
I want to print this page with the same style and this page containe an image also
function printContent() {
var divContents = document.getElementById("terms").innerHTML;
var a = window.open('', '', 'height=1000, width=1000');
a.document.write('<html>');
a.document.write("<link rel=\"stylesheet\" href=\"print.css\" type=\"text/css\" media=\"print\"/>");
a.document.write('<body >');
a.document.write(divContents);
a.document.write('</body></html>');
a.document.close();
a.focus();
setTimeout(function () {
a.print();
}, 1000);
a.print();
}
p {
display: inline-block;
}
.container header .top {
display: flex;
align-items: center;
gap: 30px;
margin-top: 30px;
padding-bottom: 20px;
border-bottom: 2px solid #2c2a6b;
}
.container header .top h1 {
color: white;
background-color: #2c2a6b;
padding: 10px 20px;
width: fit-content;
width: -moz-fit-content;
border-top-left-radius: 20px;
}
.container header .top .print-header {
display: flex;
gap: 20px;
}
.container header .top .print-header h2{
color: #2c2a6b;
align-self: flex-end;
}
.container header .print-subheader {
padding: 20px 0;
color: #2c2a6b;
}
.container main .print-content .top {
display: flex;
align-items: flex-end;
gap: 20px;
padding-bottom: 20px;
border-bottom: 1px solid #2c2a6b;
}
.container main .print-content .top img {
width: 50px;
}
.container main .print-content .top h3 {
color: #2c2a6b;
}
.container main .print-content .content{
margin-top: 20px;
}
.container main .print-content .content .inner-box{
display: flex;
align-items: center;
gap: 15px;
margin-bottom: 10px;
}
.container main .print-content .content .inner-box p{
color: #000;
font-weight: 500;
font-size: 16px;
}
<body>
<div class="container" id="terms">
<header>
<div class="top">
<h1>قطاع السياحة</h1>
<div class="print-header">
<img src="images/title-icon.png" alt="title">
<h2> اصدار رخصة الإيواء السياحي</h2>
</div>
</div>
<div class="print-subheader">
<h2>الحصول على رخصة استثمار (المستثمر الاجنبي)</h2>
</div>
</header>
<main>
<div class="print-content">
<div class="top">
<img src="images/terms.png" alt="terms">
<h3>الاشتراطات العامة</h3>
</div>
<div class="content">
</div>
</div>
</main>
</div>
<input type="button" class="btn-print" onclick="printContent()" value="Print">
</body>
the problem is that the image and the style don't appear after i click the print button
after a search i found that i should put a timeout, and i should include the print.css file with media print but after all that the problem still exist i can't print the page with the image and the css style anyone can help please
document.writeis called after the page has been parsed, it cretes a new document, which wipes out all the code on the page, only what is written withd.wis there.