Skip to content

Commit 8157584

Browse files
authored
Merge pull request #124 from lucas-lourencoo/master
adds `Typescript` to project
2 parents 84aa2bc + 1d1ac2c commit 8157584

7 files changed

Lines changed: 805 additions & 82 deletions

File tree

index.js

Lines changed: 0 additions & 78 deletions
This file was deleted.

index.ts

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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;

package-lock.json

Lines changed: 77 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@
4747
"license": "ISC",
4848
"dependencies": {
4949
"handlebars": "^4.7.7",
50-
"html-pdf": "^3.0.1"
50+
"html-pdf": "^3.0.1",
51+
"typescript": "^5.7.3"
52+
},
53+
"devDependencies": {
54+
"@types/html-pdf": "^3.0.3",
55+
"@types/node": "^22.13.0"
5156
}
5257
}

tsconfig.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"compilerOptions": {
3+
/* Visit https://aka.ms/tsconfig to read more about this file */
4+
"target": "es2016",
5+
"module": "commonjs",
6+
"esModuleInterop": true,
7+
"forceConsistentCasingInFileNames": true,
8+
"strict": true,
9+
"skipLibCheck": true
10+
}
11+
}

types/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
interface IfCondOptions {
2+
fn: (context: any) => string;
3+
inverse: (context: any) => string;
4+
}
5+
6+
interface Document {
7+
html: string;
8+
data: any;
9+
type: "buffer" | "stream" | "file";
10+
path?: string;
11+
}

0 commit comments

Comments
 (0)