Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var JSZip = require('jszip');
- var Docxtemplater = require('docxtemplater');
- var fs = require('fs');
- var path = require('path');
- //Load the docx file as a binary
- var content = fs
- .readFileSync(path.resolve(__dirname, 'invoice-template.docx'), 'binary');
- var zip = new JSZip(content);
- var doc = new Docxtemplater();
- doc.loadZip(zip);
- //set the templateVariables
- doc.setData({CustomerName : "Microsoft Corp",
- AddressLine1: "One Microsoft Way",
- City: "Redmond", State: "WA", Zip: "98052",
- Country: "USA",
- InvoiceDate : "14 March 2019",
- InvoiceNumber: "INV123",
- Items: [
- {
- Item_Description: "Bananas",
- Item_Price: "5",
- Item_Qty: "10",
- Item_SubTotal: "50"
- },
- {
- Item_Description: "Mangoes",
- Item_Price: "10",
- Item_Qty: "4",
- Item_SubTotal: "40"
- }
- ],
- TotalEx:"90",
- SalesTax:"9",
- Shipping:"10",
- TotalPrice:"$109",
- DueDate : "28 March 2019"
- });
- try {
- // render the document ie replace the variables
- doc.render()
- }
- catch (error) {
- var e = {
- message: error.message,
- name: error.name,
- stack: error.stack,
- properties: error.properties,
- }
- console.log(JSON.stringify({error: e}));
- throw error;
- }
- var buf = doc.getZip()
- .generate({type: 'nodebuffer'});
- // buf is a nodejs buffer, you can either write it to a file or do anything else with it.
- fs.writeFileSync(path.resolve(__dirname, 'invoice-instance.docx'), buf);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement