{"versions":{"1.1.1":{"name":"docxtemplater-image-module-free","version":"1.1.1","description":"Open Source Image Module for docxtemplater","main":"js/index.js","scripts":{"test:coverage":"istanbul cover _mocha --  es6/test.js","compile":"rimraf js && mkdirp js && babel es6 --out-dir js","preversion":"npm test && npm run browserify && npm run uglify","test:compiled":"mocha js/test.js","test:es6":"mocha es6/test.js","lint":"eslint .","test":"npm run compile && npm run test:compiled","browserify":"rimraf build && mkdirp build && browserify --insert-global-vars __filename,__dirname -r ./js/index.js -s ImageModule > build/imagemodule.js","uglify":"uglifyjs build/imagemodule.js > build/imagemodule.min.js"},"devDependencies":{"babel-cli":"^6.11.4","babel-eslint":"^7.1.1","babel-preset-es2015":"^6.3.13","browserify":"^14.0.0","chai":"^3.4.1","docxtemplater":"^3.0.0","eslint":"^3.15.0","image-size":"^0.5.1","istanbul":"^0.4.5","jszip":"^2.6.1","mkdirp":"^0.5.1","mocha":"^3.2.0","rimraf":"^2.5.4","uglifyjs":"^2.4.10"},"repository":{"type":"git","url":"git+https://github.com/evilc0des/docxtemplater-image-module-free.git"},"author":{"name":"Dk Saha"},"license":"MIT","dependencies":{"xmldom":"^0.1.27"},"gitHead":"3c18ebc13d544238df676dd6be05bd4a1a8a21a9","bugs":{"url":"https://github.com/evilc0des/docxtemplater-image-module-free/issues"},"homepage":"https://github.com/evilc0des/docxtemplater-image-module-free#readme","_id":"docxtemplater-image-module-free@1.1.1","_npmVersion":"6.4.1","_nodeVersion":"11.3.0","_npmUser":{"name":"anonymous","email":"dhrtklpsh82@gmail.com"},"dist":{"shasum":"a7f12125c791624759c4d468412f8cf44c539392","size":66887,"noattachment":false,"tarball":"http://123.232.10.234:8212/nexus/content/groups/npm-public/docxtemplater-image-module-free/-/docxtemplater-image-module-free-1.1.1.tgz"},"maintainers":[{"name":"anonymous","email":"dhrtklpsh82@gmail.com"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/docxtemplater-image-module-free_1.1.1_1548523989074_0.7356116054247488"},"_hasShrinkwrap":false,"publish_time":1548523989220,"_cnpm_publish_time":1548523989220,"contributors":[]},"1.1.0":{"name":"docxtemplater-image-module-free","version":"1.1.0","description":"Open Source Image Module for docxtemplater","main":"js/index.js","scripts":{"test:coverage":"istanbul cover _mocha --  es6/test.js","compile":"rimraf js && mkdirp js && babel es6 --out-dir js","preversion":"npm test && npm run browserify && npm run uglify","test:compiled":"mocha js/test.js","test:es6":"mocha es6/test.js","lint":"eslint .","test":"npm run compile && npm run test:compiled","browserify":"rimraf build && mkdirp build && browserify --insert-global-vars __filename,__dirname -r ./js/index.js -s ImageModule > build/imagemodule.js","uglify":"uglifyjs build/imagemodule.js > build/imagemodule.min.js"},"devDependencies":{"babel-cli":"^6.11.4","babel-eslint":"^7.1.1","babel-preset-es2015":"^6.3.13","browserify":"^14.0.0","chai":"^3.4.1","docxtemplater":"^3.0.0","eslint":"^3.15.0","image-size":"^0.5.1","istanbul":"^0.4.5","jszip":"^2.6.1","mkdirp":"^0.5.1","mocha":"^3.2.0","rimraf":"^2.5.4","uglifyjs":"^2.4.10"},"repository":{"type":"git","url":"git+https://github.com/evilc0des/docxtemplater-image-module-free.git"},"author":{"name":"Dk Saha"},"license":"MIT","dependencies":{"xmldom":"^0.1.27"},"gitHead":"6a79b682726247c4b4368ecc4553e33a54692bb2","bugs":{"url":"https://github.com/evilc0des/docxtemplater-image-module-free/issues"},"homepage":"https://github.com/evilc0des/docxtemplater-image-module-free#readme","_id":"docxtemplater-image-module-free@1.1.0","_npmVersion":"6.4.1","_nodeVersion":"11.3.0","_npmUser":{"name":"anonymous","email":"dhrtklpsh82@gmail.com"},"dist":{"shasum":"a7df0a574ddaa8d0321418207a67c8cc7b98d56b","size":65477,"noattachment":false,"tarball":"http://123.232.10.234:8212/nexus/content/groups/npm-public/docxtemplater-image-module-free/-/docxtemplater-image-module-free-1.1.0.tgz"},"maintainers":[{"name":"anonymous","email":"dhrtklpsh82@gmail.com"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/docxtemplater-image-module-free_1.1.0_1548348900329_0.5217106966898211"},"_hasShrinkwrap":false,"publish_time":1548348900453,"_cnpm_publish_time":1548348900453,"contributors":[]},"1.0.1":{"name":"docxtemplater-image-module-free","version":"1.0.1","description":"Open Source Image Module for docxtemplater","main":"js/index.js","scripts":{"test:coverage":"istanbul cover _mocha --  es6/test.js","compile":"rimraf js && mkdirp js && babel es6 --out-dir js","preversion":"npm test && npm run browserify && npm run uglify","test:compiled":"mocha js/test.js","test:es6":"mocha es6/test.js","lint":"eslint .","test":"npm run compile && npm run test:compiled","browserify":"rimraf build && mkdirp build && browserify --insert-global-vars __filename,__dirname -r ./js/index.js -s ImageModule > build/imagemodule.js","uglify":"uglifyjs build/imagemodule.js > build/imagemodule.min.js"},"devDependencies":{"babel-cli":"^6.11.4","babel-eslint":"^7.1.1","babel-preset-es2015":"^6.3.13","browserify":"^14.0.0","chai":"^3.4.1","docxtemplater":"^3.0.0","eslint":"^3.15.0","image-size":"^0.5.1","istanbul":"^0.4.5","jszip":"^2.6.1","mkdirp":"^0.5.1","mocha":"^3.2.0","rimraf":"^2.5.4","uglifyjs":"^2.4.10"},"repository":{"type":"git","url":"git+https://github.com/evilc0des/docxtemplater-image-module-free.git"},"author":{"name":"Dk Saha"},"license":"MIT","dependencies":{"xmldom":"^0.1.27"},"gitHead":"6a79b682726247c4b4368ecc4553e33a54692bb2","bugs":{"url":"https://github.com/evilc0des/docxtemplater-image-module-free/issues"},"homepage":"https://github.com/evilc0des/docxtemplater-image-module-free#readme","_id":"docxtemplater-image-module-free@1.0.1","_npmVersion":"6.4.1","_nodeVersion":"11.3.0","_npmUser":{"name":"anonymous","email":"dhrtklpsh82@gmail.com"},"dist":{"shasum":"ff23f216612f4fc46647e9d6dafbac629762ed85","size":65477,"noattachment":false,"tarball":"http://123.232.10.234:8212/nexus/content/groups/npm-public/docxtemplater-image-module-free/-/docxtemplater-image-module-free-1.0.1.tgz"},"maintainers":[{"name":"anonymous","email":"dhrtklpsh82@gmail.com"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/docxtemplater-image-module-free_1.0.1_1548348835782_0.7070488168214082"},"_hasShrinkwrap":false,"publish_time":1548348835896,"_cnpm_publish_time":1548348835896,"contributors":[]},"1.0.0":{"name":"docxtemplater-image-module-free","version":"1.0.0","description":"Open Source Image Module for docxtemplater","main":"js/index.js","scripts":{"test:coverage":"istanbul cover _mocha --  es6/test.js","compile":"rimraf js && mkdirp js && babel es6 --out-dir js","preversion":"npm test && npm run browserify && npm run uglify","test:compiled":"mocha js/test.js","test:es6":"mocha es6/test.js","lint":"eslint .","test":"npm run compile && npm run test:compiled","browserify":"rimraf build && mkdirp build && browserify --insert-global-vars __filename,__dirname -r ./js/index.js -s ImageModule > build/imagemodule.js","uglify":"uglifyjs build/imagemodule.js > build/imagemodule.min.js"},"devDependencies":{"babel-cli":"^6.11.4","babel-eslint":"^7.1.1","babel-preset-es2015":"^6.3.13","browserify":"^14.0.0","chai":"^3.4.1","docxtemplater":"^3.0.0","eslint":"^3.15.0","image-size":"^0.5.1","istanbul":"^0.4.5","jszip":"^2.6.1","mkdirp":"^0.5.1","mocha":"^3.2.0","rimraf":"^2.5.4","uglifyjs":"^2.4.10"},"repository":{"type":"git","url":"git+https://github.com/evilc0des/docxtemplater-image-module-free.git"},"author":{"name":"Dk Saha"},"license":"MIT","dependencies":{"xmldom":"^0.1.27"},"gitHead":"c18b9be456688cdc445d35dfff33f026efdf349b","bugs":{"url":"https://github.com/evilc0des/docxtemplater-image-module-free/issues"},"homepage":"https://github.com/evilc0des/docxtemplater-image-module-free#readme","_id":"docxtemplater-image-module-free@1.0.0","_npmVersion":"6.4.1","_nodeVersion":"11.3.0","_npmUser":{"name":"anonymous","email":"dhrtklpsh82@gmail.com"},"dist":{"shasum":"d3abd0f0b852e50cfd93030b5478348f26f10c98","size":96604,"noattachment":false,"tarball":"http://123.232.10.234:8212/nexus/content/groups/npm-public/docxtemplater-image-module-free/-/docxtemplater-image-module-free-1.0.0.tgz"},"maintainers":[{"name":"anonymous","email":"dhrtklpsh82@gmail.com"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/docxtemplater-image-module-free_1.0.0_1547502998337_0.27926959198059764"},"_hasShrinkwrap":false,"publish_time":1547502998465,"_cnpm_publish_time":1547502998465,"contributors":[]}},"dist-tags":{"latest":"1.1.1"},"name":"docxtemplater-image-module-free","time":{"modified":"2022-01-27T06:48:56.072Z","created":"2022-01-27T06:48:56.072Z","1.1.1":"2019-01-26T17:33:09.220Z","1.1.0":"2019-01-24T16:55:00.453Z","1.0.1":"2019-01-24T16:53:55.896Z","1.0.0":"2019-01-14T21:56:38.465Z"},"readme":"Open Source docxtemplater image module\r\n==========================================\r\nThis repository holds an maintained version of docxtemplater image module.\r\n\r\nThis package is open source. There is also a [paid version](https://docxtemplater.com/modules/image/) maintained by docxtemplater author.\r\n\r\nNote this version is compatible with docxtemplater 3.x.\r\n\r\nInstallation\r\n-------------\r\nYou first need to install docxtemplater by following its [installation guide](https://docxtemplater.readthedocs.io/en/latest/installation.html).\r\n\r\nFor Node.js install this package:\r\n```bash\r\nnpm install docxtemplater-image-module-free\r\n```\r\n\r\nFor the browser find builds in `build/` directory.\r\n\r\nAlternatively, you can create your own build from the sources:\r\n```bash\r\nnpm run compile\r\nnpm run browserify\r\nnpm run uglify\r\n```\r\n\r\nUsage\r\n-----\r\nAssuming your **docx** or **pptx** template contains only the text `{%image}`:\r\n```javascript\r\n//Node.js example\r\nvar ImageModule = require('open-docxtemplater-image-module');\r\n\r\n//Below the options that will be passed to ImageModule instance\r\nvar opts = {}\r\nopts.centered = false; //Set to true to always center images\r\nopts.fileType = \"docx\"; //Or pptx\r\n\r\n//Pass your image loader\r\nopts.getImage = function(tagValue, tagName) {\r\n    //tagValue is 'examples/image.png'\r\n    //tagName is 'image'\r\n    return fs.readFileSync(tagValue);\r\n}\r\n\r\n//Pass the function that return image size\r\nopts.getSize = function(img, tagValue, tagName) {\r\n    //img is the image returned by opts.getImage()\r\n    //tagValue is 'examples/image.png'\r\n    //tagName is 'image'\r\n    //tip: you can use node module 'image-size' here\r\n    return [150, 150];\r\n}\r\n\r\nvar imageModule = new ImageModule(opts);\r\n\r\nvar zip = new JSZip(content);\r\nvar doc = new Docxtemplater()\r\n    .attachModule(imageModule)\r\n    .loadZip(zip)\r\n    .setData({image: 'examples/image.png'})\r\n    .render();\r\n\r\nvar buffer = doc\r\n        .getZip()\r\n        .generate({type:\"nodebuffer\"});\r\n\r\nfs.writeFile(\"test.docx\",buffer);\r\n```\r\n\r\nSome notes regarding templates:\r\n* **docx** files: the placeholder `{%image}` must be in a dedicated paragraph.\r\n* **pptx** files: the placeholder `{%image}` must be in a dedicated text cell.\r\n\r\nIn the browser, this shows how to get the image asynchronously :\r\n\r\n```html\r\n<html>\r\n<script src=\"node_modules/docxtemplater/build/docxtemplater.js\"></script>\r\n<script src=\"node_modules/jszip/dist/jszip.js\"></script>\r\n<script src=\"node_modules/jszip/vendor/FileSaver.js\"></script>\r\n<script src=\"node_modules/jszip-utils/dist/jszip-utils.js\"></script>\r\n<script src=\"build/imagemodule.js\"></script>\r\n<script>\r\n  JSZipUtils.getBinaryContent('examples/image-example.docx', function (error, content) {\r\n    if (error) {\r\n      console.error(error);\r\n      return;\r\n    }\r\n    var opts = {}\r\n    opts.centered = false;\r\n    opts.getImage = function (tagValue, tagName) {\r\n      return new Promise(function (resolve, reject) {\r\n        JSZipUtils.getBinaryContent(tagValue, function (error, content) {\r\n          if (error) {\r\n            return reject(error);\r\n          }\r\n          return resolve(content);\r\n        });\r\n      });\r\n    }\r\n    opts.getSize = function (img, tagValue, tagName) {\r\n      // FOR FIXED SIZE IMAGE :\r\n      return [150, 150];\r\n\r\n      // FOR IMAGE COMING FROM A URL (IF TAGVALUE IS AN ADRESS) :\r\n      // To use this feature, you have to be using docxtemplater async\r\n      // (if you are calling setData(), you are not using async).\r\n      return new Promise(function (resolve, reject) {\r\n        var image = new Image();\r\n        image.src = url;\r\n        image.onload = function () {\r\n          resolve([image.width, image.height]);\r\n        };\r\n        image.onerror = function (e) {\r\n          console.log('img, tagValue, tagName : ', img, tagValue, tagName);\r\n          alert(\"An error occured while loading \" + tagValue);\r\n          reject(e);\r\n        }\r\n      });\r\n    }\r\n\r\n    var imageModule = new ImageModule(opts);\r\n\r\n    var zip = new JSZip(content);\r\n    var doc = new docxtemplater()\r\n      .loadZip(zip)\r\n      .attachModule(imageModule)\r\n      .compile();\r\n\r\n    doc.resolveData({\r\n      image: 'examples/image.png'\r\n    }).then(function () {\r\n      console.log('ready');\r\n      doc.render();\r\n      var out = doc.getZip().generate({\r\n        type: \"blob\",\r\n        mimeType: \"application/vnd.openxmlformats-officedocument.wordprocessingml.document\",\r\n      });\r\n      saveAs(out, \"generated.docx\");\r\n    })\r\n  });\r\n</script>\r\n\r\n</html>\r\n```\r\n\r\nCentering images\r\n----------------\r\nYou can center all images by setting the global switch to true `opts.centered = true`.\r\n\r\nIf you would like to choose which images should be centered one by one:\r\n* Set the global switch to false `opts.centered = false`.\r\n* Use `{%image}` for images that shouldn't be centered.\r\n* Use `{%%image}` for images that you would like to see centered.\r\n\r\nIn **pptx** generated documents, images are centered vertically and horizontally relative to the parent cell.\r\n\r\n## Async support\r\n\r\nIt is possible to get images asynchronously by returning a Promise in the getImage function and use the docxtemplater async api : http://docxtemplater.readthedocs.io/en/latest/async.html\r\n\r\nYou can also return a promise in the getSize function if you want to resolve the size asynchronously (like in the browser example above).\r\n\r\nHere is an example in node that allows you to retrieve values from an URL and use a fixed width, and keep the aspect ratio.\r\n\r\n```js\r\nconst fs = require(\"fs\");\r\nconst DocxTemplater = require(\"docxtemplater\");\r\nconst https = require(\"https\");\r\nconst Stream = require(\"stream\").Transform;\r\n\r\nconst ImageModule = require(\"./es6\");\r\nconst JSZip = require(\"jszip\");\r\n\r\nconst content = fs.readFileSync(\"demo_template.docx\");\r\n\r\nconst data = {\r\n  image: \"https://docxtemplater.com/xt-pro.png\"\r\n};\r\n\r\nconst opts = {};\r\nopts.getImage = function (tagValue, tagName) {\r\n  console.log(tagValue, tagName);\r\n  // tagValue is \"https://docxtemplater.com/xt-pro-white.png\" and tagName is \"image\"\r\n  return new Promise(function (resolve, reject) {\r\n    getHttpData(tagValue, function (err, data) {\r\n      if (err) {\r\n        return reject(err);\r\n      }\r\n      resolve(data);\r\n    });\r\n  });\r\n};\r\n\r\nopts.getSize = function (img, tagValue, tagName) {\r\n  console.log(tagValue, tagName);\r\n  // img is the value that was returned by getImage\r\n  // This is to force the width to 600px, but keep the same aspect ration\r\n  const sizeOf = require(\"image-size\");\r\n  const sizeObj = sizeOf(img);\r\n  console.log(sizeObj);\r\n  const forceWidth = 600;\r\n  const ratio = forceWidth / sizeObj.width;\r\n  return [\r\n    forceWidth,\r\n    // calculate height taking into account aspect ratio\r\n    Math.round(sizeObj.height * ratio),\r\n  ];\r\n};\r\n\r\nconst imageModule = new ImageModule(opts);\r\n\r\nconst zip = new JSZip(content);\r\nconst doc = new DocxTemplater()\r\n  .loadZip(zip)\r\n  .attachModule(imageModule)\r\n  .compile();\r\n\r\ndoc\r\n  .resolveData(data)\r\n  .then(function () {\r\n    console.log(\"data resolved\");\r\n    doc.render();\r\n    const buffer = doc\r\n      .getZip()\r\n      .generate({\r\n        type: \"nodebuffer\",\r\n        compression: \"DEFLATE\"\r\n      });\r\n\r\n    fs.writeFileSync(\"test.docx\", buffer);\r\n    console.log(\"rendered\");\r\n  })\r\n  .catch(function (error) {\r\n    console.log(\"An error occured\", error);\r\n  });\r\n\r\nfunction getHttpData(url, callback) {\r\n  https\r\n    .request(url, function (response) {\r\n      if (response.statusCode !== 200) {\r\n        return callback(\r\n          new Error(\r\n            `Request to ${url} failed, status code: ${response.statusCode}`\r\n          )\r\n        );\r\n      }\r\n\r\n      const data = new Stream();\r\n      response.on(\"data\", function (chunk) {\r\n        data.push(chunk);\r\n      });\r\n      response.on(\"end\", function () {\r\n        callback(null, data.read());\r\n      });\r\n      response.on(\"error\", function (e) {\r\n        callback(e);\r\n      });\r\n    })\r\n    .end();\r\n}\r\n```\r\n\r\n## Size and path based on placeholder\r\n\r\nYou can have customizable image loader using the template's placeholder name.\r\n\r\n```\r\nopts.getImage = function (tagValue, tagName) {\r\n    if(tagName === 'logo')\r\n        return fs.readFileSync(__dirname + '/logos/' + tagValue);\r\n\r\n    return fs.readFileSync(__dirname + '/images/' + tagValue);\r\n};\r\n```\r\n\r\nThe same thing can be used to customize image size.\r\n\r\n```\r\nopts.getSize = function (img, tagValue, tagName) {\r\n    if(tagName === 'logo')\r\n        return [100, 100];\r\n\r\n    return [300, 300];\r\n};\r\n```\r\n\r\n## Base64 include\r\n\r\nYou can use base64 images with the following code:\r\n\r\n```js\r\nfunction base64DataURLToArrayBuffer(dataURL) {\r\n  const base64Regex = /^data:image\\/(png|jpg|svg|svg\\+xml);base64,/;\r\n  if (!base64Regex.test(dataURL)) {\r\n    return false;\r\n  }\r\n  const stringBase64 = dataURL.replace(base64Regex, \"\");\r\n  let binaryString;\r\n  if (typeof window !== \"undefined\") {\r\n    binaryString = window.atob(stringBase64);\r\n  } else {\r\n    binaryString = new Buffer(stringBase64, \"base64\").toString(\"binary\");\r\n  }\r\n  const len = binaryString.length;\r\n  const bytes = new Uint8Array(len);\r\n  for (let i = 0; i < len; i++) {\r\n    const ascii = binaryString.charCodeAt(i);\r\n    bytes[i] = ascii;\r\n  }\r\n  return bytes.buffer;\r\n}\r\nconst imageOpts = {\r\n  getImage(tag) {\r\n    return base64DataURLToArrayBuffer(tag);\r\n  },\r\n  getSize() {\r\n    return [100, 100];\r\n  },\r\n};\r\ndoc.attachModule(new ImageModule(imageOpts));\r\n```","users":{}}