GithubHelp home page GithubHelp logo

charon01 / open-docxtemplater-image-module Goto Github PK

View Code? Open in Web Editor NEW

This project forked from maxrcd/open-docxtemplater-image-module

0.0 0.0 0.0 1.17 MB

Open Source Image Module for docxtemplater

License: Other

JavaScript 100.00%

open-docxtemplater-image-module's Introduction

Open source docxtemplater image module

This package is open source. There is also a paid version maintained by docxtemplater author.

Note this version is compatible until docxtemplater 3.5.2. There are good forks of this repository.

Installation

If not already done, install docxtemplater by following its installation guide.

Node.js: after installing docxtemplater, install the open source image module:

npm install open-docxtemplater-image-module

For the browser find builds in build/ directory.

Alternatively, you can create your own build from the sources:

npm run compile
npm run browserify
npm run uglify

Usage

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

//Node.js example
var ImageModule = require('open-docxtemplater-image-module');

//Below the options that will be passed to ImageModule instance
var opts = {}
opts.centered = false; //Set to true to always center images
opts.fileType = "docx"; //Or pptx

//Pass your image loader
opts.getImage = function(tagValue, tagName) {
    //tagValue is 'examples/image.png'
    //tagName is 'image'
    return fs.readFileSync(tagValue);
}

//Pass the function that return image size
opts.getSize = function(img, tagValue, tagName) {
    //img is the image returned by opts.getImage()
    //tagValue is 'examples/image.png'
    //tagName is 'image'
    //tip: you can use node module 'image-size' here
    return [150, 150];
}

var imageModule = new ImageModule(opts);

var zip = new JSZip(content);
var doc = new Docxtemplater()
    .attachModule(imageModule)
    .loadZip(zip)
    .setData({image: 'examples/image.png'})
    .render();

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

fs.writeFile("test.docx",buffer);

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.centered = true.

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

  • Set the global switch to false opts.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.

open-docxtemplater-image-module's People

Contributors

dependabot[bot] avatar edi9999 avatar maxrcd avatar netei avatar nrekretep avatar ofkrown avatar takeno avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.