Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. var JSZip = require('jszip');
  2. var Docxtemplater = require('docxtemplater');
  3.  
  4. var fs = require('fs');
  5. var path = require('path');
  6.  
  7. //Load the docx file as a binary
  8. var content = fs
  9. .readFileSync(path.resolve(__dirname, 'invoice-template.docx'), 'binary');
  10.  
  11. var zip = new JSZip(content);
  12.  
  13. var doc = new Docxtemplater();
  14. doc.loadZip(zip);
  15.  
  16. //set the templateVariables
  17. doc.setData({CustomerName : "Microsoft Corp",
  18. AddressLine1: "One Microsoft Way",
  19. City: "Redmond", State: "WA", Zip: "98052",
  20. Country: "USA",
  21.  
  22. InvoiceDate : "14 March 2019",
  23. InvoiceNumber: "INV123",
  24. Items: [
  25. {
  26. Item_Description: "Bananas",
  27. Item_Price: "5",
  28. Item_Qty: "10",
  29. Item_SubTotal: "50"
  30. },
  31. {
  32. Item_Description: "Mangoes",
  33. Item_Price: "10",
  34. Item_Qty: "4",
  35. Item_SubTotal: "40"
  36. }
  37. ],
  38. TotalEx:"90",
  39. SalesTax:"9",
  40. Shipping:"10",
  41. TotalPrice:"$109",
  42. DueDate : "28 March 2019"
  43. });
  44.  
  45. try {
  46. // render the document ie replace the variables
  47. doc.render()
  48. }
  49. catch (error) {
  50. var e = {
  51. message: error.message,
  52. name: error.name,
  53. stack: error.stack,
  54. properties: error.properties,
  55. }
  56. console.log(JSON.stringify({error: e}));
  57. throw error;
  58. }
  59.  
  60. var buf = doc.getZip()
  61. .generate({type: 'nodebuffer'});
  62.  
  63. // buf is a nodejs buffer, you can either write it to a file or do anything else with it.
  64. fs.writeFileSync(path.resolve(__dirname, 'invoice-instance.docx'), buf);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement