Skip to content

Commit 2b745f7

Browse files
🤖 Merge PR DefinitelyTyped#73411 feat : add type definitions for html2pdf.js by @Mutesa-Cedric
1 parent 9577a2c commit 2b745f7

File tree

5 files changed

+363
-0
lines changed

5 files changed

+363
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*
2+
!**/*.d.ts
3+
!**/*.d.cts
4+
!**/*.d.mts
5+
!**/*.d.*.ts
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import html2pdf = require("html2pdf.js");
2+
3+
// Interfaces
4+
let options: html2pdf.Options = {
5+
filename: "test.pdf",
6+
margin: 1,
7+
image: { type: "jpeg", quality: 0.95 },
8+
enableLinks: true,
9+
pagebreak: {
10+
mode: ["css", "legacy"],
11+
before: ".page-break",
12+
after: ".section-end",
13+
avoid: ".no-break",
14+
},
15+
html2canvas: { scale: 2 },
16+
jsPDF: { unit: "in", format: "letter", orientation: "portrait" },
17+
};
18+
19+
let imageOptions: html2pdf.ImageOptions = {
20+
type: "png",
21+
quality: 0.9,
22+
};
23+
24+
let pagebreakOptions: html2pdf.PagebreakOptions = {
25+
mode: "avoid-all",
26+
before: ["h1", ".chapter"],
27+
after: [".section-end"],
28+
avoid: ["img", ".keep-together"],
29+
};
30+
31+
// Worker interface
32+
let worker: html2pdf.Worker;
33+
worker = html2pdf();
34+
worker = html2pdf(document.createElement("div"), options);
35+
36+
// Worker methods
37+
worker = worker.from(document.createElement("div"));
38+
worker = worker.from("<div>HTML string</div>", "string");
39+
worker = worker.from(document.createElement("div"), "element");
40+
worker = worker.from(document.createElement("canvas"), "canvas");
41+
worker = worker.from(document.createElement("img"), "img");
42+
43+
worker = worker.to("container");
44+
worker = worker.toContainer();
45+
worker = worker.toCanvas();
46+
worker = worker.toImg();
47+
worker = worker.toPdf();
48+
49+
worker = worker.output("pdf", { type: "blob" });
50+
worker = worker.outputPdf("blob", { type: "application/pdf" });
51+
worker = worker.outputImg("dataurl", { quality: 0.8 });
52+
53+
worker = worker.save("test.pdf");
54+
worker = worker.set(options);
55+
worker = worker.get("filename", (value: any) => {});
56+
worker = worker.setMargin(1);
57+
worker = worker.setMargin([1, 2]);
58+
worker = worker.setMargin([1, 2, 3, 4]);
59+
worker = worker.setPageSize({ width: 210, height: 297 });
60+
worker = worker.setProgress(0.5, "processing", 1, []);
61+
worker = worker.updateProgress(0.75, "almost-done", 2, []);
62+
63+
// Promise methods
64+
worker = worker.then((result: any) => result);
65+
worker = worker.thenCore((result: any) => result);
66+
worker = worker.catch((error: any) => error);
67+
68+
// Aliases
69+
worker = worker.saveAs("test.pdf");
70+
worker = worker.using(options);
71+
worker = worker.export("pdf");
72+
worker = worker.run((result: any) => result);
73+
74+
// Error method
75+
worker = worker.error("Custom error message");
Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
/// <reference lib="dom" />
2+
3+
declare function html2pdf(
4+
source?: HTMLElement | string,
5+
options?: html2pdf.Options,
6+
): html2pdf.Worker;
7+
8+
declare namespace html2pdf {
9+
// Type aliases using branded string pattern for keeping intelisense working
10+
type ImageType = "png" | "jpeg" | "webp" | (string & {});
11+
type PagebreakMode = "avoid-all" | "css" | "legacy" | (string & {});
12+
type FromType = "string" | "element" | "canvas" | "img" | (string & {});
13+
type ToTarget = "container" | "canvas" | "img" | "pdf" | (string & {});
14+
type OutputType = "pdf" | "img" | (string & {});
15+
type OutputImgType = "img" | "datauristring" | "dataurlstring" | "datauri" | "dataurl" | (string & {});
16+
17+
/**
18+
* Worker class for Promise-based PDF generation
19+
*/
20+
interface Worker extends Promise<any> {
21+
/**
22+
* Sets the source (HTML string or element) for the PDF
23+
*/
24+
from(src: HTMLElement | string, type?: FromType): Worker;
25+
26+
/**
27+
* Converts the source to the specified target
28+
*/
29+
to(target: ToTarget): Worker;
30+
31+
/**
32+
* Converts to container
33+
*/
34+
toContainer(): Worker;
35+
36+
/**
37+
* Converts to canvas
38+
*/
39+
toCanvas(): Worker;
40+
41+
/**
42+
* Converts to image
43+
*/
44+
toImg(): Worker;
45+
46+
/**
47+
* Converts to PDF
48+
*/
49+
toPdf(): Worker;
50+
51+
/**
52+
* Routes to appropriate output method
53+
*/
54+
output(type?: OutputType, options?: any, src?: any): Worker;
55+
56+
/**
57+
* Outputs PDF with specified type and options
58+
*/
59+
outputPdf(type?: string, options?: any): Worker;
60+
61+
/**
62+
* Outputs image with specified type and options
63+
*/
64+
outputImg(type?: OutputImgType, options?: any): Worker;
65+
66+
/**
67+
* Saves the PDF with optional filename
68+
*/
69+
save(filename?: string): Worker;
70+
71+
/**
72+
* Sets options and properties
73+
*/
74+
set(options: Options | Partial<Options>): Worker;
75+
76+
/**
77+
* Gets a property value
78+
*/
79+
get(key: string, callback?: (value: any) => void): Worker;
80+
81+
/**
82+
* Sets margin
83+
*/
84+
setMargin(margin: number | number[]): Worker;
85+
86+
/**
87+
* Sets page size
88+
*/
89+
setPageSize(pageSize?: any): Worker;
90+
91+
/**
92+
* Sets progress tracking
93+
*/
94+
setProgress(val: number, state: any, n: number, stack: any[]): Worker;
95+
96+
/**
97+
* Updates progress
98+
*/
99+
updateProgress(val: number, state: any, n: number, stack: any[]): Worker;
100+
101+
/**
102+
* Promise then with progress tracking
103+
*/
104+
then<TResult1 = any, TResult2 = never>(
105+
onfulfilled?: ((value: any) => TResult1 | PromiseLike<TResult1>) | null,
106+
onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null,
107+
): Worker;
108+
109+
/**
110+
* Promise then without progress tracking
111+
*/
112+
thenCore<TResult1 = any, TResult2 = never>(
113+
onfulfilled?: ((value: any) => TResult1 | PromiseLike<TResult1>) | null,
114+
onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null,
115+
): Worker;
116+
117+
/**
118+
* True Promise then - exits Worker chain
119+
*/
120+
thenExternal<TResult1 = any, TResult2 = never>(
121+
onfulfilled?: ((value: any) => TResult1 | PromiseLike<TResult1>) | null,
122+
onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null,
123+
): Promise<TResult1 | TResult2>;
124+
125+
/**
126+
* Promise catch with progress tracking
127+
*/
128+
catch<TResult = never>(
129+
onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null,
130+
): Worker;
131+
132+
/**
133+
* True Promise catch - exits Worker chain
134+
*/
135+
catchExternal<TResult = never>(
136+
onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null,
137+
): Promise<any>;
138+
139+
/**
140+
* Throws an error in the Worker's Promise chain
141+
*/
142+
error(msg: string): Worker;
143+
144+
// Aliases
145+
saveAs(filename?: string): Worker;
146+
using(options: Options | Partial<Options>): Worker;
147+
export(type?: OutputType, options?: any, src?: any): Worker;
148+
run<TResult1 = any, TResult2 = never>(
149+
onfulfilled?: ((value: any) => TResult1 | PromiseLike<TResult1>) | null,
150+
onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null,
151+
): Worker;
152+
}
153+
154+
/**
155+
* Options for html2pdf configuration
156+
*/
157+
interface Options {
158+
/**
159+
* The default filename of the exported PDF
160+
* @default 'file.pdf'
161+
*/
162+
filename?: string | undefined;
163+
164+
/**
165+
* PDF margin (in jsPDF units). Can be a single number, [vMargin, hMargin], or [top, left, bottom, right]
166+
* @default [0,0,0,0]
167+
*/
168+
margin?: number | number[] | undefined;
169+
170+
/**
171+
* The image type and quality used to generate the PDF
172+
* @default { type: 'jpeg', quality: 0.95 }
173+
*/
174+
image?: ImageOptions;
175+
176+
/**
177+
* If enabled, PDF hyperlinks are automatically added ontop of all anchor tags
178+
* @default true
179+
*/
180+
enableLinks?: boolean | undefined;
181+
182+
/**
183+
* Controls the pagebreak behaviour on the page
184+
* @default { mode: ['css', 'legacy'] }
185+
*/
186+
pagebreak?: PagebreakOptions | undefined;
187+
188+
/**
189+
* Configuration options sent directly to html2canvas
190+
*/
191+
html2canvas?: any;
192+
193+
/**
194+
* Configuration options sent directly to jsPDF
195+
*/
196+
jsPDF?: any;
197+
}
198+
199+
/**
200+
* Image options for PDF generation
201+
*/
202+
interface ImageOptions {
203+
/**
204+
* The image type. HTMLCanvasElement only supports 'png', 'jpeg', and 'webp' (on Chrome)
205+
* @default 'jpeg'
206+
*/
207+
type?: ImageType | undefined;
208+
209+
/**
210+
* The image quality, from 0 to 1. This setting is only used for jpeg/webp (not png)
211+
* @default 0.95
212+
*/
213+
quality?: number | undefined;
214+
}
215+
216+
/**
217+
* Pagebreak options for controlling page breaks
218+
*/
219+
interface PagebreakOptions {
220+
/**
221+
* The mode(s) on which to automatically add page-breaks
222+
* @default ['css', 'legacy']
223+
*/
224+
mode?: PagebreakMode | Array<PagebreakMode> | undefined;
225+
226+
/**
227+
* CSS selectors for which to add page-breaks before each element
228+
* @default []
229+
*/
230+
before?: string | string[] | undefined;
231+
232+
/**
233+
* CSS selectors for which to add page-breaks after each element
234+
* @default []
235+
*/
236+
after?: string | string[] | undefined;
237+
238+
/**
239+
* CSS selectors for which to avoid page-breaks
240+
* @default []
241+
*/
242+
avoid?: string | string[] | undefined;
243+
}
244+
}
245+
246+
export = html2pdf;
247+
export as namespace html2pdf;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"private": true,
3+
"name": "@types/html2pdf.js",
4+
"version": "0.10.9999",
5+
"projects": [
6+
"https://github.com/eKoopmans/html2pdf.js"
7+
],
8+
"devDependencies": {
9+
"@types/html2pdf.js": "workspace:."
10+
},
11+
"owners": [
12+
{
13+
"name": "Cedric Mutesa",
14+
"githubUsername": "Mutesa-Cedric"
15+
}
16+
]
17+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"compilerOptions": {
3+
"module": "node16",
4+
"lib": [
5+
"es6"
6+
],
7+
"noImplicitAny": true,
8+
"noImplicitThis": true,
9+
"strictNullChecks": true,
10+
"strictFunctionTypes": true,
11+
"types": [],
12+
"noEmit": true,
13+
"forceConsistentCasingInFileNames": true
14+
},
15+
"files": [
16+
"index.d.ts",
17+
"html2pdf.js-tests.ts"
18+
]
19+
}

0 commit comments

Comments
 (0)