The following HTML and JavaScript is used to create a receipt PDF. The PDF will print in receipt printer, the paper size in 80 mm width. And the printing height will calculate by the length of three parts(header, footer and buying items).
I tried with this code but it did not create PDF to print.
JavaScript
$(document).ready(function() { $(".btn").click(function() { var contentHeight; // header + footer + number of buying items var doc = new jsPDF("p", "mm", [80, contentHeight]), source = $("#template_invoice"), margins = { top: 10, //bottom: 60, left: 5, width: 80 // Receipt printer width }; doc.fromHTML( source, // HTML string or DOM elem ref. margins.left, // x coord margins.top, { // y coord width: margins.width // max width of content on PDF }, function(dispose) { // dispose: object with X, Y of the last line add to the PDF // this allow the insertion of new lines after html doc.save("Test.pdf"); }, margins ); }); }); HTML
<!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <title>Receipt PDF</title> <link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css'> <link rel="stylesheet" href="css/style.css"> <style> table{ font-family: 'Courier New', Courier, monospace; } td#aliCenter{ text-align:center; } td#aliRight{ text-align:right; } </style> </head> <body> <div class="container" id="template_invoice"> <table> <!-- header --> <tr> <td colspan="5" id="aliCenter">ABC Restaurant<br>Sunny Road, Island<br>Contact : 123456789</td> </tr> <tr> <td colspan="5" id="aliCenter">15/11/2018 02:14:52 IamCoder No: 150</td> </tr> <tr> <td colspan="5"> <hr> </td> </tr> <tr> <td id="aliCenter">NO</td> <td id="aliCenter">ITEM</td> <td id="aliCenter">QTY</td> <td id="aliCenter">PRICE</td> <td id="aliCenter">AMOUNT</td> </tr> <tr> <td colspan="5"> <hr> </td> </tr> <!-- buying items --> <tr> <td>1</td> <td colspan="4">The big tasty Pizza</td> </tr> <tr> <td></td> <td>PZ4545</td> <td>2.00</td> <td id="aliRight">150.00</td> <td id="aliRight">300.00</td> </tr> <tr> <td>2</td> <td colspan="4">Crunchy huge Buggers</td> </tr> <tr> <td></td> <td>BR7878</td> <td>5.00</td> <td id="aliRight">500.00</td> <td id="aliRight">2500.00</td> </tr> <tr> <td>3</td> <td colspan="4">1.5 l Coca-Cola pack</td> </tr> <tr> <td></td> <td>CC6523</td> <td>3.00</td> <td id="aliRight">180.00</td> <td id="aliRight">540.00</td> </tr> <!-- footer --> <tr> <td colspan="5"> <hr> </td> </tr> <tr> <td></td> <td colspan="3">Net Total</td> <td id="aliRight">3340.00</td> </tr> <tr></tr> <tr> <td></td> <td colspan="3">Cash</td> <td id="aliRight">3500.00</td> </tr> <tr> <td></td> <td colspan="3">Balance</td> <td id="aliRight">160.00</td> </tr> <tr></tr> <tr> <td colspan="5" id="aliCenter">-----------IMPORTANT NOTICE-----------</td> </tr> <tr> <td colspan="5" id="aliCenter">In case of a price discrepancy, <br>return the bill and item within <br> 2 days to refund the difference</td> </tr> </table> <div class="row"> <div class="col-xs-4"> <button class="btn btn-info pull-right">Download</button> </div> </div> </div> <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script> <script src='https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.4/jspdf.min.js'></script> <script src="js/index.js"></script> </body> </html>
source = $("#template_invoice")[0],is this correct? But does not work