@shad0wz7/nestjs-pdf 2.0.1 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -3,13 +3,12 @@
3
3
  A small NestJS library to generate and manipulate PDFs using Puppeteer and multiple template engines, including Handlebars, EJS, Pug, and MJML.
4
4
 
5
5
  Main features:
6
+
6
7
  - Generate PDFs from HTML, Handlebars, EJS, Pug or MJML templates
7
8
  - Concurrency limiting for Puppeteer jobs (p-limit)
8
9
  - Add signature fields based on a text anchor in an existing PDF
9
10
  - Simple API (module and service) to integrate easily into a Nest application
10
11
 
11
- > Note: this repository contains the library source. To use it in another project, you can either install it from npm or import the local package from the source.
12
-
13
12
  ## Installation
14
13
 
15
14
  To add the library inside your project :
@@ -29,6 +28,16 @@ pnpm add ../path/to/nestjs-pdf
29
28
 
30
29
  Import the module and configure it in your Nest application:
31
30
 
31
+ To use a template engine, you need to install the corresponding package as a dependency in your project:
32
+
33
+ - For Handlebars: `npm install @gboutte/nestjs-hbs`
34
+ - For EJS: `npm install ejs`
35
+ - For Pug: `npm install pug`
36
+ - For MJML: `npm install mjml`
37
+ - For Nunjucks: `npm install nunjucks`
38
+ - For Eta: `npm install eta`
39
+ - For Mustache: `npm install mustache`
40
+
32
41
  ```ts
33
42
  import { Module } from '@nestjs/common';
34
43
  import { NestjsPdfModule } from 'nestjs-pdf';
@@ -68,11 +77,17 @@ Use a Handlebars template (string or file):
68
77
 
69
78
  ```ts
70
79
  const template = '<h1>{{title}}</h1><p>{{content}}</p>';
71
- const pdf = await pdfService.generatePdfFromTemplateHbsString(template, { title: 'T', content: '...' });
80
+ const pdf = await pdfService.generatePdfFromTemplateHbsString(template, {
81
+ title: 'T',
82
+ content: '...',
83
+ });
72
84
  ```
73
85
 
74
86
  ```ts
