如何打印pdf页面与css属性?


function printDiv() {
var divContents = document.getElementById("box2text").innerHTML;
var a = window.open('', '', 'height=500, width=500');
a.document.write('<html>');
a.document.write('<body > <h1>Div contents are <br>');
a.document.write(divContents);
a.document.write('</body></html>');
a.document.close();
a.print();
}

这是我使用的html代码

<div class="box2" id="box2">
<textarea name="box2text" id="box2text" cols="30" rows="10"></textarea>
</div>
<div class="button">
<button onclick="printDiv()">PRINT</button>
</div>

只打印文本我想要文本的css属性如color

你可以创建一个空白的html页面(print-page.html),你的css链接到它。然后使用下面的代码打开它并插入你的内容,然后打印内容:

function printDiv() {
var divContents = document.getElementById("box2text").innerHTML;
const printWindow = window.open('print-page.html', '_blank', `width=${window.innerWidth},height=${window.innerHeight}`);
printWindow.onload = function(){
printWindow.document.body.innerHTML = divContents;
printWindow.print();
}
}

最新更新