Skip to content

Commit 2069f6c

Browse files
committed
Update documentation for v0.2.0
1 parent 3569705 commit 2069f6c

File tree

3 files changed

+95
-34
lines changed

3 files changed

+95
-34
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ Thus, PME was born.
2020

2121
To fulfill the requirements that I was looking for, the PME CLI takes data from a [YAML](https://yaml.org), [JSON](https://www.json.org/json-en.html), JSON with Comments (JSONC), or [JSON5](https://json5.org) file and injects it into a template to generate HTML which is then used to generate a PDF using [Puppeteer](https://pptr.dev). This process is repeated whenever there are changes to either the data or template file so that the PDF preview can refresh automatically.
2222

23-
Presently, the default template engine used to generate HTML is [Liquid](https://liquidjs.com). This can be changed when using PME as a library, but not as a CLI. However, in the future, [it is planned](#todos) to make this configurable in the CLI as well via [the config file](docs/01-cli.md#configuration).
24-
25-
Additionally, the autorefresh feature currently only works if the viewer used to preview the PDF supports repainting when there are changes to the PDF file. Though, it is also in [the plan](#todos) to add an auto-reloading viewer together with the CLI to make previewing changes more streamlined.
23+
Currently, the autorefresh feature only works if the viewer used to preview the PDF supports repainting when there are changes to the PDF file. However, in the future, [it is planned](#todos) to add an auto-reloading viewer together with the CLI to make previewing changes more streamlined.
2624

2725
## Requirements
2826

docs/01-cli.md

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,75 @@ Examples:
2828

2929
## Configuration
3030

31-
The CLI can be configured using an [ECMAScript module](https://nodejs.org/api/esm.html) config file with a `.mjs` extension. By default, it will search for a file named `pme.config.mjs` in the current working directory. If not found, it will continue searching for this file up the directory tree. If there is no `pme.config.mjs` file found in the directory tree, it will try to look for it in the home directory. Alternatively, you can pass the path to a config file using the `--config` or `-c` option.
31+
The CLI can optionally be configured using an [ECMAScript module](https://nodejs.org/api/esm.html) config file with a `.mjs` extension. By default, it will search for a file named `pme.config.mjs` in the current working directory. If not found, it will continue searching for this file up the directory tree. If there is no `pme.config.mjs` file found in the directory tree, it will try to look for it in the home directory. Alternatively, you can pass the path to a config file using the `--config` or `-c` option.
3232

33-
If present, the config file is expected to have a default object export with the following optional properties:
33+
### Options
3434

35-
- [`templateOptions`](https://liquidjs.com/api/interfaces/liquid_options_.liquidoptions.html), this will be passed to the template renderer, which is [Liquid](https://liquidjs.com).
36-
- [`pdfOptions`](https://pptr.dev/api/puppeteer.pdfoptions), this will be passed to the PDF renderer, which is [Puppeteer](https://pptr.dev).
35+
If present, the config file is expected to have a default object export with the following optional properties and methods:
3736

38-
Below is an example of a config file:
37+
#### `templateOptions`
38+
39+
- Type: `object | undefined`
40+
- Description: Options passed to the template renderer
41+
- [Default template renderer options reference](https://liquidjs.com/api/interfaces/liquid_options_.liquidoptions.html)
42+
43+
#### `pdfOptions`
44+
45+
- Type: `object | undefined`
46+
- Description: Options passed to the PDF renderer
47+
- [PDF renderer options reference](https://pptr.dev/api/puppeteer.pdfoptions)
48+
49+
#### `getTemplateRenderer`
50+
51+
- Type: `function | undefined`
52+
- Description: Returns a custom template renderer using the options passed from `templateOptions`
53+
- Signature: `(options?) => (template: string, data?: object) => string | Promise<string>`
54+
55+
### Examples
56+
57+
**Tip:** If PDF Made Easy is installed locally or a global install is [`npm link`ed](https://docs.npmjs.com/cli/v9/commands/npm-link), you can add a [JSDoc](https://jsdoc.app) `@type` comment for [IntelliSense](https://en.wikipedia.org/wiki/Intelligent_code_completion) like in the examples below.
58+
59+
#### Passing options to the template and PDF renderers
3960

4061
```js
4162
/** @type {import("pdf-made-easy").PMEConfig} */
4263
export default {
4364
templateOptions: {
44-
// ...
65+
jsTruthy: true,
66+
globals: {
67+
custom: "global variables here"
68+
}
4569
},
4670
pdfOptions: {
47-
// ...
71+
format: "LEGAL",
72+
landscape: true,
73+
margin: {
74+
top: "0.5in",
75+
bottom: "0.5in",
76+
left: "1in",
77+
right: "1in"
78+
}
4879
}
4980
};
5081
```
5182

52-
The [JSDoc](https://jsdoc.app) `@type` comment helps to provide [code completion](https://en.wikipedia.org/wiki/Intelligent_code_completion) when using editors that support loading [declaration files](https://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html).
83+
#### Using a custom template renderer
84+
85+
```js
86+
import Handlebars from "handlebars";
87+
88+
/**
89+
* @type {import("pdf-made-easy").PMEConfig<Parameters<Handlebars.compile>[1]>}
90+
*/
91+
export default {
92+
// 'templateOptions' will be passed to the 'getTemplateRenderer' method below
93+
// as 'options'.
94+
templateOptions: {
95+
noEscape: true,
96+
strict: true
97+
},
98+
getTemplateRenderer(options) {
99+
return (template, data) => Handlebars.compile(template, options)(data);
100+
}
101+
};
102+
```

docs/02-api.md

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,74 +2,87 @@
22

33
**Note:** PDF Made Easy is [ECMAScript modules only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c), meaning, it can only be `import`ed, not `require`d.
44

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.
66

77
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:
88

99
```js
1010
import Handlebars from "handlebars";
11-
import { getBuilder, getDefaultTemplateRenderer } from "pdf-made-easy";
11+
import { getBuilder } from "pdf-made-easy";
1212

1313
// Instantiates the 'Builder' object and resources needed for PDF generation.
1414
const builder = await getBuilder();
1515

1616
// 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.
2119
await builder.build({
2220
data: "data.yml",
2321
template: "template.liquid",
24-
output: "output.pdf",
25-
getTemplateRenderer: getDefaultTemplateRenderer
22+
output: "output.pdf"
2623
});
2724

2825
// Use a JSON data file.
2926
await builder.build({
3027
data: "data.json",
3128
template: "template.liquid",
32-
output: "output.pdf",
33-
getTemplateRenderer: getDefaultTemplateRenderer
29+
output: "output.pdf"
3430
});
3531

3632
// Resolve relative file paths in a custom directory.
3733
await builder.build(
3834
{
3935
data: "data.yml",
4036
template: "template.liquid",
41-
output: "output.pdf",
42-
getTemplateRenderer: getDefaultTemplateRenderer
37+
output: "output.pdf"
4338
},
4439
"/my/custom/root/directory/where/relative/paths/will/be/resolved/in"
4540
);
4641

47-
// Use Handlebars as the templating engine.
42+
// Pass options to the templating engine and the PDF renderer.
4843
await builder.build({
4944
data: "data.yml",
50-
template: "template.hbs",
45+
template: "template.liquid",
5146
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+
}
5464
}
5565
});
5666

57-
// Pass options to the templating engine and the PDF renderer.
67+
// Use Handlebars as the templating engine.
5868
await builder.build({
5969
data: "data.yml",
60-
template: "template.liquid",
70+
template: "template.hbs",
6171
output: "output.pdf",
62-
getTemplateRenderer: getDefaultTemplateRenderer,
6372
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]} */
6476
templateOptions: {
65-
// ...
77+
noEscape: true,
78+
strict: true
6679
},
67-
pdfOptions: {
68-
// ...
80+
getTemplateRenderer(options) {
81+
return (template, data) => Handlebars.compile(template, options)(data);
6982
}
7083
}
7184
});
7285

73-
// Dispose all instatiated resouces so the script can exit.
86+
// Dispose all instantiated resouces so the script can exit.
7487
await builder.close();
7588
```

0 commit comments

Comments
 (0)