parent
							
								
									3dc23fd746
								
							
						
					
					
						commit
						97d8d34e12
					
				
				 5 changed files with 93 additions and 27 deletions
			
			
		
									
										Binary file not shown.
									
								
							
						@ -0,0 +1,67 @@ | 
				
			|||||||
 | 
					const PizZip = require('pizzip'); | 
				
			||||||
 | 
					const Docxtemplater = require('docxtemplater'); | 
				
			||||||
 | 
					const fs = require('fs'); | 
				
			||||||
 | 
					const path = require('path'); | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// The error object contains additional information when logged with JSON.stringify (it contains a properties object containing all suberrors).
 | 
				
			||||||
 | 
					function replaceErrors(key, value) { | 
				
			||||||
 | 
					    if (value instanceof Error) { | 
				
			||||||
 | 
					        return Object.getOwnPropertyNames(value).reduce(function (error, key) { | 
				
			||||||
 | 
					            error[key] = value[key]; | 
				
			||||||
 | 
					            return error; | 
				
			||||||
 | 
					        }, {}); | 
				
			||||||
 | 
					    } | 
				
			||||||
 | 
					    return value; | 
				
			||||||
 | 
					} | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function errorHandler(error) { | 
				
			||||||
 | 
					    console.log(JSON.stringify({ | 
				
			||||||
 | 
					        error: error | 
				
			||||||
 | 
					    }, replaceErrors)); | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (error.properties && error.properties.errors instanceof Array) { | 
				
			||||||
 | 
					        const errorMessages = error.properties.errors.map(function (error) { | 
				
			||||||
 | 
					            return error.properties.explanation; | 
				
			||||||
 | 
					        }).join("\n"); | 
				
			||||||
 | 
					        console.log('errorMessages', errorMessages); | 
				
			||||||
 | 
					        // errorMessages is a humanly readable message looking like this :
 | 
				
			||||||
 | 
					        // 'The tag beginning with "foobar" is unopened'
 | 
				
			||||||
 | 
					    } | 
				
			||||||
 | 
					    throw error; | 
				
			||||||
 | 
					} | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					module.exports = function (userInfo, fileName) { | 
				
			||||||
 | 
					    return new Promise((resolve, reject) => { | 
				
			||||||
 | 
					        let content = fs.readFileSync(path.resolve(__dirname, `../template/${fileName}`), 'binary'); | 
				
			||||||
 | 
					        let zip = new PizZip(content); | 
				
			||||||
 | 
					        let doc; | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        try { | 
				
			||||||
 | 
					            doc = new Docxtemplater(zip); | 
				
			||||||
 | 
					        } catch (error) { | 
				
			||||||
 | 
					            errorHandler(error); | 
				
			||||||
 | 
					        } | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        doc.setData(userInfo); | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        try { | 
				
			||||||
 | 
					            doc.render() | 
				
			||||||
 | 
					        } catch (error) { | 
				
			||||||
 | 
					            errorHandler(error); | 
				
			||||||
 | 
					        } | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        let buf = doc.getZip().generate({ | 
				
			||||||
 | 
					            type: 'nodebuffer' | 
				
			||||||
 | 
					        }); | 
				
			||||||
 | 
					        fs.writeFileSync(path.resolve(__dirname, `../public/files/${userInfo.firstName}-${userInfo.lastName}-${fileName}`), buf); | 
				
			||||||
 | 
					        resolve(`http://localhost:3000/users/download/${userInfo.firstName}-${userInfo.lastName}-${fileName}`) | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            .catch(err => { | 
				
			||||||
 | 
					                reject({ | 
				
			||||||
 | 
					                    eCode: 500, | 
				
			||||||
 | 
					                    eText: err | 
				
			||||||
 | 
					                }) | 
				
			||||||
 | 
					            }) | 
				
			||||||
 | 
					    }) | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					} | 
				
			||||||
					Loading…
					
					
				
		Reference in new issue