75
- const pdf = await pdfService.generatePdfFromTemplateHbsFile(filePath, { title: 'T', content: '...' });
87
+ const pdf = await pdfService.generatePdfFromTemplateHbsFile(filePath, {
88
+ title: 'T',
89
+ content: '...',
90
+ });
76
91
  ```
77
92
 
78
93
  Add a signature field to an existing PDF (anchor-based):
@@ -94,9 +109,67 @@ If the anchor is not found, the signature field will be added at a default posit
94
109
 
95
110
  The library exposes Puppeteer options via the `PuppeteerParameters` interface (see `src/puppeteer/puppeteer-parameters.interface.ts`). You can configure:
96
111
 
97
- - `headless` (boolean | 'shell')
98
- - `pdfOptions` (native Puppeteer options for `page.pdf`)
99
- - `extraPuppeteerArgs` (launch arguments for Chromium)
112
+
113
+ | | Description |
114
+ |-----------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
115
+ | `pdfOptions` | The pdf options can be found on: https://pptr.dev/api/puppeteer.pdfoptions. Native Puppeteer options for `page.pdf` |
116
+ | `hbsOptions` | The handlebars can be found on [@gboutte/nestjs-hbs](https://github.com/gboutte/nestjs-hbs) as it use a peer dependency and not handlebars directly |
117
+ | `ejsOptions` | The ejs options can be found on [EJS documentation](https://ejs.co/#options) |
118
+ | `pugOptions` | The pug options can be found on [Pug documentation](https://pugjs.org/api/reference.html#options) |
119
+ | `mjmlOptions` | The mjml options can be found on [MJML documentation](https://mjml.io/documentation/#options) |
120
+ | `nunjucksOptions` | The nunjucks options can be found on [Nunjucks documentation](https://mozilla.github.io/nunjucks/api.html#configure) |
121
+ | `etaOptions` | The eta options can be found on [Eta documentation](https://eta.js.org/docs/4.x.x/api/configuration) |
122
+ | `mustacheOptions` | The mustache options can be found on [Mustache documentation](https://mustache.github.io/mustache.5.html) |
123
+ | `browser` | The browser to use for the PDF generation. By default: `Browser.CHROMIUM`. Allowed values `Browser.CHROMIUM`, `Browser.CHROMEHEADLESSSHELL`, `Browser.CHROME`, `Browser.FIREFOX`, `Browser.CHROMEDRIVER` ([Official documentation of Browser](https://pptr.dev/browsers-api/browsers.browser)) |
124
+ | `browserTag` | The version of the browser to use for the PDF generation. By default: `BrowserTag.LATEST` if Browser is **CHROMIUM** else `BrowserTag.STABLE`. Allowed values `BrowserTag.STABLE`, `BrowserTag.LATEST`, `BrowserTag.BETA`, `BrowserTag.DEV`, `BrowserTag.CANARY` |
125
+ | `browserInstallBaseUrl` | The baseUrl used for the installation of the browser. This baseUrl is passed to the [`install`](https://pptr.dev/browsers-api/browsers.install) method of `@puppeteer/browsers` |
126
+ | `headless` | Define if you want to use chromium headless or not, or the old headless version (`shell`). By default it's `true`. Allowed values: `true`,`false`,`"shell"` |
127
+ | `useLockedBrowser` | Define if you want to use the locked version of the browser. By default it's `false`. Allowed values: `true`,`false` |
128
+ | `buildId` | You can force the build id. Should be `string` |
129
+ | `cleanupBrowserCacheOnExit` | Define if you want to clean up the browser cache folder on exit. By default it's `true`. Allowed values: `true`,`false` |
130
+ | `extraPuppeteerArgs` | It passes some extra arguments to Puppeteer's launch method. You can check the default args at bellow. Should be `string[]` |
131
+
132
+ these are the default extra arguments passed to Puppeteer:
133
+ ```ts
134
+ [
135
+ '--autoplay-policy=user-gesture-required',
136
+ '--disable-background-networking',
137
+ '--disable-background-timer-throttling',
138
+ '--disable-backgrounding-occluded-windows',
139
+ '--disable-breakpad',
140
+ '--disable-client-side-phishing-detection',
141
+ '--disable-component-update',
142
+ '--disable-default-apps',
143
+ '--disable-dev-shm-usage',
144
+ '--disable-domain-reliability',
145
+ '--disable-extensions',
146
+ '--disable-features=AudioServiceOutOfProcess',
147
+ '--disable-hang-monitor',
148
+ '--disable-ipc-flooding-protection',
149
+ '--disable-notifications',
150
+ '--disable-offer-store-unmasked-wallet-cards',
151
+ '--disable-popup-blocking',
152
+ '--disable-print-preview',
153
+ '--disable-prompt-on-repost',
154
+ '--disable-renderer-backgrounding',
155
+ '--disable-setuid-sandbox',
156
+ '--disable-speech-api',
157
+ '--disable-sync',
158
+ '--hide-scrollbars',
159
+ '--ignore-gpu-blacklist',
160
+ '--metrics-recording-only',
161
+ '--mute-audio',
162
+ '--no-default-browser-check',
163
+ '--no-first-run',
164
+ '--no-pings',
165
+ '--password-store=basic',
166
+ '--use-gl=swiftshader',
167
+ '--use-mock-keychain',
168
+ '--disable-accelerated-2d-canvas',
169
+ '--no-zygote',
170
+ '--disable-gpu',
171
+ ];
172
+ ```
100
173
 
101
174
  You can use `forRootAsync(...)` if you need to provide configuration asynchronously.
102
175
 
package/dist/index.d.mts CHANGED
@@ -1,11 +1,14 @@
1
1
  import { OnModuleDestroy, DynamicModule, ModuleMetadata, OnModuleInit, InjectionToken, OptionalFactoryDependency } from '@nestjs/common';
2
2
  import { HandlebarsOptions } from '@gboutte/nestjs-hbs/dist/handlebars-options.interface';
3
- import * as puppeteerBrowser from '@puppeteer/browsers';
3
+ import * as _puppeteer_browsers from '@puppeteer/browsers';
4
4
  import { Browser as Browser$1 } from '@puppeteer/browsers';
5
5
  import { Browser, BrowserContext, PDFOptions } from 'puppeteer';
6
6
  import { MJMLParsingOptions } from 'mjml-core';
7
- import pug, { LocalsObject } from 'pug';
8
- import { Options, Data } from 'ejs';
7
+ import { Options, LocalsObject } from 'pug';
8
+ import { Options as Options$1, Data } from 'ejs';
9
+ import { ConfigureOptions } from 'nunjucks';
10
+ import { EtaConfig } from 'eta';
11
+ import { EscapeFunction } from 'mustache';
9
12
  import { HandlebarsService } from '@gboutte/nestjs-hbs';
10
13
 
11
14
  declare enum BrowserTag {
@@ -35,7 +38,7 @@ declare class BrowserService implements OnModuleDestroy {
35
38
  markJobFinished(): Promise<void>;
36
39
  recycleBrowserIfNeeded(): Promise<void>;
37
40
  private cleanupCacheBestEffort;
38
- install(lock?: boolean): Promise<puppeteerBrowser.InstalledBrowser | null | undefined>;
41
+ install(lock?: boolean): Promise<_puppeteer_browsers.InstalledBrowser | null | undefined>;
39
42
  private registerCleanupHooks;
40
43
  private loadBrowser;
41
44
  setBrowser(browser: Browser$1): void;
@@ -53,13 +56,13 @@ declare class BrowserService implements OnModuleDestroy {
53
56
  getLockedBuildId(browser: Browser$1): string | null;
54
57
  }
55
58
 
56
- type PugOptions = pug.Options;
59
+ type PugOptions = Options;
57
60
  declare class PugService {
58
- render(template: string, data?: pug.LocalsObject, options?: PugOptions): string;
59
- renderFile(filePath: string, data?: pug.LocalsObject, options?: PugOptions): string;
61
+ render(template: string, data?: LocalsObject, options?: PugOptions): string;
62
+ renderFile(filePath: string, data?: LocalsObject, options?: PugOptions): string;
60
63
  }
61
64
 
62
- interface EjsOptions extends Omit<Options, 'async'> {
65
+ interface EjsOptions extends Omit<Options$1, 'async'> {
63
66
  async?: boolean;
64
67
  }
65
68
  declare class EjsService {
@@ -67,12 +70,102 @@ declare class EjsService {
67
70
  renderFile(filePath: string, data?: Data, options?: EjsOptions): Promise<string>;
68
71
  }
69
72
 
73
+ interface NunjucksOptions extends Partial<ConfigureOptions> {
74
+ noCache?: boolean;
75
+ watch?: boolean;
76
+ throwOnUndefined?: boolean;
77
+ trimBlocks?: boolean;
78
+ lstripBlocks?: boolean;
79
+ }
80
+ declare class NunjucksService {
81
+ constructor();
82
+ render(template: string, data?: Record<string, unknown>, options?: NunjucksOptions): string;
83
+ renderFile(filePath: string, data?: Record<string, unknown>, options?: NunjucksOptions): string;
84
+ }
85
+
86
+ interface EtaOptions extends Partial<EtaConfig> {
87
+ cache?: boolean;
88
+ autoEscape?: boolean;
89
+ varName?: string;
90
+ views?: string;
91
+ useWith?: boolean;
92
+ plugins?: any[];
93
+ }
94
+ declare class EtaService {
95
+ private eta;
96
+ private readonly templateCache;
97
+ private readonly maxCacheSize;
98
+ constructor();
99
+ /**
100
+ * Render an Eta template from a string
101
+ * @param template The template string to render
102
+ * @param data The data object to use for rendering
103
+ * @param options Optional Eta rendering options
104
+ * @returns The rendered HTML string
105
+ */
106
+ render(template: string, data?: Record<string, unknown>, options?: EtaOptions): string;
107
+ /**
108
+ * Render an Eta template from a file
109
+ * @param filePath The path to the template file
110
+ * @param data The data object to use for rendering
111
+ * @param options Optional Eta rendering options
112
+ * @returns The rendered HTML string
113
+ */
114
+ renderFile(filePath: string, data?: Record<string, unknown>, options?: EtaOptions): string;
115
+ /**
116
+ * Clear the template cache
117
+ */
118
+ clearCache(): void;
119
+ /**
120
+ * Get the current cache size
121
+ * @returns The number of cached templates
122
+ */
123
+ getCacheSize(): number;
124
+ }
125
+
126
+ interface MustacheOptions {
127
+ tags?: [string, string];
128
+ escape?: EscapeFunction;
129
+ }
130
+ declare class MustacheService {
131
+ private readonly templateCache;
132
+ private readonly maxCacheSize;
133
+ /**
134
+ * Render a Mustache template from a string
135
+ * @param template The template string to render
136
+ * @param data The data object to use for rendering
137
+ * @param options Optional Mustache rendering options
138
+ * @returns The rendered HTML string
139
+ */
140
+ render(template: string, data?: Record<string, unknown>, options?: MustacheOptions): string;
141
+ /**
142
+ * Render a Mustache template from a file
143
+ * @param filePath The path to the template file
144
+ * @param data The data object to use for rendering
145
+ * @param options Optional Mustache rendering options
146
+ * @returns The rendered HTML string
147
+ */
148
+ renderFile(filePath: string, data?: Record<string, unknown>, options?: MustacheOptions): string;
149
+ /**
150
+ * Clear the template cache
151
+ */
152
+ clearCache(): void;
153
+ /**
154
+ * Get the current cache size
155
+ * @returns The number of cached templates
156
+ */
157
+ getCacheSize(): number;
158
+ }
159
+
70
160
  interface PuppeteerParameters {
71
161
  pdfOptions?: PDFOptions;
72
162
  hbsOptions?: HandlebarsOptions;
73
163
  mjmlOptions?: MJMLParsingOptions;
74
164
  pugOptions?: PugOptions;
75
165
  ejsOptions?: EjsOptions;
166
+ nunjucksOptions?: NunjucksOptions;
167
+ etaOptions?: EtaOptions;
168
+ mustacheOptions?: MustacheOptions;
76
169
  chromiumRevision?: string;
77
170
  buildId?: string;
78
171
  headless?: boolean | 'shell';
@@ -105,7 +198,10 @@ declare class PuppeteerService {
105
198
  private readonly mjmlService?;
106
199
  private readonly pugService?;
107
200
  private readonly ejsService?;
108
- constructor(hbsService: HandlebarsService, browserService: BrowserService, options: PuppeteerParameters, mjmlService?: MjmlService | undefined, pugService?: PugService | undefined, ejsService?: EjsService | undefined);
201
+ private readonly nunjucksService?;
202
+ private readonly etaService?;
203
+ private readonly mustacheService?;
204
+ constructor(hbsService: HandlebarsService, browserService: BrowserService, options: PuppeteerParameters, mjmlService?: MjmlService | undefined, pugService?: PugService | undefined, ejsService?: EjsService | undefined, nunjucksService?: NunjucksService | undefined, etaService?: EtaService | undefined, mustacheService?: MustacheService | undefined);
109
205
  private readonly limit;
110
206
  generatePdfFromHtml(html: string, options?: PuppeteerParameters): Promise<Uint8Array>;
111
207
  generatePdfFromTemplateHbsString(template: string, parameters?: any, options?: PuppeteerParameters): Promise<Uint8Array<ArrayBufferLike>>;
@@ -116,6 +212,12 @@ declare class PuppeteerService {
116
212
  generatePdfFromPugFile(file: string, data?: LocalsObject, options?: PuppeteerParameters): Promise<Uint8Array<ArrayBufferLike>>;
117
213
  generatePdfFromEjsString(template: string, data?: Data, options?: PuppeteerParameters): Promise<Uint8Array<ArrayBufferLike>>;
118
214
  generatePdfFromEjsFile(file: string, data?: Data, options?: PuppeteerParameters): Promise<Uint8Array<ArrayBufferLike>>;
215
+ generatePdfFromNunjucksString(template: string, data?: Record<string, unknown>, options?: PuppeteerParameters): Promise<Uint8Array<ArrayBufferLike>>;
216
+ generatePdfFromNunjucksFile(file: string, data?: Record<string, unknown>, options?: PuppeteerParameters): Promise<Uint8Array<ArrayBufferLike>>;
217
+ generatePdfFromEtaString(template: string, data?: Record<string, unknown>, options?: PuppeteerParameters): Promise<Uint8Array<ArrayBufferLike>>;
218
+ generatePdfFromEtaFile(file: string, data?: Record<string, unknown>, options?: PuppeteerParameters): Promise<Uint8Array<ArrayBufferLike>>;
219
+ generatePdfFromMustacheString(template: string, data?: Record<string, unknown>, options?: PuppeteerParameters): Promise<Uint8Array<ArrayBufferLike>>;
220
+ generatePdfFromMustacheFile(file: string, data?: Record<string, unknown>, options?: PuppeteerParameters): Promise<Uint8Array<ArrayBufferLike>>;
119
221
  }
120
222
 
121
223
  declare class NestjsPdfService {
@@ -128,24 +230,6 @@ declare class NestjsPdfService {
128
230
  * @description This method uses the Puppeteer service to generate a PDF from an HTML string. Generation options can be customized by passing a `PuppeteerParameters` object as the second argument. This allows you to control various aspects of PDF rendering, such as page size, margins, background display, etc. If no options are provided, the default parameters defined in the Puppeteer service will be used.
129
231
  */
130
232
  generatePdfFromHtml(html: string, options?: PuppeteerParameters): Promise<Uint8Array<ArrayBufferLike>>;
131
- /**
132
- * @deprecated Use the new method generatePdfFromTemplateHbsString
133
- * @param template Handlebars template string
134
- * @param parameters Data parameters to inject into the template
135
- * @param options PDF generation options (optional)
136
- * @returns PDF generated from the provided Handlebars template with parameters applied
137
- * @description This method generates a PDF from a Handlebars template provided as a string. The parameters to inject into the template are passed in the `parameters` object, which is used to render the template before converting it to PDF. PDF generation options can also be customized by passing a `PuppeteerParameters` object as the third argument. This allows you to control various aspects of PDF rendering, such as page size, margins, background display, etc. If no options are provided, the default parameters defined in the Puppeteer service will be used.
138
- */
139
- generatePdfFromTemplateString(template: string, parameters?: any, options?: PuppeteerParameters): Promise<Uint8Array<ArrayBufferLike>>;
140
- /**
141
- * @deprecated Use the new method generatePdfFromTemplateHbsFile
142
- * @param file Path to the Handlebars template file
143
- * @param parameters Data parameters to inject into the template
144
- * @param options PDF generation options (optional)
145
- * @returns PDF generated from the Handlebars template at the provided file path with parameters applied
146
- * @description This method generates a PDF from a Handlebars template located at the specified file path. The file path is passed as a string in the `file` parameter. The parameters to inject into the template are passed in the `parameters` object, which is used to render the template before converting it to PDF. PDF generation options can also be customized by passing a `PuppeteerParameters` object as the third argument. This allows you to control various aspects of PDF rendering, such as page size, margins, background display, etc. If no options are provided, the default parameters defined in the Puppeteer service will be used.
147
- */
148
- generatePdfFromTemplateFile(file: string, parameters?: any, options?: PuppeteerParameters): Promise<Uint8Array<ArrayBufferLike>>;
149
233
  /**
150
234
  * @param template Handlebars template string
151
235
  * @param parameters Data parameters to inject into the template
@@ -208,6 +292,54 @@ declare class NestjsPdfService {
208
292
  * @description This method generates a PDF from a Pug template located at the specified file path. The file path is passed as a string in the `file` parameter. The data to inject into the template is passed in the `data` object, which is used to render the template before converting it to PDF. PDF generation options can be customized by passing a `PuppeteerParameters` object as the third argument.
209
293
  */
210
294
  generatePdfFromPugFile(file: string, data?: LocalsObject, options?: PuppeteerParameters): Promise<Uint8Array<ArrayBufferLike>>;
295
+ /**
296
+ * @param template Nunjucks template string
297
+ * @param data Data to inject into the template
298
+ * @param options PDF generation options (optional)
299
+ * @returns PDF generated from the provided Nunjucks template with data applied
300
+ * @description This method generates a PDF from a Nunjucks template provided as a string. Nunjucks is a powerful templating language with support for inheritance, macros, and filters. The data to inject into the template is passed in the `data` object, which is used to render the template before converting it to PDF. PDF generation options can be customized by passing a `PuppeteerParameters` object as the third argument.
301
+ */
302
+ generatePdfFromNunjucksString(template: string, data?: Record<string, unknown>, options?: PuppeteerParameters): Promise<Uint8Array<ArrayBufferLike>>;
303
+ /**
304
+ * @param file Path to the Nunjucks template file
305
+ * @param data Data to inject into the template
306
+ * @param options PDF generation options (optional)
307
+ * @returns PDF generated from the Nunjucks template at the provided file path with data applied
308
+ * @description This method generates a PDF from a Nunjucks template located at the specified file path. The file path is passed as a string in the `file` parameter. The data to inject into the template is passed in the `data` object, which is used to render the template before converting it to PDF. PDF generation options can be customized by passing a `PuppeteerParameters` object as the third argument.
309
+ */
310
+ generatePdfFromNunjucksFile(file: string, data?: Record<string, unknown>, options?: PuppeteerParameters): Promise<Uint8Array<ArrayBufferLike>>;
311
+ /**
312
+ * @param template Eta template string
313
+ * @param data Data to inject into the template
314
+ * @param options PDF generation options (optional)
315
+ * @returns PDF generated from the provided Eta template with data applied
316
+ * @description This method generates a PDF from an Eta template provided as a string. Eta is a fast and lightweight embedded templating language with a syntax similar to EJS but with better performance. The data to inject into the template is passed in the `data` object, which is used to render the template before converting it to PDF. PDF generation options can be customized by passing a `PuppeteerParameters` object as the third argument.
317
+ */
318
+ generatePdfFromEtaString(template: string, data?: Record<string, unknown>, options?: PuppeteerParameters): Promise<Uint8Array<ArrayBufferLike>>;
319
+ /**
320
+ * @param file Path to the Eta template file
321
+ * @param data Data to inject into the template
322
+ * @param options PDF generation options (optional)
323
+ * @returns PDF generated from the Eta template at the provided file path with data applied
324
+ * @description This method generates a PDF from an Eta template located at the specified file path. The file path is passed as a string in the `file` parameter. The data to inject into the template is passed in the `data` object, which is used to render the template before converting it to PDF. PDF generation options can be customized by passing a `PuppeteerParameters` object as the third argument.
325
+ */
326
+ generatePdfFromEtaFile(file: string, data?: Record<string, unknown>, options?: PuppeteerParameters): Promise<Uint8Array<ArrayBufferLike>>;
327
+ /**
328
+ * @param template Mustache template string
329
+ * @param data Data to inject into the template
330
+ * @param options PDF generation options (optional)
331
+ * @returns PDF generated from the provided Mustache template with data applied
332
+ * @description This method generates a PDF from a Mustache template provided as a string. Mustache is a logic-less template language that emphasizes readable and simple templates. The data to inject into the template is passed in the `data` object, which is used to render the template before converting it to PDF. PDF generation options can be customized by passing a `PuppeteerParameters` object as the third argument.
333
+ */
334
+ generatePdfFromMustacheString(template: string, data?: Record<string, unknown>, options?: PuppeteerParameters): Promise<Uint8Array<ArrayBufferLike>>;
335
+ /**
336
+ * @param file Path to the Mustache template file
337
+ * @param data Data to inject into the template
338
+ * @param options PDF generation options (optional)
339
+ * @returns PDF generated from the Mustache template at the provided file path with data applied
340
+ * @description This method generates a PDF from a Mustache template located at the specified file path. The file path is passed as a string in the `file` parameter. The data to inject into the template is passed in the `data` object, which is used to render the template before converting it to PDF. PDF generation options can be customized by passing a `PuppeteerParameters` object as the third argument.
341
+ */
342
+ generatePdfFromMustacheFile(file: string, data?: Record<string, unknown>, options?: PuppeteerParameters): Promise<Uint8Array<ArrayBufferLike>>;
211
343
  /**
212
344
  * @param pdf The PDF bytes to modify
213
345
  * @param fieldName The name of the signature field to add (default: 'SignatureDebtor')
@@ -257,4 +389,4 @@ declare class PuppeteerModule implements OnModuleInit {
257
389
  */
258
390
  declare function getRelativePathToValue(moduleDir: string, relativeToModule: string): string;
259
391
 
260
- export { type EjsOptions, EjsService, MjmlService, NestjsPdfModule, NestjsPdfService, type PdfLibModuleAsyncOptions, type PugOptions, PugService, PuppeteerModule, type PuppeteerModuleAsyncOptions, type PuppeteerParameters, PuppeteerService, getRelativePathToValue };
392
+ export { type EjsOptions, EjsService, type EtaOptions, EtaService, MjmlService, type MustacheOptions, MustacheService, NestjsPdfModule, NestjsPdfService, type NunjucksOptions, NunjucksService, type PdfLibModuleAsyncOptions, type PugOptions, PugService, PuppeteerModule, type PuppeteerModuleAsyncOptions, type PuppeteerParameters, PuppeteerService, getRelativePathToValue };
package/dist/index.d.ts CHANGED
@@ -1,11 +1,14 @@
1
1
  import { OnModuleDestroy, DynamicModule, ModuleMetadata, OnModuleInit, InjectionToken, OptionalFactoryDependency } from '@nestjs/common';
2
2
  import { HandlebarsOptions } from '@gboutte/nestjs-hbs/dist/handlebars-options.interface';
3
- import * as puppeteerBrowser from '@puppeteer/browsers';
3
+ import * as _puppeteer_browsers from '@puppeteer/browsers';
4
4
  import { Browser as Browser$1 } from '@puppeteer/browsers';
5
5
  import { Browser, BrowserContext, PDFOptions } from 'puppeteer';
6
6
  import { MJMLParsingOptions } from 'mjml-core';
7
- import pug, { LocalsObject } from 'pug';
8
- import { Options, Data } from 'ejs';
7
+ import { Options, LocalsObject } from 'pug';
8
+ import { Options as Options$1, Data } from 'ejs';
9
+ import { ConfigureOptions } from 'nunjucks';
10
+ import { EtaConfig } from 'eta';
11
+ import { EscapeFunction } from 'mustache';
9
12
  import { HandlebarsService } from '@gboutte/nestjs-hbs';
10
13
 
11
14
  declare enum BrowserTag {
@@ -35,7 +38,7 @@ declare class BrowserService implements OnModuleDestroy {
35
38
  markJobFinished(): Promise<void>;
36
39
  recycleBrowserIfNeeded(): Promise<void>;
37
40
  private cleanupCacheBestEffort;
38
- install(lock?: boolean): Promise<puppeteerBrowser.InstalledBrowser | null | undefined>;
41
+ install(lock?: boolean): Promise<_puppeteer_browsers.InstalledBrowser | null | undefined>;
39
42
  private registerCleanupHooks;
40
43
  private loadBrowser;
41
44
  setBrowser(browser: Browser$1): void;
@@ -53,13 +56,13 @@ declare class BrowserService implements OnModuleDestroy {
53
56
  getLockedBuildId(browser: Browser$1): string | null;
54
57
  }
55
58
 
56
- type PugOptions = pug.Options;
59
+ type PugOptions = Options;
57
60
  declare class PugService {
58
- render(template: string, data?: pug.LocalsObject, options?: PugOptions): string;
59
- renderFile(filePath: string, data?: pug.LocalsObject, options?: PugOptions): string;
61
+ render(template: string, data?: LocalsObject, options?: PugOptions): string;
62
+ renderFile(filePath: string, data?: LocalsObject, options?: PugOptions): string;
60
63
  }
61
64
 
62
- interface EjsOptions extends Omit<Options, 'async'> {
65
+ interface EjsOptions extends Omit<Options$1, 'async'> {
63
66
  async?: boolean;
64
67
  }
65
68
  declare class EjsService {
@@ -67,12 +70,102 @@ declare class EjsService {
67
70
  renderFile(filePath: string, data?: Data, options?: EjsOptions): Promise<string>;
68
71
  }
69
72
 
73
+ interface NunjucksOptions extends Partial<ConfigureOptions> {
74
+ noCache?: boolean;
75
+ watch?: boolean;
76
+ throwOnUndefined?: boolean;
77
+ trimBlocks?: boolean;
78
+ lstripBlocks?: boolean;
79
+ }
80
+ declare class NunjucksService {
81
+ constructor();
82
+ render(template: string, data?: Record<string, unknown>, options?: NunjucksOptions): string;
83
+ renderFile(filePath: string, data?: Record<string, unknown>, options?: NunjucksOptions): string;
84
+ }
85
+
86
+ interface EtaOptions extends Partial<EtaConfig> {
87
+ cache?: boolean;
88
+ autoEscape?: boolean;
89
+ varName?: string;
90
+ views?: string;
91
+ useWith?: boolean;
92
+ plugins?: any[];
93
+ }
94
+ declare class EtaService {
95
+ private eta;
96
+ private readonly templateCache;
97
+ private readonly maxCacheSize;
98
+ constructor();
99
+ /**
100
+ * Render an Eta template from a string
101
+ * @param template The template string to render
102
+ * @param data The data object to use for rendering
103
+ * @param options Optional Eta rendering options
104
+ * @returns The rendered HTML string
105
+ */
106
+ render(template: string, data?: Record<string, unknown>, options?: EtaOptions): string;
107
+ /**
108
+ * Render an Eta template from a file
109
+ * @param filePath The path to the template file
110
+ * @param data The data object to use for rendering
111
+ * @param options Optional Eta rendering options
112
+ * @returns The rendered HTML string
113
+ */
114
+ renderFile(filePath: string, data?: Record<string, unknown>, options?: EtaOptions): string;
115
+ /**
116
+ * Clear the template cache
117
+ */
118
+ clearCache(): void;
119
+ /**
120
+ * Get the current cache size
121
+ * @returns The number of cached templates
122
+ */
123
+ getCacheSize(): number;
124
+ }
125
+
126
+ interface MustacheOptions {
127
+ tags?: [string, string];
128
+ escape?: EscapeFunction;
129
+ }
130
+ declare class MustacheService {
131
+ private readonly templateCache;
132
+ private readonly maxCacheSize;
133
+ /**
134
+ * Render a Mustache template from a string
135
+ * @param template The template string to render
136
+ * @param data The data object to use for rendering
137
+ * @param options Optional Mustache rendering options
138
+ * @returns The rendered HTML string
139
+ */
140
+ render(template: string, data?: Record<string, unknown>, options?: MustacheOptions): string;
141
+ /**
142
+ * Render a Mustache template from a file
143
+ * @param filePath The path to the template file
144
+ * @param data The data object to use for rendering
145
+ * @param options Optional Mustache rendering options
146
+ * @returns The rendered HTML string
147
+ */
148
+ renderFile(filePath: string, data?: Record<string, unknown>, options?: MustacheOptions): string;
149
+ /**
150
+ * Clear the template cache
151
+ */
152
+ clearCache(): void;
153
+ /**
154
+ * Get the current cache size
155
+ * @returns The number of cached templates
156
+ */
157
+ getCacheSize(): number;
158
+ }
159
+
70
160
  interface PuppeteerParameters {
71
161
  pdfOptions?: PDFOptions;
72
162
  hbsOptions?: HandlebarsOptions;
73
163
  mjmlOptions?: MJMLParsingOptions;
74
164
  pugOptions?: PugOptions;
75
165
  ejsOptions?: EjsOptions;
166
+ nunjucksOptions?: NunjucksOptions;
167
+ etaOptions?: EtaOptions;
168
+ mustacheOptions?: MustacheOptions;
76
169
  chromiumRevision?: string;
77
170
  buildId?: string;
78
171
  headless?: boolean | 'shell';
@@ -105,7 +198,10 @@ declare class PuppeteerService {
105
198
  private readonly mjmlService?;
106
199
  private readonly pugService?;
107
200
  private readonly ejsService?;
108
- constructor(hbsService: HandlebarsService, browserService: BrowserService, options: PuppeteerParameters, mjmlService?: MjmlService | undefined, pugService?: PugService | undefined, ejsService?: EjsService | undefined);
201
+ private readonly nunjucksService?;
202
+ private readonly etaService?;
203
+ private readonly mustacheService?;
204
+ constructor(hbsService: HandlebarsService, browserService: BrowserService, options: PuppeteerParameters, mjmlService?: MjmlService | undefined, pugService?: PugService | undefined, ejsService?: EjsService | undefined, nunjucksService?: NunjucksService | undefined, etaService?: EtaService | undefined, mustacheService?: MustacheService | undefined);
109
205
  private readonly limit;
110
206
  generatePdfFromHtml(html: string, options?: PuppeteerParameters): Promise<Uint8Array>;
111
207
  generatePdfFromTemplateHbsString(template: string, parameters?: any, options?: PuppeteerParameters): Promise<Uint8Array<ArrayBufferLike>>;
@@ -116,6 +212,12 @@ declare class PuppeteerService {
116
212
  generatePdfFromPugFile(file: string, data?: LocalsObject, options?: PuppeteerParameters): Promise<Uint8Array<ArrayBufferLike>>;
117
213
  generatePdfFromEjsString(template: string, data?: Data, options?: PuppeteerParameters): Promise<Uint8Array<ArrayBufferLike>>;
118
214
  generatePdfFromEjsFile(file: string, data?: Data, options?: PuppeteerParameters): Promise<Uint8Array<ArrayBufferLike>>;
215
+ generatePdfFromNunjucksString(template: string, data?: Record<string, unknown>, options?: PuppeteerParameters): Promise<Uint8Array<ArrayBufferLike>>;
216
+ generatePdfFromNunjucksFile(file: string, data?: Record<string, unknown>, options?: PuppeteerParameters): Promise<Uint8Array<ArrayBufferLike>>;
217
+ generatePdfFromEtaString(template: string, data?: Record<string, unknown>, options?: PuppeteerParameters): Promise<Uint8Array<ArrayBufferLike>>;
218
+ generatePdfFromEtaFile(file: string, data?: Record<string, unknown>, options?: PuppeteerParameters): Promise<Uint8Array<ArrayBufferLike>>;
219
+ generatePdfFromMustacheString(template: string, data?: Record<string, unknown>, options?: PuppeteerParameters): Promise<Uint8Array<ArrayBufferLike>>;
220
+ generatePdfFromMustacheFile(file: string, data?: Record<string, unknown>, options?: PuppeteerParameters): Promise<Uint8Array<ArrayBufferLike>>;
119
221
  }
120
222
 
121
223
  declare class NestjsPdfService {
@@ -128,24 +230,6 @@ declare class NestjsPdfService {
128
230
  * @description This method uses the Puppeteer service to generate a PDF from an HTML string. Generation options can be customized by passing a `PuppeteerParameters` object as the second argument. This allows you to control various aspects of PDF rendering, such as page size, margins, background display, etc. If no options are provided, the default parameters defined in the Puppeteer service will be used.
129
231
  */
130
232
  generatePdfFromHtml(html: string, options?: PuppeteerParameters): Promise<Uint8Array<ArrayBufferLike>>;
131
- /**
132
- * @deprecated Use the new method generatePdfFromTemplateHbsString
133
- * @param template Handlebars template string
134
- * @param parameters Data parameters to inject into the template
135
- * @param options PDF generation options (optional)
136
- * @returns PDF generated from the provided Handlebars template with parameters applied
137
- * @description This method generates a PDF from a Handlebars template provided as a string. The parameters to inject into the template are passed in the `parameters` object, which is used to render the template before converting it to PDF. PDF generation options can also be customized by passing a `PuppeteerParameters` object as the third argument. This allows you to control various aspects of PDF rendering, such as page size, margins, background display, etc. If no options are provided, the default parameters defined in the Puppeteer service will be used.
138
- */
139
- generatePdfFromTemplateString(template: string, parameters?: any, options?: PuppeteerParameters): Promise<Uint8Array<ArrayBufferLike>>;
140
- /**
141
- * @deprecated Use the new method generatePdfFromTemplateHbsFile
142
- * @param file Path to the Handlebars template file
143
- * @param parameters Data parameters to inject into the template
144
- * @param options PDF generation options (optional)
145
- * @returns PDF generated from the Handlebars template at the provided file path with parameters applied
146
- * @description This method generates a PDF from a Handlebars template located at the specified file path. The file path is passed as a string in the `file` parameter. The parameters to inject into the template are passed in the `parameters` object, which is used to render the template before converting it to PDF. PDF generation options can also be customized by passing a `PuppeteerParameters` object as the third argument. This allows you to control various aspects of PDF rendering, such as page size, margins, background display, etc. If no options are provided, the default parameters defined in the Puppeteer service will be used.
147
- */
148
- generatePdfFromTemplateFile(file: string, parameters?: any, options?: PuppeteerParameters): Promise<Uint8Array<ArrayBufferLike>>;
149
233
  /**
150
234
  * @param template Handlebars template string
151
235
  * @param parameters Data parameters to inject into the template
@@ -208,6 +292,54 @@ declare class NestjsPdfService {
208
292
  * @description This method generates a PDF from a Pug template located at the specified file path. The file path is passed as a string in the `file` parameter. The data to inject into the template is passed in the `data` object, which is used to render the template before converting it to PDF. PDF generation options can be customized by passing a `PuppeteerParameters` object as the third argument.
209
293
  */
210
294
  generatePdfFromPugFile(file: string, data?: LocalsObject, options?: PuppeteerParameters): Promise<Uint8Array<ArrayBufferLike>>;
295
+ /**
296
+ * @param template Nunjucks template string
297
+ * @param data Data to inject into the template
298
+ * @param options PDF generation options (optional)
299
+ * @returns PDF generated from the provided Nunjucks template with data applied
300
+ * @description This method generates a PDF from a Nunjucks template provided as a string. Nunjucks is a powerful templating language with support for inheritance, macros, and filters. The data to inject into the template is passed in the `data` object, which is used to render the template before converting it to PDF. PDF generation options can be customized by passing a `PuppeteerParameters` object as the third argument.
301
+ */
302
+ generatePdfFromNunjucksString(template: string, data?: Record<string, unknown>, options?: PuppeteerParameters): Promise<Uint8Array<ArrayBufferLike>>;
303
+ /**
304
+ * @param file Path to the Nunjucks template file
305
+ * @param data Data to inject into the template
306
+ * @param options PDF generation options (optional)
307
+ * @returns PDF generated from the Nunjucks template at the provided file path with data applied
308
+ * @description This method generates a PDF from a Nunjucks template located at the specified file path. The file path is passed as a string in the `file` parameter. The data to inject into the template is passed in the `data` object, which is used to render the template before converting it to PDF. PDF generation options can be customized by passing a `PuppeteerParameters` object as the third argument.
309
+ */
310
+ generatePdfFromNunjucksFile(file: string, data?: Record<string, unknown>, options?: PuppeteerParameters): Promise<Uint8Array<ArrayBufferLike>>;
311
+ /**
312
+ * @param template Eta template string
313
+ * @param data Data to inject into the template
314
+ * @param options PDF generation options (optional)
315
+ * @returns PDF generated from the provided Eta template with data applied
316
+ * @description This method generates a PDF from an Eta template provided as a string. Eta is a fast and lightweight embedded templating language with a syntax similar to EJS but with better performance. The data to inject into the template is passed in the `data` object, which is used to render the template before converting it to PDF. PDF generation options can be customized by passing a `PuppeteerParameters` object as the third argument.
317
+ */
318
+ generatePdfFromEtaString(template: string, data?: Record<string, unknown>, options?: PuppeteerParameters): Promise<Uint8Array<ArrayBufferLike>>;
319
+ /**
320
+ * @param file Path to the Eta template file
321
+ * @param data Data to inject into the template
322
+ * @param options PDF generation options (optional)
323
+ * @returns PDF generated from the Eta template at the provided file path with data applied
324
+ * @description This method generates a PDF from an Eta template located at the specified file path. The file path is passed as a string in the `file` parameter. The data to inject into the template is passed in the `data` object, which is used to render the template before converting it to PDF. PDF generation options can be customized by passing a `PuppeteerParameters` object as the third argument.
325
+ */
326
+ generatePdfFromEtaFile(file: string, data?: Record<string, unknown>, options?: PuppeteerParameters): Promise<Uint8Array<ArrayBufferLike>>;
327
+ /**
328
+ * @param template Mustache template string
329
+ * @param data Data to inject into the template
330
+ * @param options PDF generation options (optional)
331
+ * @returns PDF generated from the provided Mustache template with data applied
332
+ * @description This method generates a PDF from a Mustache template provided as a string. Mustache is a logic-less template language that emphasizes readable and simple templates. The data to inject into the template is passed in the `data` object, which is used to render the template before converting it to PDF. PDF generation options can be customized by passing a `PuppeteerParameters` object as the third argument.
333
+ */
334
+ generatePdfFromMustacheString(template: string, data?: Record<string, unknown>, options?: PuppeteerParameters): Promise<Uint8Array<ArrayBufferLike>>;
335
+ /**
336
+ * @param file Path to the Mustache template file
337
+ * @param data Data to inject into the template
338
+ * @param options PDF generation options (optional)
339
+ * @returns PDF generated from the Mustache template at the provided file path with data applied
340
+ * @description This method generates a PDF from a Mustache template located at the specified file path. The file path is passed as a string in the `file` parameter. The data to inject into the template is passed in the `data` object, which is used to render the template before converting it to PDF. PDF generation options can be customized by passing a `PuppeteerParameters` object as the third argument.
341
+ */
342
+ generatePdfFromMustacheFile(file: string, data?: Record<string, unknown>, options?: PuppeteerParameters): Promise<Uint8Array<ArrayBufferLike>>;
211
343
  /**
212
344
  * @param pdf The PDF bytes to modify
213
345
  * @param fieldName The name of the signature field to add (default: 'SignatureDebtor')
@@ -257,4 +389,4 @@ declare class PuppeteerModule implements OnModuleInit {
257
389
  */
258
390
  declare function getRelativePathToValue(moduleDir: string, relativeToModule: string): string;
259
391
 
260
- export { type EjsOptions, EjsService, MjmlService, NestjsPdfModule, NestjsPdfService, type PdfLibModuleAsyncOptions, type PugOptions, PugService, PuppeteerModule, type PuppeteerModuleAsyncOptions, type PuppeteerParameters, PuppeteerService, getRelativePathToValue };
392
+ export { type EjsOptions, EjsService, type EtaOptions, EtaService, MjmlService, type MustacheOptions, MustacheService, NestjsPdfModule, NestjsPdfService, type NunjucksOptions, NunjucksService, type PdfLibModuleAsyncOptions, type PugOptions, PugService, PuppeteerModule, type PuppeteerModuleAsyncOptions, type PuppeteerParameters, PuppeteerService, getRelativePathToValue };