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

@bposman/docxtemplater

v2.1.6

Published

.docx generator working with templates and data (like Mustache)

Downloads

23

Readme

docxtemplater

Join the chat at https://gitter.im/open-xml-templating/docxtemplater

Build Status Download count Current tag Issues closed

docxtemplater logo

docxtemplater is a library to generate docx/pptx documents from a docx/pptx template. It can replace {placeholders} with data and also supports loops and conditions. The templates can be edited by non-programmers, eg for example your clients.

Note: The CLI will soon be moved to another repository : keep posted on https://github.com/open-xml-templating/docxtemplater-cli

Features

Demo Site

  • Replace a {placeholder} by a value
  • Use loops: {#users} {name} {/users}
  • Use loops in tables to generate columns
  • Use expressions {product.unit_price*product.count} with angular Parsing
  • Insert custom XML {@rawXml} (for formatted text for example)

Quickstart in Node

Installation: npm install docxtemplater

var fs = require('fs');
var Docxtemplater = require('docxtemplater');

//Load the docx file as a binary
var content = fs
    .readFileSync(__dirname + "/input.docx", "binary");

var doc = new Docxtemplater(content);

//set the templateVariables
doc.setData({
    "first_name":"Hipp",
    "last_name":"Edgar",
    "phone":"0652455478",
    "description":"New Website"
});

//apply them (replace all occurences of {first_name} by Hipp, ...)
doc.render();

var buf = doc.getZip()
             .generate({type:"nodebuffer"});

fs.writeFileSync(__dirname+"/output.docx",buf);

You can download input.docx and put it in the same folder than your script.

Quickstart in the browser

Installation

JS download

You can directly download built versions from github : https://github.com/open-xml-templating/docxtemplater-build/tree/master/build

If you download the JS from there, you should use new Docxgen() instead of new Docxtemplater(), because I do not want to bring in a breaking change on a minor version change in the docxtemplater-build repository.

Bower

bower install --save docxtemplater

If you download the JS from there, you should use new Docxgen() instead of new Docxtemplater(), because I do not want to bring in a breaking change on a minor version change in the docxtemplater-build repository.

Build it yourself

I recommend you to use the npm scripts I wrote (which can be found in the package.json).

git clone [email protected]:edi9999/docxtemplater.git && cd docxtemplater
# git checkout v2.0.0 # Optional
npm install
npm run compile
# Optionally :
# npm run browserify
# npm run uglify:lib

Docxtemplater will be exported to window.Docxtemplater for easy usage (on some systems, it might export it in window.docxtemplater (see https://github.com/edi9999/docxtemplater/issues/118))

Your version of docxtemplater will be in /build (minified and non minified options) and already include all dependencies

Html demo

Create the following html

<html>
    <script src="build/docxtemplater.js"></script>
    <script src="vendor/FileSaver.min.js"></script>
    <script src="vendor/jszip-utils.js"></script>
    <!--
    Mandatory in IE 6, 7, 8 and 9.
    -->
    <!--[if IE]>
        <script type="text/javascript" src="examples/vendor/jszip-utils-ie.js"></script>
    <![endif]-->
    <script>
    var loadFile=function(url,callback){
        JSZipUtils.getBinaryContent(url,callback);
    }
    loadFile("examples/tagExample.docx",function(err,content){
        if (err) { throw e};
        doc=new Docxtemplater(content);
        doc.setData( {"first_name":"Hipp",
            "last_name":"Edgar",
            "phone":"0652455478",
            "description":"New Website"
            }
        ) //set the templateVariables
        doc.render() //apply them (replace all occurences of {first_name} by Hipp, ...)
        out=doc.getZip().generate({type:"blob"}) //Output the document using Data-URI
        saveAs(out,"output.docx")
    })
    </script>
</html>

Documentation

The full documentation of the latest version can be found on read the docs.

See CHANGELOG.md for information about how to migrate from older versions.

Similar libraries

They are a few similar libraries that work with docx, here’s a list of those I know a bit about:

  • docx4j :JAVA, this is probably the biggest docx library out there. They is no built in templating engine, but you can generate your docx yourself programmatically
  • docx.js: Javascript in the browser, you can create (not modify) your docx from scratch, but only do very simple things such as adding non formatted text
  • xlsx-templater : its working quite well, does the same as here but for xlsx

Modules

Functionality can be added with modules. They is yet no doc for the modules because it is not completely mature yet, but you can open an issue if you have any question about it.

Here is the list of existing modules:

  • Chart Module using the syntax: {$chart} , user contributed https://github.com/prog666/docxtemplater-chart-module
  • Image module using the syntax: {%image}, https://github.com/open-xml-templating/docxtemplater-image-module
  • Hyperlink module using the syntax: {^link}, https://github.com/sujith3g/docxtemplater-link-module

Professional Support

I can give your company support for installing, extending, answering support questions, or maintainning your app that runs docxtemplater. You can find my email address on my profile