Skip to content

Pure docx library for replacing Office Word (.docx), Excel (.xlsx) and Powerpoint (.pptx) documents

License

Notifications You must be signed in to change notification settings

mllx87/puredocx

Repository files navigation

PureDocx - Template for Js

Current tag

English | 简体中文

PureDocx is a library that generates docx/xlsx/pptx documents from docx/xlsx/pptx templates. Based on the secondary development of docxtemplater, it can replace {placeholders} with data, and also supports loops, conditions, adding or replacing images, charts, watermarks, etc.

Word Examples

Powerpoint Examples

Multiple examples are provided in our example repository https://github.com/mllx87/puredocx-examples.

Contact us if you need any specific examples.

Modules

The following functions can be added through the paid module puredocx-pro-module :

  • Image function to add an image and replace an existing image with the syntax: {%image};
  • Html function to insert formatted text in a docx document with the syntax {~html};
  • XLSX function to be able to do templating on Excel files (xlsx extension), also with loops and conditions;
  • Chart function to add a chart by using data from the JSON object that you give with the syntax {$chart};
  • Watermark function to replace a given Watermark with the syntax: {watermark};
  • QrCode function to add a qrcode with the syntax {&qrcode};

Installation

Puredocx NPM Home

With modules:

npm install --save @mllx87/puredocx

Usage

First, download the input.docx file and place it in the same folder as your JavaScript file.

const Puredocx = require("@mllx87/puredocx");

var puredocx = new Puredocx()

var substitutions = {
    "title":"hello",
    "summary":"New World",
    "staff":30,
    "company":"mllx"
};

puredocx.renderAsync("input.docx",substitutions,"output.docx")

Images,Assuming your docx or pptx template contains only the text {%image}

const Puredocx = require("@mllx87/puredocx");

var puredocx = new Puredocx()

var substitutions = {
    "title":"hello",
    "summary":"New World",
    "staff":30,
    "company":"mllx",
    "image": "99e36.jpeg"
};

puredocx.renderAsync("input.docx",substitutions,"output.docx")

Some notes regarding templates:

  • docx files: the placeholder {%image} must be in a dedicated paragraph.
  • pptx files: the placeholder {%image} must be in a dedicated text cell.

Centering images


You can center all images by setting the global switch to true opts.imageOptions.centered = true.

If you would like to choose which images should be centered one by one:

  • Set the global switch to false opts.imageOptions.centered = false.
  • Use {%image} for images that shouldn't be centered.
  • Use {%%image} for images that you would like to see centered.

In pptx generated documents, images are centered vertically and horizontally relative to the parent cell.

Supported image format in given data :

You can pass imageRootPath option for setting the root folder for your images.

    var puredocx = new Puredocx({
    "imageOptions": {
        imageRootPath: "/path/to/your/image/dir",
        getSize() { return [350, 300] }
    }
})

For more options see:PureDocx