|
| 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; |
0 commit comments