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

docx-reporting

v2.2.3

Published

Render a docx file from a template

Downloads

206

Readme

docx-reporting

NPM module to render a docx file from a template.

The template docx file support a template syntax for text insertion, conditionals, loops and images.

Installation

yarn add docx-reporting

Usage

import * as docx from "docx-reporting";

const template = fs.readFileSync(`template.docx`);
const data = {
  text: "Hello, World!",
};

docx.generate(template, data).then(rendered => {
  fs.writeFileSync(`rendered.docx`, rendered);
});

Template Syntax

| Template | Description | | --------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | {value} | Simple placeholder. {value} will be substituted with the real value. | | {#items}...{/items} | Loop placeholder. The content within the loop scope, indicated by "...", will be repeated for as many items there are. See "Loops" below for further details | | {?value}...{/value} | Condition placeholder. The content within the condition scope, indicated by "...", will be rendered if value is not null, undefined or false. Otherwise the whole construct is removed. | | {!image}🖼️{/image} | Image placeholder. The image within, indicated by 🖼️, will be replaced with a provided image. See "Images" below for further details |

Loops

Each time the loop is rendered, the scope is set to the current array element.

Example:

Template:

{#fruits}
{name}
{price} EUR
{/fruits}

Data:

{
  fruits: [{ name: "Apple", price: 1.0 }, { name: "Orange", price: 1.8 }];
}

To access variables outside the element scope, $parent can be used.

Example:

Template:

{#fruits}
{name}
{price} {$parent.currency}
{/fruits}

Data:

{
  "currency": "EUR",
  "fruits": [
    { "name": "Apple", "price": 1.0 },
    { "name": "Orange", "price": 1.8 }
  ]
}

Additionaly a set of special variables are provided for additional loop control

| Variable | Type | Description | | ----------- | --------- | --------------------------------------------------------------------------- | | $index | number | iterator offset of the repeated element (0..length-1) | | $first | boolean | true if the repeated element is first in the iterator. | | $notFirst | boolean | true if the repeated element is not first in the iterator. | | $middle | boolean | true if the repeated element is between the first and last in the iterator. | | $last | boolean | true if the repeated element is last in the iterator. | | $notLast | boolean | true if the repeated element is not last in the iterator. | | $even | boolean | true if the iterator position $index is even | | $odd | boolean | true if the iterator position $index is odd |

Images

The template need to include an image that will be repalced during rendering. You can use this placeholder image to set the correct size and position.

Example:

Template:

{?image}
🖼️
{/image}

Data:

{
  "image": {
    "name": "image.jpg"
  }
}

The actual image data must be passed as an ArrayBuffer or Buffer as the third parameter to the docx.generate function.

import * as docx from "docx-reporting";

const template = fs.readFileSync(`template.docx`);
const image = fs.readFileSync(`image.jpg`);
const data = {
  image: { name: "image.jpg" },
};
const media = {
  "image.jpg": image,
};

docx.generate(template, data, media);

If you do not want your image to keep the size set in the template you can provide a size in pixels in the image object:

{
  "image": {
    "name": "image.jpg",
    "size": {
      "width": 500,
      "height": 300
    }
  }
}