npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@universis/templates

v1.0.1-dev.1

Published

Universis API extension for managing presentation documents and reports

Downloads

1

Readme

universis-templates

Universis Api Server extension for managing report templates. @universis/templates uses @themost/web Angular JS for server applications template to render documents and reports.

Editor

The following code snippet uses a simple template to render a document based on the data specified.

    // get template service
    const templateService = context.getApplication().getService(DocumentTemplateService);
    // get template path
    const templatePath = path.resolve(__dirname, 'files/HelloTemplate.docx');
    // render template
    const buffer = await templateService.render(context, templatePath, {
        department: {
            name: 'Computer Science',
            organization: {
                name: 'Educational Institute'
            }
        },
        message: 'Hello World'
    });
    // save file
    const outFile = path.resolve(__dirname, '.tmp/HelloTemplate.docx');
    await new Promise((resolve, reject) => {
        fs.writeFile(outFile, buffer, (err)=> {
           if (err) {
               return reject(err);
           }
           return resolve();
        });
    });

The result will be a document like this:

Render document

Export document

Use @universis/converter#DocumentConverterService to convert a document to pdf.

// get template service
const templateService = context.getApplication().getService(DocumentTemplateService);
/**
 * get DocumentConverterService service
 * @type {DocumentConverterService}
 */
const converterService = context.getApplication().getService(DocumentConverterService);
// get template path
const templatePath = path.resolve(__dirname, 'files/HelloTemplate.docx');
// render template
const buffer = await templateService.render(context, templatePath, {
    department: {
        name: 'Computer Science',
        organization: {
            name: 'Educational Institute'
        }
    },
    message: 'Hello World'
});
// save file
const docFile = path.resolve(__dirname, '.tmp/HelloTemplate.docx');
await new Promise((resolve, reject) => {
    fs.writeFile(docFile, buffer, (err)=> {
        if (err) {
            return reject(err);
        }
        converterService.convertFile(docFile, docFile.replace(/.docx$/ig, '.pdf')).then(()=> {
            return resolve();
        }).catch( err => {
            return reject(err);
        });

    });
});

and the result will be a pdf document:

Export document

Usage

Install @universis/templates and @universis/converter modules

npm i @universis/templates @universis/converter

Install LibreOffice or use docker engine to install it as docker container. LibreOffice - a powerful open source office suite- and its tools are used by DocumentConverterService in order to convert docx to pdf documents.

add DocumentTemplateService to @universis/api services section of application configuration:

# server/config/app.json

{
    "services": [
        ...
        { "serviceType": "@universis/templates#DocumentTemplateService" }
    ]
...
}

add DocumentTemplateLoader to @universis/api services section of application configuration:

# server/config/app.json

{
    "services": [
        ...
        { "serviceType": "@universis/templates#DocumentTemplateLoader" }
    ]
...
}
Use Google Drive REST API

If your intended to use GoogleDriveTemplateLoader to load templates using Google Drive REST API, register GoogleDriveTemplateLoader:

# server/config/app.json
    
    {
        "services": [
            ...
            { 
                "stategyType": "@universis/templates#DocumentTemplateLoader",
                "serviceType": "@universis/templates#GoogleDriveTemplateLoader"
            }
        ]
    ...
    }
    

Follow OAuth 2.0 for Server to Server Applications instructions for generating a service account for google drive api and set account credentials to application configuration:

"settings": {
...
    "universis": {
            "converter": {
                "command": "libreoffice"
            },
            "templates": {
                "drive": {
                    "credentials": {
                        "type": "service_account",
                        "project_id": "project",
                        "private_key_id": "...",
                        "private_key": "...",
                        "client_email": "...",
                        "client_id": "...",
                        "auth_uri": "https://accounts.google.com/o/oauth2/auth",
                        "token_uri": "https://oauth2.googleapis.com/token",
                        "auth_provider_x509_cert_url": "...",
                        "client_x509_cert_url": "..."
                    }
                }
            }
        }
    }
 ...

and render documents downloaded from google drive:

// get template service
const templateService = context.getApplication().getService(DocumentTemplateService);
/**
 * get DocumentConverterService service
 * @type {DocumentConverterService}
 */
const converterService = context.getApplication().getService(DocumentConverterService);
// get google drive document by name
const templatePath = 'HelloTemplate.docx';
// render template
const buffer = await templateService.render(context, templatePath, {
    department: {
        name: 'Computer Science',
        organization: {
            name: 'Educational Institute'
        }
    },
    message: 'Hello World'
});
// save file
const docFile = path.resolve(__dirname, '.tmp/HelloTemplate.docx');
await new Promise((resolve, reject) => {
    fs.writeFile(docFile, buffer, (err)=> {
        if (err) {
            return reject(err);
        }
        converterService.convertFile(docFile, docFile.replace(/.docx$/ig, '.pdf')).then(()=> {
            return resolve();
        }).catch( err => {
            return reject(err);
        });

    });
});
Use document converter

Finally add DocumentConverterService to @universis/api services section of application configuration:

# server/config/app.json

{
    "services": [
        ...
        { "serviceType": "@universis/converter#DocumentConverterService" }
    ]
...
}

and start using document templates for creating Universis reports.