|
2 | 2 |
|
3 | 3 | **Note:** PDF Made Easy is [ECMAScript modules only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c), meaning, it can only be `import`ed, not `require`d. |
4 | 4 |
|
5 | | -The full documentation for PDF Made Easy's API is yet to be written. However, for now, [intellisense](https://en.wikipedia.org/wiki/Intelligent_code_completion) and the [library's declaration file](../index.d.ts) would probably give you a good enough idea of how to use it. |
| 5 | +The full documentation for PDF Made Easy's API is yet to be written. However, for now, [IntelliSense](https://en.wikipedia.org/wiki/Intelligent_code_completion) and the [library's declaration file](../index.d.ts) would probably give you a good enough idea of how to use it. |
6 | 6 |
|
7 | 7 | For most use cases where a customized script is needed, the `Builder` object will likely suffice. Below is an example of how to use it: |
8 | 8 |
|
9 | 9 | ```js |
10 | 10 | import Handlebars from "handlebars"; |
11 | | -import { getBuilder, getDefaultTemplateRenderer } from "pdf-made-easy"; |
| 11 | +import { getBuilder } from "pdf-made-easy"; |
12 | 12 |
|
13 | 13 | // Instantiates the 'Builder' object and resources needed for PDF generation. |
14 | 14 | const builder = await getBuilder(); |
15 | 15 |
|
16 | 16 | // Generate a PDF from 'template.liquid' with data injected from 'data.yml' and |
17 | | -// emit it to an 'output.pdf' file. |
18 | | -// Relative file paths will be resolved in the current working directory. |
19 | | -// 'getDefaultTemplateRenderer' returns the default template renderer which |
20 | | -// uses Liquid. |
| 17 | +// emit it to an 'output.pdf' file. Relative file paths will be resolved in the |
| 18 | +// current working directory. |
21 | 19 | await builder.build({ |
22 | 20 | data: "data.yml", |
23 | 21 | template: "template.liquid", |
24 | | - output: "output.pdf", |
25 | | - getTemplateRenderer: getDefaultTemplateRenderer |
| 22 | + output: "output.pdf" |
26 | 23 | }); |
27 | 24 |
|
28 | 25 | // Use a JSON data file. |
29 | 26 | await builder.build({ |
30 | 27 | data: "data.json", |
31 | 28 | template: "template.liquid", |
32 | | - output: "output.pdf", |
33 | | - getTemplateRenderer: getDefaultTemplateRenderer |
| 29 | + output: "output.pdf" |
34 | 30 | }); |
35 | 31 |
|
36 | 32 | // Resolve relative file paths in a custom directory. |
37 | 33 | await builder.build( |
38 | 34 | { |
39 | 35 | data: "data.yml", |
40 | 36 | template: "template.liquid", |
41 | | - output: "output.pdf", |
42 | | - getTemplateRenderer: getDefaultTemplateRenderer |
| 37 | + output: "output.pdf" |
43 | 38 | }, |
44 | 39 | "/my/custom/root/directory/where/relative/paths/will/be/resolved/in" |
45 | 40 | ); |
46 | 41 |
|
47 | | -// Use Handlebars as the templating engine. |
| 42 | +// Pass options to the templating engine and the PDF renderer. |
48 | 43 | await builder.build({ |
49 | 44 | data: "data.yml", |
50 | | - template: "template.hbs", |
| 45 | + template: "template.liquid", |
51 | 46 | output: "output.pdf", |
52 | | - getTemplateRenderer(options) { |
53 | | - return (template, data) => Handlebars.compile(template, options)(data); |
| 47 | + options: { |
| 48 | + templateOptions: { |
| 49 | + jsTruthy: true, |
| 50 | + globals: { |
| 51 | + custom: "global variables here" |
| 52 | + } |
| 53 | + }, |
| 54 | + pdfOptions: { |
| 55 | + format: "LEGAL", |
| 56 | + landscape: true, |
| 57 | + margin: { |
| 58 | + top: "0.5in", |
| 59 | + bottom: "0.5in", |
| 60 | + left: "0.5in", |
| 61 | + right: "0.5in" |
| 62 | + } |
| 63 | + } |
54 | 64 | } |
55 | 65 | }); |
56 | 66 |
|
57 | | -// Pass options to the templating engine and the PDF renderer. |
| 67 | +// Use Handlebars as the templating engine. |
58 | 68 | await builder.build({ |
59 | 69 | data: "data.yml", |
60 | | - template: "template.liquid", |
| 70 | + template: "template.hbs", |
61 | 71 | output: "output.pdf", |
62 | | - getTemplateRenderer: getDefaultTemplateRenderer, |
63 | 72 | options: { |
| 73 | + // The @type JSDoc adds proper IntelliSense. When using TypeScript, the type |
| 74 | + // can be passed as a generic type to 'Builder.build'. |
| 75 | + /** @type {Parameters<Handlebars.compile>[1]} */ |
64 | 76 | templateOptions: { |
65 | | - // ... |
| 77 | + noEscape: true, |
| 78 | + strict: true |
66 | 79 | }, |
67 | | - pdfOptions: { |
68 | | - // ... |
| 80 | + getTemplateRenderer(options) { |
| 81 | + return (template, data) => Handlebars.compile(template, options)(data); |
69 | 82 | } |
70 | 83 | } |
71 | 84 | }); |
72 | 85 |
|
73 | | -// Dispose all instatiated resouces so the script can exit. |
| 86 | +// Dispose all instantiated resouces so the script can exit. |
74 | 87 | await builder.close(); |
75 | 88 | ``` |
0 commit comments