2.2.2 • Published 3 years ago

@bcgov/carbone-render v2.2.2

Weekly downloads
1
License
Apache-2.0
Repository
github
Last release
3 years ago

Deprecated carbone-render

Note: This package has been deprecated and will no longer be receiving support updates.

npm downloads License img

Library to generate a document from an existing template and JSON data. This is a wrapper around carbone, please refer to their documentation for more detail.

Prerequisites

This library will require LibreOffice installed to do pdf generation.

Installation

npm i @bcgov/carbone-render

Usage

const carboneRenderer = require('@bcgov/carbone-render');

startFactory

Optional call to start up the carbone python converter. This is recommended to call on the start of your application. Otherwise, the first call to render will take longer as it starts the converter in the background.

carboneRenderer.startFactory();

fileTypes

Return a dictionary of allowable file conversions. Convert to pdf is always allowed.

const allowedConversions = carboneRenderer.fileTypes;

Returns dictionary

{
    "csv": [
        "doc",
        "docx",
        "html",
        "odt",
        "pdf",
        "rtf",
        "txt",
        "csv"
    ],
    "docx": [
        "doc",
        "docx",
        "html",
        "odt",
        "pdf",
        "rtf",
        "txt"
    ],
    "html": [
        "html",
        "odt",
        "pdf",
        "rtf",
        "txt"
    ],
    "odt": [
        "doc",
        "docx",
        "html",
        "odt",
        "pdf",
        "rtf",
        "txt"
    ],
    "pptx": [
        "odt",
        "pdf"
    ],
    "rtf": [
        "docx",
        "pdf"
    ],
    "txt": [
        "doc",
        "docx",
        "html",
        "odt",
        "pdf",
        "rtf",
        "txt"
    ],
    "xlsx": [
        "odt",
        "pdf",
        "rtf",
        "txt",
        "csv",
        "xls",
        "xlsx"
    ]
}

async render

Generate a new document from a template and JSON data. Render the report.

const data = {
    "firstName": "Jane",
    "lastName": "Smith",
    "title": "Who?"
};

const options = {
    "convertTo": "pdf",
    "reportName": "{d.firstName}-{d.lastName}.docx",
    "overwrite": "true",
    "cacheReport": "true"
};

const formatters = {
    // this formatter can be used in a template with {d.myBoolean:yesOrNo()}
    yesOrNo : function (data) { // data = d.myBoolean
        if (this.lang === 'fr') {
            return data === true ? 'oui' : 'non';
        }
        return data === true ? 'yes' : 'no';
    }
};

const output = await carboneRenderer.render(template.path, data, options, formatters);
ParametersDescription
templatetemplate file
dataobject or array of objects used in the template
optionsOptions object to pass to carbone. See carbone documenation.
options.convertToWhat file type to convert to, see fileTypes or pdf
options.reportNameName of the generated report, should include expected extension. Can contain template placeholders, see carbone documentation.
formattersObject. Additional formatters to add to carbone. See carbone documenation.

Returns object

{
    "success": false,
    "errorType": null,
    "errorMsg": null,
    "reportName": null,
    "report": null
}
FieldDescription
successboolean, true indicates report generated, false otherwise
errorTypenumber - error number if not successful.
errorMsgstring - error message if not successful.
reportNamestring - name of the generated report
reportbinary - generated file
2.2.2

3 years ago

2.2.1

3 years ago

2.1.2

3 years ago

2.2.0

3 years ago

2.1.1

4 years ago

2.1.0

4 years ago

2.0.0

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago