you can use javascript
and jquery
to print it
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>html2pdf</title>
</head>
<body>
<div id="page">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Numquam id, ex
facere perferendis eligendi corporis provident ipsam, ea porro debitis
natus aut nulla, ipsa atque aliquam architecto est dolorem impedit!
</div>
<button id="btn">print content</button>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"
referrerpolicy="no-referrer"
></script>
<script>
$('#btn').click(function () {
var divContents = $('#page').html()
var printWindow = window.open('', '_self', 'height=400,width=800')
printWindow.document.write('<html><head><title>html2pdf</title>')
printWindow.document.write('</head><body>')
printWindow.document.write(divContents)
printWindow.document.write('</body></html>')
printWindow.document.close()
printWindow.print()
})
</script>
</body>
</html>
you can also add style css to the page
by add this line between head
tag, if html
file and css
file in same folder
printWindow.document.write('<link rel="stylesheet" href="style.css" />')