Last active
May 23, 2022 16:10
-
-
Save JasonHarrop/399d3a55e405bb40128f153fad73e415 to your computer and use it in GitHub Desktop.
Create PDF proposals, invoices, contracts etc in Javascript from a suitable Word document template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Step 1: generate docx | |
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, '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, 'output.docx'), buf); | |
// Step 2: convert docx to pdf | |
const docx = require("@nativedocuments/docx-wasm"); | |
// init docx engine | |
docx.init({ | |
// ND_DEV_ID: "XXXXXXXXXXXXXXXXXXXXXXXXXX", // goto https://developers.nativedocuments.com/ to get a dev-id/dev-secret | |
// ND_DEV_SECRET: "YYYYYYYYYYYYYYYYYYYYYYYYYY", // you can also set the credentials in the enviroment variables | |
ENVIRONMENT: "NODE", // required | |
LAZY_INIT: true // if set to false the WASM engine will be initialized right now, usefull pre-caching (like e.g. for AWS lambda) | |
}).catch( function(e) { | |
console.error(e); | |
}); | |
async function convertHelper(document, exportFct) { | |
const api = await docx.engine(); | |
await api.load(document); | |
const arrayBuffer = await api[exportFct](); | |
await api.close(); | |
return arrayBuffer; | |
} | |
convertHelper(buf, "exportPDF").then((arrayBuffer) => { | |
fs.writeFileSync("output.pdf", new Uint8Array(arrayBuffer)); | |
}).catch((e) => { | |
console.error(e); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how could i use your example in a browser (without node)?