pdfmake-markup-nunjucks
v1.0.0-beta
Published
Render .pdfmk.njk templates into pure pdfmake-markup using Nunjucks.
Downloads
7
Maintainers
Readme
pdfmake-markup-nunjucks
Render Nunjucks templates with a .pdfmk.njk extension into pure pdfmake-markup strings.
This package is intentionally small: it handles the templating pass only. The output is plain .pdfmk, which you can then pass to pdfmake-markup yourself.
It also tries hard not to get in Nunjucks' way. If you already have your own Environment, custom loaders, extensions, globals, filters, or custom tag syntax, you can keep using them by passing that environment into render.
Install
npm install pdfmake-markup-nunjucks nunjucksFor local development in this repo, pdfmake-markup is kept in devDependencies for tests and the sample runner.
Usage
const nunjucks = require('nunjucks');
const { render } = require('pdfmake-markup-nunjucks');
const { decode } = require('pdfmake-markup');
const template = `
{% set rows = [
{ name: "Alpha", value: 10 },
{ name: "Beta", value: 20 }
] %}
{
content: [
{ text: "Invoice", bold: true, fontSize: 18 },
...map(row in %{ {{ rows | json }} }%) {
{ text: %{row.name + ": " + row.value}% }
}
]
}
`;
const markup = render(template);
const docDefinition = decode(markup);Using your own Nunjucks environment:
const env = new nunjucks.Environment(null, {
autoescape: false,
tags: {
variableStart: '[[',
variableEnd: ']]',
},
});
env.addGlobal('companyName', 'ACME');
const markup = render('{ title: [[ companyName | json ]] }', {}, { env });API
render(template: string, context?: object, options?: RenderOptions): string
Renders a .pdfmk.njk template string into pure .pdfmk.
createEnvironment(options?: EnvironmentOptions): nunjucks.Environment
Creates a Nunjucks environment and passes standard Nunjucks options through. By default it sets autoescape: false unless you override it, and adds the built-in json / literal filters.
Built-in filter
json
Serializes a value into a pdfmake-markup-friendly literal so it can be embedded safely into .pdfmk expressions.
literal
Alias of json.
{% set rows = [{ "name": "Alpha" }, { "name": "Beta" }] %}
...map(row in %{ {{ rows | json }} }%) {
{ text: %{row.name}% }
}Render Options
interface EnvironmentOptions {
loader?: nunjucks.ILoaderAny | nunjucks.ILoaderAny[] | null;
loaderOptions?: nunjucks.FileSystemLoaderOptions;
searchPaths?: string | string[];
installBuiltins?: boolean;
overwriteBuiltins?: boolean;
configureEnvironment?: (env: nunjucks.Environment) => void;
}
interface RenderOptions extends EnvironmentOptions {
env?: nunjucks.Environment;
}If you want to render a file, read it yourself and pass the contents to render(). For relative {% include %} resolution, provide searchPaths or your own configured environment.
const fs = require('fs');
const path = require('path');
const { render } = require('pdfmake-markup-nunjucks');
const filePath = path.join(__dirname, 'invoice.pdfmk.njk');
const template = fs.readFileSync(filePath, 'utf8');
const markup = render(template, data, {
searchPaths: path.dirname(filePath),
});Samples
The repo ships the upstream pdfmake-markup samples under samples/*.pdfmk.njk, adjusted to use Nunjucks variables where that helps demonstrate the templating layer.
You can generate PDFs with:
npm run sample -- basics
npm run sample:allLicense
MIT
