|
| 1 | +/** |
| 2 | + * @author Shyam Hajare <hajareshyam@gmail.com> |
| 3 | + */ |
| 4 | + |
| 5 | +/** |
| 6 | + * create function is used to create pdf from handlebar templates. |
| 7 | + * @param {document, options} |
| 8 | + * @return {callback} |
| 9 | + */ |
| 10 | + |
| 11 | +import Handlebars from "handlebars"; |
| 12 | +import pdf, { CreateOptions } from "html-pdf"; |
| 13 | + |
| 14 | +Handlebars.registerHelper( |
| 15 | + "ifCond", |
| 16 | + function ( |
| 17 | + this: any, |
| 18 | + v1: any, |
| 19 | + operator: string, |
| 20 | + v2: any, |
| 21 | + options: IfCondOptions |
| 22 | + ) { |
| 23 | + switch (operator) { |
| 24 | + case "==": |
| 25 | + return v1 == v2 ? options.fn(this) : options.inverse(this); |
| 26 | + case "===": |
| 27 | + return v1 === v2 ? options.fn(this) : options.inverse(this); |
| 28 | + case "!=": |
| 29 | + return v1 != v2 ? options.fn(this) : options.inverse(this); |
| 30 | + case "!==": |
| 31 | + return v1 !== v2 ? options.fn(this) : options.inverse(this); |
| 32 | + case "<": |
| 33 | + return v1 < v2 ? options.fn(this) : options.inverse(this); |
| 34 | + case "<=": |
| 35 | + return v1 <= v2 ? options.fn(this) : options.inverse(this); |
| 36 | + case ">": |
| 37 | + return v1 > v2 ? options.fn(this) : options.inverse(this); |
| 38 | + case ">=": |
| 39 | + return v1 >= v2 ? options.fn(this) : options.inverse(this); |
| 40 | + case "&&": |
| 41 | + return v1 && v2 ? options.fn(this) : options.inverse(this); |
| 42 | + case "||": |
| 43 | + return v1 || v2 ? options.fn(this) : options.inverse(this); |
| 44 | + default: |
| 45 | + return options.inverse(this); |
| 46 | + } |
| 47 | + } |
| 48 | +); |
| 49 | + |
| 50 | +const create = function ( |
| 51 | + document: Document, |
| 52 | + options: CreateOptions |
| 53 | +): Promise<Buffer | NodeJS.ReadableStream | pdf.FileInfo> { |
| 54 | + return new Promise((resolve, reject) => { |
| 55 | + if (!document || !document.html || !document.data) { |
| 56 | + reject(new Error("Some, or all, options are missing.")); |
| 57 | + } |
| 58 | + // Compiles a template |
| 59 | + const html = Handlebars.compile(document.html)(document.data); |
| 60 | + const pdfPromise = pdf.create(html, options); |
| 61 | + |
| 62 | + // Create PDF from html template generated by handlebars |
| 63 | + // Output will be PDF file |
| 64 | + |
| 65 | + switch (document.type) { |
| 66 | + case "buffer": |
| 67 | + pdfPromise.toBuffer((err, res) => { |
| 68 | + if (!err) resolve(res); |
| 69 | + else reject(err); |
| 70 | + }); |
| 71 | + break; |
| 72 | + |
| 73 | + case "stream": |
| 74 | + pdfPromise.toStream((err, res) => { |
| 75 | + if (!err) resolve(res); |
| 76 | + else reject(err); |
| 77 | + }); |
| 78 | + break; |
| 79 | + |
| 80 | + default: |
| 81 | + pdfPromise.toFile(document.path!, (err, res) => { |
| 82 | + if (!err) resolve(res); |
| 83 | + else reject(err); |
| 84 | + }); |
| 85 | + break; |
| 86 | + } |
| 87 | + }); |
| 88 | +}; |
| 89 | + |
| 90 | +module.exports.create = create; |
0 commit comments