Greetings! I have successfully converted HTML to PDF by utilizing the print option and implementing a custom size for the document:
Firstly, make sure to check the version of jQuery as the latest version may not support printing. In my case, I used
<script src="/js/jquery.min.js"></script>
To achieve a PDF with a specific canvas size, include the size property in the print media option and set the margin to zero so that the header and page count are hidden:
@media print { @page { size:5.4cm 9.1cm; margin:0;
page-break-after: auto; }
- The printable div should have an id of target_warpper1;
- Additionally, use JavaScript to hide other elements using CSS
HTML:
<div id="target_warpper1_print">
<div class="visiting-card-right">
<div class="visiting-card-up active">Employee Id</div>
<div class="visiting-card-down active">
<div class="visiting-card-logo"><img src="../images/visiting-card/logo.svg" alt="Vishal Korade Logo"></div>
<div class="visiting-card-detail">
<div class="visiting-card-name">Vishal Korade</div>
<div class="visiting-card-designation">Front End Developer</div>
<div class="visiting-card-txt visiting-card-email1"><span class="card-img"><img src="../images/visiting-card/mail.svg" alt="Vishal Korade Logo"></span><span class="crad-text visiting-card-email"><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ed878c83858c9b84c39b849e858c81ad8a808c8481c38e8280">[email protected]</a></span></div>
<div class="visiting-card-txt visiting-card-number1"><span class="card-img"><img src="../images/visiting-card/number.svg" alt="Vishal Korade Logo"></span><span class="crad-text visiting-card-number">9769367630</span></div>
<div class="visiting-card-txt visiting-card-skype1"><span class="card-img"><img src="../images/visiting-card/skype.svg" alt="Vishal Korade Logo"></span><span class="crad-text visiting-card-skype">test</span></div>
<div class="visiting-card-txt visiting-card-address1"><span class="card-img"><img src="../images/visiting-card/address.svg" alt="Vishal Korade Logo"></span><span class="crad-text visiting-card-address">7 Straits View Marina One East Tower, test, 66337</span></div>
</div>
</div>
</div>
<div class="visiting-card-left">
<div class="visiting-card-up"><img src="../images/logohome.png" alt="Vishal Korade Logo"></div>
<div class="visiting-card-down"><img class="page-cover" src="../images/visiting-card/page-cover.svg" alt="Vishal Korade ID Page Cover"></div>
</div>
</div>
<div class="download-pdf" id="create_pdf" onclick="PrintElem()">Download as PDF</div>
JS:
function PrintElem(target_warpper1)
{
var mywindow = window.open('', 'PRINT', 'height=680,width=1200,toolbars=no,scrollbars=no,status=no,resizable=no');
// mywindow.document.writeln('<html><head><title>' + document.title + '</title>');
mywindow.document.writeln('</head><body >');
mywindow.document.writeln('<link rel="stylesheet" href="/style/style.css">');
mywindow.document.writeln('<style>');
mywindow.document.writeln(' * { -webkit-print-color-adjust: exact !important; color-adjust: exact !important; }');
mywindow.document.writeln(' body {background-color: #1f2959;}');
mywindow.document.writeln('.card-left {background-color: #1f2959;float: left;}');
mywindow.document.writeln('#target_warpper1 {display: block;width: 204px;padding-bottom: 0px;float: none;margin: 0 auto;}');
mywindow.document.writeln('.visiting-card-right {width: 204px; float: none; margin: 0px auto; }');
mywindow.document.writeln('.visiting-card-left { width: 204px;float: none; margin: 25px auto 0;}');
mywindow.document.writeln('#target_warpper1_print { display: block;width: 204px;padding-bottom: 0px;margin-right:0px;float: none;margin: 0 auto; }');
mywindow.document.writeln(' .visiting-card-up, .visiting-card-down::after, .visiting-card-down::before, .visiting-measure { display:none;}');
mywindow.document.writeln(' @media print { @page { size:5.4cm 9.1cm; margin:0; page-break-after: auto; } html, body { height: 99%;} .print:last-child { page-break-after: auto;} }');
mywindow.document.writeln('</style>');
mywindow.document.writeln(document.getElementById('target_warpper1').innerHTML);
mywindow.document.writeln('</body></html>');
mywindow.document.close(); // necessary for IE >= 10
mywindow.focus(); // necessary for IE >= 10*/
$(mywindow).load(function(){
mywindow.print();
mywindow.close();
});
return true;
}
If you require any further details, feel free to reach out.