This repository contains the source code for Letters Generation (plain text or PDF) from templates
You can install this tools via yarn
:
yarn add letter-generator
You can use a template to generate your text with variables and flags
// The text contains the static text, flags and variables
// The flags should follow this form: [flag_name>The text contained in the flag]
// The variables should follow this form: {variable_name}
const templateText = "Here is a wonderful template [flag_1>I don't want this to be printed out] \n Checkout the {webpage} website! \n[flag_2>I want this to be printed out]";
// You should map the flag_name used in the template text with their visibilities (Boolean)
const flags = { flag_1: false, flag_2: true };
// You should map the variable_name used in the template text with their values
const variables = {webpage: 'datenanfragen.de'};
// Create the template from our values and generate the output text as string
const template = new Template(templateText, flags, variables);
const content = template.getText();
You'll have to pass an object to the letter constructor containing those informations:
const props = {
// The sender address, either as a string or as an array of the individual lines.
sender_address: ['Jane Doe', '123 Some Lane', '12345 Some City', 'Some Country'],
// The recipient address, individual or array
recipient_address: ['John Doe', '667 One Street', '98765 Another City', 'A Country'],
// The information block usually displayed at the top-right of the letter. Can either be a string, a string[] or anything recognized by pdfmake.
information_block: 'A block of information',
// The Letter subject
// In this case we use the text generated with the template but you can use a string
subject: content,
// The content of the letter. Can either be a string or anything recognized by pdfmake.
content: 'Content of my letter',
// OPTIONAL
// The signature to be included after the content.
signature: { type: 'text', name: 'Name' } // To just add the name
//{ type: 'image', name: 'Name', value: '<BASE64 ENCODED IMAGE>'} to include an image with the name underneath
}
You can now create the letter object and use it:
The default layout is the DIN 5008-a layout
// Use default layout
const letter = new Letter(props);
// You can also give the letter a function that returns a pdfmake layout as second parameter:
const letter_with_layout = new Letter(props, layout_function);
Once your Letter is generated you can create a PdfRender from it:
const render = new PdfRenderer(letter);
render.triggerOpenInNewWindow();
render.triggerDownload();
First of all, thank you very much for taking the time to contribute! Contributions are incredibly valuable for a project like ours.
We warmly welcome issues and pull requests through GitHub.
Please be aware that by contributing, you agree for your work to be released under the MIT license, as specified in the LICENSE file.
If you are interested in contributing in other ways besides coding, we can also really use your help. Have a look at our contribute page for more details.