Welcome folks today in this blog post we will be building easy payment pdf invoice
generator in node.js and express using javascript. All the full source code of the application is shown below.
Get Started
In order to get started you need to initiate a new node.js
project using the below command as shown below
npm init -y
After that you need to install the below libraries using the below command as shown below
npm i easyinvoice
Now we need to create the index.js
file inside the root directory and copy paste the following code
index.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
//Import the library into your project var easyinvoice = require('easyinvoice'); let fs = require('fs') var data = { //"documentTitle": "RECEIPT", //Defaults to INVOICE "currency": "USD", "taxNotation": "vat", //or gst "marginTop": 25, "marginRight": 25, "marginLeft": 25, "marginBottom": 25, "logo": "https://www.easyinvoice.cloud/img/logo.png", //or base64 //"logoExtension": "png", //only when logo is base64 "sender": { "company": "Sample Corp", "address": "Sample Street 123", "zip": "1234 AB", "city": "Sampletown", "country": "Samplecountry" //"custom1": "custom value 1", //"custom2": "custom value 2", //"custom3": "custom value 3" }, "client": { "company": "Client Corp", "address": "Clientstreet 456", "zip": "4567 CD", "city": "Clientcity", "country": "Clientcountry" //"custom1": "custom value 1", //"custom2": "custom value 2", //"custom3": "custom value 3" }, "invoiceNumber": "2020.0001", "invoiceDate": "05-01-2020", "products": [ { "quantity": "2", "description": "Test1", "tax": 6, "price": 33.87 }, { "quantity": "4", "description": "Test2", "tax": 21, "price": 10.45 } ], "bottomNotice": "Kindly pay your invoice within 15 days." }; |
As you can see we have declared the data invoice
object where we have declared the no of items
to be present inside the payment invoice and also the prices
of the products alongside with taxes
.
Now we need to export the invoice
to pdf document using the base64 code as shown below.
1 2 3 4 5 6 |
//Create your invoice! Easy! easyinvoice.createInvoice(data, async function (result) { //The response will contain a base64 encoded PDF file console.log(result.pdf); await fs.writeFileSync("invoice.pdf", result.pdf, 'base64'); }); |