I need help with printing a specific page that contains some information designed in print.css
. I want to print this page, including an image, with the same style.
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>Tourism Sector</h1>
<div class="print-header">
<img src="images/title-icon.png" alt="title">
<h2>Issuance of Tourist Accommodation License</h2>
</div>
</div>
<div class="print-subheader">
<h2>Obtaining an Investment License (Foreign Investor)</h2>
</div>
</header>
<main>
<div class="print-content">
<div class="top">
<img src="images/terms.png" alt="terms">
<h3>General Terms</h3>
</div>
<div class="content">
</div>
</div>
</main>
</div>
<input type="button" class="btn-print" onclick="printContent()" value="Print">
</body>
The issue I'm facing is that the image and style do not appear correctly after clicking the print button. Despite trying different solutions like putting a timeout and including the print.css file with media print, the problem persists. Any assistance would be greatly appreciated.