🚨 Shai-Hulud Strikes Again:834 Packages Compromised.Technical Analysis →
Socket
Book a DemoInstallSign in
Socket

@bcgov/carbone-render

Package Overview
Dependencies
Maintainers
7
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@bcgov/carbone-render

Async wrapper around Carbone render

Source
npmnpm
Version
2.0.0
Version published
Weekly downloads
70
-28.57%
Maintainers
7
Weekly downloads
 
Created
Source

carbone-render

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.

important

This library will require LibreOffice installed. It requires LibreOffice to do pdf generation.

usage

install/initialize

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

startFactory

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

carboneRenderer.startFactory();

fileTypes

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

const allowedConversions = carboneRenderer.fileTypes;
returns object
{
    "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

Keywords

carbone

FAQs

Package last updated on 01 Oct 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts