Commit 81776cc5 by yeln4ts

Add border color option

parent 3c18ebc1
- ### 1.1.0
### 3.0.0
- Add Async support
- Add border color option
- ### 1.0.0
### 1.1.0
- This version is compatible with docxtemplater 3.0.
- Add Async support
### 1.0.0
- This version is compatible with docxtemplater 3.0.
Open Source docxtemplater image module
==========================================
This repository holds an maintained version of docxtemplater image module.
This repository holds a maintained version of docxtemplater image module for PwnDoc
This package is open source. There is also a [paid version](https://docxtemplater.com/modules/image/) maintained by docxtemplater author.
......@@ -12,7 +12,7 @@ You first need to install docxtemplater by following its [installation guide](ht
For Node.js install this package:
```bash
npm install docxtemplater-image-module-free
npm install git+https://github.com/pwndoc/docxtemplater-image-module-pwndoc
```
For the browser find builds in `build/` directory.
......@@ -35,6 +35,7 @@ var ImageModule = require('open-docxtemplater-image-module');
var opts = {}
opts.centered = false; //Set to true to always center images
opts.fileType = "docx"; //Or pptx
opts.border = "000000" // Set border on images with Hex color WITHOUT # (here black)
//Pass your image loader
opts.getImage = function(tagValue, tagName) {
......
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.ImageModule = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({"/js/index.js":[function(require,module,exports){
"use strict";
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var templates = require("./templates");
var DocUtils = require("docxtemplater").DocUtils;
var DOMParser = require("xmldom").DOMParser;
function isNaN(number) {
return !(number === number);
}
var ImgManager = require("./imgManager");
var moduleName = "open-xml-templating/docxtemplater-image-module";
function getInnerDocx(_ref) {
var part = _ref.part;
return part;
}
function getInnerPptx(_ref2) {
var part = _ref2.part,
left = _ref2.left,
right = _ref2.right,
postparsed = _ref2.postparsed;
var xmlString = postparsed.slice(left + 1, right).reduce(function (concat, item) {
return concat + item.value;
}, "");
var xmlDoc = new DOMParser().parseFromString("<xml>" + xmlString + "</xml>");
part.offset = { x: 0, y: 0 };
part.ext = { cx: 0, cy: 0 };
var offset = xmlDoc.getElementsByTagName("a:off");
var ext = xmlDoc.getElementsByTagName("a:ext");
if (ext.length > 0) {
part.ext.cx = parseInt(ext[ext.length - 1].getAttribute("cx"), 10);
part.ext.cy = parseInt(ext[ext.length - 1].getAttribute("cy"), 10);
}
if (offset.length > 0) {
part.offset.x = parseInt(offset[offset.length - 1].getAttribute("x"), 10);
part.offset.y = parseInt(offset[offset.length - 1].getAttribute("y"), 10);
}
return part;
}
var ImageModule = function () {
function ImageModule(options) {
_classCallCheck(this, ImageModule);
this.name = "ImageModule";
this.options = options || {};
this.imgManagers = {};
if (this.options.centered == null) {
this.options.centered = false;
}
if (this.options.getImage == null) {
throw new Error("You should pass getImage");
}
if (this.options.getSize == null) {
throw new Error("You should pass getSize");
}
this.imageNumber = 1;
}
_createClass(ImageModule, [{
key: "optionsTransformer",
value: function optionsTransformer(options, docxtemplater) {
var relsFiles = docxtemplater.zip.file(/\.xml\.rels/).concat(docxtemplater.zip.file(/\[Content_Types\].xml/)).map(function (file) {
return file.name;
});
this.fileTypeConfig = docxtemplater.fileTypeConfig;
this.fileType = docxtemplater.fileType;
this.zip = docxtemplater.zip;
options.xmlFileNames = options.xmlFileNames.concat(relsFiles);
return options;
}
}, {
key: "set",
value: function set(options) {
if (options.zip) {
this.zip = options.zip;
}
if (options.xmlDocuments) {
this.xmlDocuments = options.xmlDocuments;
}
}
}, {
key: "parse",
value: function parse(placeHolderContent) {
var module = moduleName;
var type = "placeholder";
if (this.options.setParser) {
return this.options.setParser(placeHolderContent);
}
if (placeHolderContent.substring(0, 2) === "%%") {
return { type: type, value: placeHolderContent.substr(2), module: module, centered: true };
}
if (placeHolderContent.substring(0, 1) === "%") {
return { type: type, value: placeHolderContent.substr(1), module: module, centered: false };
}
return null;
}
}, {
key: "postparse",
value: function postparse(parsed) {
var expandTo = void 0;
var getInner = void 0;
if (this.fileType === "pptx") {
expandTo = "p:sp";
getInner = getInnerPptx;
} else {
expandTo = this.options.centered ? "w:p" : "w:t";
getInner = getInnerDocx;
}
return DocUtils.traits.expandToOne(parsed, { moduleName: moduleName, getInner: getInner, expandTo: expandTo });
}
}, {
key: "render",
value: function render(part, options) {
if (!part.type === "placeholder" || part.module !== moduleName) {
return null;
}
var tagValue = options.scopeManager.getValue(part.value, {
part: part
});
if (!tagValue) {
return { value: this.fileTypeConfig.tagTextXml };
} else if ((typeof tagValue === "undefined" ? "undefined" : _typeof(tagValue)) === "object") {
return this.getRenderedPart(part, tagValue.rId, tagValue.sizePixel);
}
// this.imgManagers[options.filePath] = this.imgManagers[options.filePath] || new ImgManager(this.zip, options.filePath, this.xmlDocuments, this.fileType);
var imgManager = new ImgManager(this.zip, options.filePath, this.xmlDocuments, this.fileType);
var imgBuffer = this.options.getImage(tagValue, part.value);
var rId = imgManager.addImageRels(this.getNextImageName(), imgBuffer);
var sizePixel = this.options.getSize(imgBuffer, tagValue, part.value);
return this.getRenderedPart(part, rId, sizePixel);
}
}, {
key: "resolve",
value: function resolve(part, options) {
var _this = this;
// this.imgManagers[options.filePath] = this.imgManagers[options.filePath] || new ImgManager(this.zip, options.filePath, this.xmlDocuments, this.fileType);
var imgManager = new ImgManager(this.zip, options.filePath, this.xmlDocuments, this.fileType);
if (!part.type === "placeholder" || part.module !== moduleName) {
return null;
}
var value = options.scopeManager.getValue(part.value, {
part: part
});
if (!value) {
return { value: this.fileTypeConfig.tagTextXml };
}
return new Promise(function (resolve) {
var imgBuffer = _this.options.getImage(value, part.value);
resolve(imgBuffer);
}).then(function (imgBuffer) {
var rId = imgManager.addImageRels(_this.getNextImageName(), imgBuffer);
return new Promise(function (resolve) {
var sizePixel = _this.options.getSize(imgBuffer, value, part.value);
resolve(sizePixel);
}).then(function (sizePixel) {
return {
rId: rId,
sizePixel: sizePixel
};
});
});
}
}, {
key: "getRenderedPart",
value: function getRenderedPart(part, rId, sizePixel) {
if (isNaN(rId)) {
throw new Error("rId is NaN, aborting");
}
var size = [DocUtils.convertPixelsToEmus(sizePixel[0]), DocUtils.convertPixelsToEmus(sizePixel[1])];
var centered = this.options.centered || part.centered;
var newText = void 0;
if (this.fileType === "pptx") {
newText = this.getRenderedPartPptx(part, rId, size, centered);
} else {
newText = this.getRenderedPartDocx(rId, size, centered);
}
return { value: newText };
}
}, {
key: "getRenderedPartPptx",
value: function getRenderedPartPptx(part, rId, size, centered) {
var offset = { x: parseInt(part.offset.x, 10), y: parseInt(part.offset.y, 10) };
var cellCX = parseInt(part.ext.cx, 10) || 1;
var cellCY = parseInt(part.ext.cy, 10) || 1;
var imgW = parseInt(size[0], 10) || 1;
var imgH = parseInt(size[1], 10) || 1;
if (centered) {
offset.x = Math.round(offset.x + cellCX / 2 - imgW / 2);
offset.y = Math.round(offset.y + cellCY / 2 - imgH / 2);
}
return templates.getPptxImageXml(rId, [imgW, imgH], offset);
}
}, {
key: "getRenderedPartDocx",
value: function getRenderedPartDocx(rId, size, centered) {
return centered ? templates.getImageXmlCentered(rId, size) : templates.getImageXml(rId, size);
}
}, {
key: "getNextImageName",
value: function getNextImageName() {
var name = "image_generated_" + this.imageNumber + ".png";
this.imageNumber++;
return name;
}
}]);
return ImageModule;
}();
module.exports = ImageModule;
},{"./imgManager":2,"./templates":3,"docxtemplater":5,"xmldom":25}],1:[function(require,module,exports){
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.ImageModule = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
"use strict";
var DocUtils = require("docxtemplater").DocUtils;
......@@ -369,6 +146,9 @@ module.exports = {
getImageXml: function getImageXml(rId, size) {
return ("<w:drawing>\n\t\t<wp:inline distT=\"0\" distB=\"0\" distL=\"0\" distR=\"0\">\n\t\t\t<wp:extent cx=\"" + size[0] + "\" cy=\"" + size[1] + "\"/>\n\t\t\t<wp:effectExtent l=\"0\" t=\"0\" r=\"0\" b=\"0\"/>\n\t\t\t<wp:docPr id=\"2\" name=\"Image 2\" descr=\"image\"/>\n\t\t\t<wp:cNvGraphicFramePr>\n\t\t\t\t<a:graphicFrameLocks xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" noChangeAspect=\"1\"/>\n\t\t\t</wp:cNvGraphicFramePr>\n\t\t\t<a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">\n\t\t\t\t<a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">\n\t\t\t\t\t<pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">\n\t\t\t\t\t\t<pic:nvPicPr>\n\t\t\t\t\t\t\t<pic:cNvPr id=\"0\" name=\"Picture 1\" descr=\"image\"/>\n\t\t\t\t\t\t\t<pic:cNvPicPr>\n\t\t\t\t\t\t\t\t<a:picLocks noChangeAspect=\"1\" noChangeArrowheads=\"1\"/>\n\t\t\t\t\t\t\t</pic:cNvPicPr>\n\t\t\t\t\t\t</pic:nvPicPr>\n\t\t\t\t\t\t<pic:blipFill>\n\t\t\t\t\t\t\t<a:blip r:embed=\"rId" + rId + "\">\n\t\t\t\t\t\t\t\t<a:extLst>\n\t\t\t\t\t\t\t\t\t<a:ext uri=\"{28A0092B-C50C-407E-A947-70E740481C1C}\">\n\t\t\t\t\t\t\t\t\t\t<a14:useLocalDpi xmlns:a14=\"http://schemas.microsoft.com/office/drawing/2010/main\" val=\"0\"/>\n\t\t\t\t\t\t\t\t\t</a:ext>\n\t\t\t\t\t\t\t\t</a:extLst>\n\t\t\t\t\t\t\t</a:blip>\n\t\t\t\t\t\t\t<a:srcRect/>\n\t\t\t\t\t\t\t<a:stretch>\n\t\t\t\t\t\t\t\t<a:fillRect/>\n\t\t\t\t\t\t\t</a:stretch>\n\t\t\t\t\t\t</pic:blipFill>\n\t\t\t\t\t\t<pic:spPr bwMode=\"auto\">\n\t\t\t\t\t\t\t<a:xfrm>\n\t\t\t\t\t\t\t\t<a:off x=\"0\" y=\"0\"/>\n\t\t\t\t\t\t\t\t<a:ext cx=\"" + size[0] + "\" cy=\"" + size[1] + "\"/>\n\t\t\t\t\t\t\t</a:xfrm>\n\t\t\t\t\t\t\t<a:prstGeom prst=\"rect\">\n\t\t\t\t\t\t\t\t<a:avLst/>\n\t\t\t\t\t\t\t</a:prstGeom>\n\t\t\t\t\t\t\t<a:noFill/>\n\t\t\t\t\t\t\t<a:ln>\n\t\t\t\t\t\t\t\t<a:noFill/>\n\t\t\t\t\t\t\t</a:ln>\n\t\t\t\t\t\t</pic:spPr>\n\t\t\t\t\t</pic:pic>\n\t\t\t\t</a:graphicData>\n\t\t\t</a:graphic>\n\t\t</wp:inline>\n\t</w:drawing>\n\t\t").replace(/\t|\n/g, "");
},
getImageXmlBordered: function getImageXmlBordered(rId, size, border) {
return ("<w:drawing>\n\t\t<wp:inline distT=\"0\" distB=\"0\" distL=\"0\" distR=\"0\">\n\t\t\t<wp:extent cx=\"" + size[0] + "\" cy=\"" + size[1] + "\"/>\n\t\t\t<wp:effectExtent l=\"0\" t=\"0\" r=\"0\" b=\"0\"/>\n\t\t\t<wp:docPr id=\"2\" name=\"Image 2\" descr=\"image\"/>\n\t\t\t<wp:cNvGraphicFramePr>\n\t\t\t\t<a:graphicFrameLocks xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" noChangeAspect=\"1\"/>\n\t\t\t</wp:cNvGraphicFramePr>\n\t\t\t<a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">\n\t\t\t\t<a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">\n\t\t\t\t\t<pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">\n\t\t\t\t\t\t<pic:nvPicPr>\n\t\t\t\t\t\t\t<pic:cNvPr id=\"0\" name=\"Picture 1\" descr=\"image\"/>\n\t\t\t\t\t\t\t<pic:cNvPicPr>\n\t\t\t\t\t\t\t\t<a:picLocks noChangeAspect=\"1\" noChangeArrowheads=\"1\"/>\n\t\t\t\t\t\t\t</pic:cNvPicPr>\n\t\t\t\t\t\t</pic:nvPicPr>\n\t\t\t\t\t\t<pic:blipFill>\n\t\t\t\t\t\t\t<a:blip r:embed=\"rId" + rId + "\">\n\t\t\t\t\t\t\t\t<a:extLst>\n\t\t\t\t\t\t\t\t\t<a:ext uri=\"{28A0092B-C50C-407E-A947-70E740481C1C}\">\n\t\t\t\t\t\t\t\t\t\t<a14:useLocalDpi xmlns:a14=\"http://schemas.microsoft.com/office/drawing/2010/main\" val=\"0\"/>\n\t\t\t\t\t\t\t\t\t</a:ext>\n\t\t\t\t\t\t\t\t</a:extLst>\n\t\t\t\t\t\t\t</a:blip>\n\t\t\t\t\t\t\t<a:srcRect/>\n\t\t\t\t\t\t\t<a:stretch>\n\t\t\t\t\t\t\t\t<a:fillRect/>\n\t\t\t\t\t\t\t</a:stretch>\n\t\t\t\t\t\t</pic:blipFill>\n\t\t\t\t\t\t<pic:spPr bwMode=\"auto\">\n\t\t\t\t\t\t\t<a:xfrm>\n\t\t\t\t\t\t\t\t<a:off x=\"0\" y=\"0\"/>\n\t\t\t\t\t\t\t\t<a:ext cx=\"" + size[0] + "\" cy=\"" + size[1] + "\"/>\n\t\t\t\t\t\t\t</a:xfrm>\n\t\t\t\t\t\t\t<a:prstGeom prst=\"rect\">\n\t\t\t\t\t\t\t\t<a:avLst/>\n\t\t\t\t\t\t\t</a:prstGeom>\n\t\t\t\t\t\t\t<a:noFill/>\n\t\t\t\t\t\t\t<a:ln>\n\t\t\t\t\t\t\t\t<a:solidFill>\n\t\t\t\t\t\t\t\t\t<a:srgbClr val=\"" + border + "\"/>\n\t\t\t\t\t\t\t\t</a:solidFill>\n\t\t\t\t\t\t\t</a:ln>\n\t\t\t\t\t\t</pic:spPr>\n\t\t\t\t\t</pic:pic>\n\t\t\t\t</a:graphicData>\n\t\t\t</a:graphic>\n\t\t</wp:inline>\n\t</w:drawing>\n\t\t").replace(/\t|\n/g, "");
},
getImageXmlCentered: function getImageXmlCentered(rId, size) {
return ("<w:p>\n\t\t\t<w:pPr>\n\t\t\t\t<w:jc w:val=\"center\"/>\n\t\t\t</w:pPr>\n\t\t\t<w:r>\n\t\t\t\t<w:rPr/>\n\t\t\t\t<w:drawing>\n\t\t\t\t\t<wp:inline distT=\"0\" distB=\"0\" distL=\"0\" distR=\"0\">\n\t\t\t\t\t<wp:extent cx=\"" + size[0] + "\" cy=\"" + size[1] + "\"/>\n\t\t\t\t\t<wp:docPr id=\"0\" name=\"Picture\" descr=\"\"/>\n\t\t\t\t\t<a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">\n\t\t\t\t\t\t<a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">\n\t\t\t\t\t\t<pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">\n\t\t\t\t\t\t\t<pic:nvPicPr>\n\t\t\t\t\t\t\t<pic:cNvPr id=\"0\" name=\"Picture\" descr=\"\"/>\n\t\t\t\t\t\t\t<pic:cNvPicPr>\n\t\t\t\t\t\t\t\t<a:picLocks noChangeAspect=\"1\" noChangeArrowheads=\"1\"/>\n\t\t\t\t\t\t\t</pic:cNvPicPr>\n\t\t\t\t\t\t\t</pic:nvPicPr>\n\t\t\t\t\t\t\t<pic:blipFill>\n\t\t\t\t\t\t\t<a:blip r:embed=\"rId" + rId + "\"/>\n\t\t\t\t\t\t\t<a:stretch>\n\t\t\t\t\t\t\t\t<a:fillRect/>\n\t\t\t\t\t\t\t</a:stretch>\n\t\t\t\t\t\t\t</pic:blipFill>\n\t\t\t\t\t\t\t<pic:spPr bwMode=\"auto\">\n\t\t\t\t\t\t\t<a:xfrm>\n\t\t\t\t\t\t\t\t<a:off x=\"0\" y=\"0\"/>\n\t\t\t\t\t\t\t\t<a:ext cx=\"" + size[0] + "\" cy=\"" + size[1] + "\"/>\n\t\t\t\t\t\t\t</a:xfrm>\n\t\t\t\t\t\t\t<a:prstGeom prst=\"rect\">\n\t\t\t\t\t\t\t\t<a:avLst/>\n\t\t\t\t\t\t\t</a:prstGeom>\n\t\t\t\t\t\t\t<a:noFill/>\n\t\t\t\t\t\t\t<a:ln w=\"9525\">\n\t\t\t\t\t\t\t\t<a:noFill/>\n\t\t\t\t\t\t\t\t<a:miter lim=\"800000\"/>\n\t\t\t\t\t\t\t\t<a:headEnd/>\n\t\t\t\t\t\t\t\t<a:tailEnd/>\n\t\t\t\t\t\t\t</a:ln>\n\t\t\t\t\t\t\t</pic:spPr>\n\t\t\t\t\t\t</pic:pic>\n\t\t\t\t\t\t</a:graphicData>\n\t\t\t\t\t</a:graphic>\n\t\t\t\t\t</wp:inline>\n\t\t\t\t</w:drawing>\n\t\t\t</w:r>\n\t\t</w:p>\n\t\t").replace(/\t|\n/g, "");
},
......@@ -6117,5 +5897,230 @@ function split(source,start){
exports.XMLReader = XMLReader;
},{}]},{},[])("/js/index.js")
},{}],"/js/index.js":[function(require,module,exports){
"use strict";
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var templates = require("./templates");
var DocUtils = require("docxtemplater").DocUtils;
var DOMParser = require("xmldom").DOMParser;
function isNaN(number) {
return !(number === number);
}
var ImgManager = require("./imgManager");
var moduleName = "open-xml-templating/docxtemplater-image-module";
function getInnerDocx(_ref) {
var part = _ref.part;
return part;
}
function getInnerPptx(_ref2) {
var part = _ref2.part,
left = _ref2.left,
right = _ref2.right,
postparsed = _ref2.postparsed;
var xmlString = postparsed.slice(left + 1, right).reduce(function (concat, item) {
return concat + item.value;
}, "");
var xmlDoc = new DOMParser().parseFromString("<xml>" + xmlString + "</xml>");
part.offset = { x: 0, y: 0 };
part.ext = { cx: 0, cy: 0 };
var offset = xmlDoc.getElementsByTagName("a:off");
var ext = xmlDoc.getElementsByTagName("a:ext");
if (ext.length > 0) {
part.ext.cx = parseInt(ext[ext.length - 1].getAttribute("cx"), 10);
part.ext.cy = parseInt(ext[ext.length - 1].getAttribute("cy"), 10);
}
if (offset.length > 0) {
part.offset.x = parseInt(offset[offset.length - 1].getAttribute("x"), 10);
part.offset.y = parseInt(offset[offset.length - 1].getAttribute("y"), 10);
}
return part;
}
var ImageModule = function () {
function ImageModule(options) {
_classCallCheck(this, ImageModule);
this.name = "ImageModule";
this.options = options || {};
this.imgManagers = {};
if (this.options.centered == null) {
this.options.centered = false;
}
if (this.options.getImage == null) {
throw new Error("You should pass getImage");
}
if (this.options.getSize == null) {
throw new Error("You should pass getSize");
}
if (this.options.border && !/^[a-fA-F0-9]{6}$/.test(this.options.border)) {
throw new Error("border option is incorrect");
}
this.imageNumber = 1;
}
_createClass(ImageModule, [{
key: "optionsTransformer",
value: function optionsTransformer(options, docxtemplater) {
var relsFiles = docxtemplater.zip.file(/\.xml\.rels/).concat(docxtemplater.zip.file(/\[Content_Types\].xml/)).map(function (file) {
return file.name;
});
this.fileTypeConfig = docxtemplater.fileTypeConfig;
this.fileType = docxtemplater.fileType;
this.zip = docxtemplater.zip;
options.xmlFileNames = options.xmlFileNames.concat(relsFiles);
return options;
}
}, {
key: "set",
value: function set(options) {
if (options.zip) {
this.zip = options.zip;
}
if (options.xmlDocuments) {
this.xmlDocuments = options.xmlDocuments;
}
}
}, {
key: "parse",
value: function parse(placeHolderContent) {
var module = moduleName;
var type = "placeholder";
if (this.options.setParser) {
return this.options.setParser(placeHolderContent);
}
if (placeHolderContent.substring(0, 2) === "%%") {
return { type: type, value: placeHolderContent.substr(2), module: module, centered: true };
}
if (placeHolderContent.substring(0, 1) === "%") {
return { type: type, value: placeHolderContent.substr(1), module: module, centered: false };
}
return null;
}
}, {
key: "postparse",
value: function postparse(parsed) {
var expandTo = void 0;
var getInner = void 0;
if (this.fileType === "pptx") {
expandTo = "p:sp";
getInner = getInnerPptx;
} else {
expandTo = this.options.centered ? "w:p" : "w:t";
getInner = getInnerDocx;
}
return DocUtils.traits.expandToOne(parsed, { moduleName: moduleName, getInner: getInner, expandTo: expandTo });
}
}, {
key: "render",
value: function render(part, options) {
if (!part.type === "placeholder" || part.module !== moduleName) {
return null;
}
var tagValue = options.scopeManager.getValue(part.value, {
part: part
});
if (!tagValue) {
return { value: this.fileTypeConfig.tagTextXml };
} else if ((typeof tagValue === "undefined" ? "undefined" : _typeof(tagValue)) === "object") {
return this.getRenderedPart(part, tagValue.rId, tagValue.sizePixel);
}
var imgManager = new ImgManager(this.zip, options.filePath, this.xmlDocuments, this.fileType);
var imgBuffer = this.options.getImage(tagValue, part.value);
var rId = imgManager.addImageRels(this.getNextImageName(), imgBuffer);
var sizePixel = this.options.getSize(imgBuffer, tagValue, part.value);
return this.getRenderedPart(part, rId, sizePixel);
}
}, {
key: "resolve",
value: function resolve(part, options) {
var _this = this;
var imgManager = new ImgManager(this.zip, options.filePath, this.xmlDocuments, this.fileType);
if (!part.type === "placeholder" || part.module !== moduleName) {
return null;
}
var value = options.scopeManager.getValue(part.value, {
part: part
});
if (!value) {
return { value: this.fileTypeConfig.tagTextXml };
}
return new Promise(function (resolve) {
var imgBuffer = _this.options.getImage(value, part.value);
resolve(imgBuffer);
}).then(function (imgBuffer) {
var rId = imgManager.addImageRels(_this.getNextImageName(), imgBuffer);
return new Promise(function (resolve) {
var sizePixel = _this.options.getSize(imgBuffer, value, part.value);
resolve(sizePixel);
}).then(function (sizePixel) {
return {
rId: rId,
sizePixel: sizePixel
};
});
});
}
}, {
key: "getRenderedPart",
value: function getRenderedPart(part, rId, sizePixel) {
if (isNaN(rId)) {
throw new Error("rId is NaN, aborting");
}
var size = [DocUtils.convertPixelsToEmus(sizePixel[0]), DocUtils.convertPixelsToEmus(sizePixel[1])];
var centered = this.options.centered || part.centered;
var border = this.options.border || null;
var newText = void 0;
if (this.fileType === "pptx") {
newText = this.getRenderedPartPptx(part, rId, size, centered);
} else {
newText = this.getRenderedPartDocx(rId, size, centered, border);
}
return { value: newText };
}
}, {
key: "getRenderedPartPptx",
value: function getRenderedPartPptx(part, rId, size, centered) {
var offset = { x: parseInt(part.offset.x, 10), y: parseInt(part.offset.y, 10) };
var cellCX = parseInt(part.ext.cx, 10) || 1;
var cellCY = parseInt(part.ext.cy, 10) || 1;
var imgW = parseInt(size[0], 10) || 1;
var imgH = parseInt(size[1], 10) || 1;
if (centered) {
offset.x = Math.round(offset.x + cellCX / 2 - imgW / 2);
offset.y = Math.round(offset.y + cellCY / 2 - imgH / 2);
}
return templates.getPptxImageXml(rId, [imgW, imgH], offset);
}
}, {
key: "getRenderedPartDocx",
value: function getRenderedPartDocx(rId, size, centered) {
if (centered) return templates.getImageXmlCentered(rId, size);else if (border) return templates.getImageXmlBordered(rId, size, border);else return templates.getImageXml(rId, size);
}
}, {
key: "getNextImageName",
value: function getNextImageName() {
var name = "image_generated_" + this.imageNumber + ".png";
this.imageNumber++;
return name;
}
}]);
return ImageModule;
}();
module.exports = ImageModule;
},{"./imgManager":2,"./templates":3,"docxtemplater":5,"xmldom":25}]},{},[])("/js/index.js")
});
......@@ -43,6 +43,7 @@ class ImageModule {
if (this.options.centered == null) { this.options.centered = false; }
if (this.options.getImage == null) { throw new Error("You should pass getImage"); }
if (this.options.getSize == null) { throw new Error("You should pass getSize"); }
if (this.options.border && !/^[a-fA-F0-9]{6}$/.test(this.options.border)) { throw new Error("border option is incorrect"); }
this.imageNumber = 1;
}
optionsTransformer(options, docxtemplater) {
......@@ -142,12 +143,13 @@ class ImageModule {
}
const size = [DocUtils.convertPixelsToEmus(sizePixel[0]), DocUtils.convertPixelsToEmus(sizePixel[1])];
const centered = (this.options.centered || part.centered);
const border = this.options.border || null;
let newText;
if (this.fileType === "pptx") {
newText = this.getRenderedPartPptx(part, rId, size, centered);
}
else {
newText = this.getRenderedPartDocx(rId, size, centered);
newText = this.getRenderedPartDocx(rId, size, centered, border);
}
return {value: newText};
}
......@@ -164,7 +166,12 @@ class ImageModule {
return templates.getPptxImageXml(rId, [imgW, imgH], offset);
}
getRenderedPartDocx(rId, size, centered) {
return centered ? templates.getImageXmlCentered(rId, size) : templates.getImageXml(rId, size);
if (centered)
return templates.getImageXmlCentered(rId, size)
else if (border)
return templates.getImageXmlBordered(rId, size, border)
else
return templates.getImageXml(rId, size);
}
getNextImageName() {
const name = `image_generated_${this.imageNumber}.png`;
......
......@@ -50,6 +50,59 @@ module.exports = {
</w:drawing>
`.replace(/\t|\n/g, "");
},
getImageXmlBordered(rId, size, border) {
return `<w:drawing>
<wp:inline distT="0" distB="0" distL="0" distR="0">
<wp:extent cx="${size[0]}" cy="${size[1]}"/>
<wp:effectExtent l="0" t="0" r="0" b="0"/>
<wp:docPr id="2" name="Image 2" descr="image"/>
<wp:cNvGraphicFramePr>
<a:graphicFrameLocks xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" noChangeAspect="1"/>
</wp:cNvGraphicFramePr>
<a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
<a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
<pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
<pic:nvPicPr>
<pic:cNvPr id="0" name="Picture 1" descr="image"/>
<pic:cNvPicPr>
<a:picLocks noChangeAspect="1" noChangeArrowheads="1"/>
</pic:cNvPicPr>
</pic:nvPicPr>
<pic:blipFill>
<a:blip r:embed="rId${rId}">
<a:extLst>
<a:ext uri="{28A0092B-C50C-407E-A947-70E740481C1C}">
<a14:useLocalDpi xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main" val="0"/>
</a:ext>
</a:extLst>
</a:blip>
<a:srcRect/>
<a:stretch>
<a:fillRect/>
</a:stretch>
</pic:blipFill>
<pic:spPr bwMode="auto">
<a:xfrm>
<a:off x="0" y="0"/>
<a:ext cx="${size[0]}" cy="${size[1]}"/>
</a:xfrm>
<a:prstGeom prst="rect">
<a:avLst/>
</a:prstGeom>
<a:noFill/>
<a:ln>
<a:solidFill>
<a:srgbClr val="${border}"/>
</a:solidFill>
</a:ln>
</pic:spPr>
</pic:pic>
</a:graphicData>
</a:graphic>
</wp:inline>
</w:drawing>
`.replace(/\t|\n/g, "");
},
getImageXmlCentered(rId, size) {
return `<w:p>
<w:pPr>
......
......@@ -64,6 +64,9 @@ var ImageModule = function () {
if (this.options.getSize == null) {
throw new Error("You should pass getSize");
}
if (this.options.border && !/^[a-fA-F0-9]{6}$/.test(this.options.border)) {
throw new Error("border option is incorrect");
}
this.imageNumber = 1;
}
......@@ -133,7 +136,6 @@ var ImageModule = function () {
} else if ((typeof tagValue === "undefined" ? "undefined" : _typeof(tagValue)) === "object") {
return this.getRenderedPart(part, tagValue.rId, tagValue.sizePixel);
}
var imgManager = new ImgManager(this.zip, options.filePath, this.xmlDocuments, this.fileType);
var imgBuffer = this.options.getImage(tagValue, part.value);
var rId = imgManager.addImageRels(this.getNextImageName(), imgBuffer);
......@@ -179,11 +181,12 @@ var ImageModule = function () {
}
var size = [DocUtils.convertPixelsToEmus(sizePixel[0]), DocUtils.convertPixelsToEmus(sizePixel[1])];
var centered = this.options.centered || part.centered;
var border = this.options.border || null;
var newText = void 0;
if (this.fileType === "pptx") {
newText = this.getRenderedPartPptx(part, rId, size, centered);
} else {
newText = this.getRenderedPartDocx(rId, size, centered);
newText = this.getRenderedPartDocx(rId, size, centered, border);
}
return { value: newText };
}
......@@ -204,7 +207,7 @@ var ImageModule = function () {
}, {
key: "getRenderedPartDocx",
value: function getRenderedPartDocx(rId, size, centered) {
return centered ? templates.getImageXmlCentered(rId, size) : templates.getImageXml(rId, size);
if (centered) return templates.getImageXmlCentered(rId, size);else if (border) return templates.getImageXmlBordered(rId, size, border);else return templates.getImageXml(rId, size);
}
}, {
key: "getNextImageName",
......
......@@ -4,6 +4,9 @@ module.exports = {
getImageXml: function getImageXml(rId, size) {
return ("<w:drawing>\n\t\t<wp:inline distT=\"0\" distB=\"0\" distL=\"0\" distR=\"0\">\n\t\t\t<wp:extent cx=\"" + size[0] + "\" cy=\"" + size[1] + "\"/>\n\t\t\t<wp:effectExtent l=\"0\" t=\"0\" r=\"0\" b=\"0\"/>\n\t\t\t<wp:docPr id=\"2\" name=\"Image 2\" descr=\"image\"/>\n\t\t\t<wp:cNvGraphicFramePr>\n\t\t\t\t<a:graphicFrameLocks xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" noChangeAspect=\"1\"/>\n\t\t\t</wp:cNvGraphicFramePr>\n\t\t\t<a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">\n\t\t\t\t<a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">\n\t\t\t\t\t<pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">\n\t\t\t\t\t\t<pic:nvPicPr>\n\t\t\t\t\t\t\t<pic:cNvPr id=\"0\" name=\"Picture 1\" descr=\"image\"/>\n\t\t\t\t\t\t\t<pic:cNvPicPr>\n\t\t\t\t\t\t\t\t<a:picLocks noChangeAspect=\"1\" noChangeArrowheads=\"1\"/>\n\t\t\t\t\t\t\t</pic:cNvPicPr>\n\t\t\t\t\t\t</pic:nvPicPr>\n\t\t\t\t\t\t<pic:blipFill>\n\t\t\t\t\t\t\t<a:blip r:embed=\"rId" + rId + "\">\n\t\t\t\t\t\t\t\t<a:extLst>\n\t\t\t\t\t\t\t\t\t<a:ext uri=\"{28A0092B-C50C-407E-A947-70E740481C1C}\">\n\t\t\t\t\t\t\t\t\t\t<a14:useLocalDpi xmlns:a14=\"http://schemas.microsoft.com/office/drawing/2010/main\" val=\"0\"/>\n\t\t\t\t\t\t\t\t\t</a:ext>\n\t\t\t\t\t\t\t\t</a:extLst>\n\t\t\t\t\t\t\t</a:blip>\n\t\t\t\t\t\t\t<a:srcRect/>\n\t\t\t\t\t\t\t<a:stretch>\n\t\t\t\t\t\t\t\t<a:fillRect/>\n\t\t\t\t\t\t\t</a:stretch>\n\t\t\t\t\t\t</pic:blipFill>\n\t\t\t\t\t\t<pic:spPr bwMode=\"auto\">\n\t\t\t\t\t\t\t<a:xfrm>\n\t\t\t\t\t\t\t\t<a:off x=\"0\" y=\"0\"/>\n\t\t\t\t\t\t\t\t<a:ext cx=\"" + size[0] + "\" cy=\"" + size[1] + "\"/>\n\t\t\t\t\t\t\t</a:xfrm>\n\t\t\t\t\t\t\t<a:prstGeom prst=\"rect\">\n\t\t\t\t\t\t\t\t<a:avLst/>\n\t\t\t\t\t\t\t</a:prstGeom>\n\t\t\t\t\t\t\t<a:noFill/>\n\t\t\t\t\t\t\t<a:ln>\n\t\t\t\t\t\t\t\t<a:noFill/>\n\t\t\t\t\t\t\t</a:ln>\n\t\t\t\t\t\t</pic:spPr>\n\t\t\t\t\t</pic:pic>\n\t\t\t\t</a:graphicData>\n\t\t\t</a:graphic>\n\t\t</wp:inline>\n\t</w:drawing>\n\t\t").replace(/\t|\n/g, "");
},
getImageXmlBordered: function getImageXmlBordered(rId, size, border) {
return ("<w:drawing>\n\t\t<wp:inline distT=\"0\" distB=\"0\" distL=\"0\" distR=\"0\">\n\t\t\t<wp:extent cx=\"" + size[0] + "\" cy=\"" + size[1] + "\"/>\n\t\t\t<wp:effectExtent l=\"0\" t=\"0\" r=\"0\" b=\"0\"/>\n\t\t\t<wp:docPr id=\"2\" name=\"Image 2\" descr=\"image\"/>\n\t\t\t<wp:cNvGraphicFramePr>\n\t\t\t\t<a:graphicFrameLocks xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" noChangeAspect=\"1\"/>\n\t\t\t</wp:cNvGraphicFramePr>\n\t\t\t<a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">\n\t\t\t\t<a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">\n\t\t\t\t\t<pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">\n\t\t\t\t\t\t<pic:nvPicPr>\n\t\t\t\t\t\t\t<pic:cNvPr id=\"0\" name=\"Picture 1\" descr=\"image\"/>\n\t\t\t\t\t\t\t<pic:cNvPicPr>\n\t\t\t\t\t\t\t\t<a:picLocks noChangeAspect=\"1\" noChangeArrowheads=\"1\"/>\n\t\t\t\t\t\t\t</pic:cNvPicPr>\n\t\t\t\t\t\t</pic:nvPicPr>\n\t\t\t\t\t\t<pic:blipFill>\n\t\t\t\t\t\t\t<a:blip r:embed=\"rId" + rId + "\">\n\t\t\t\t\t\t\t\t<a:extLst>\n\t\t\t\t\t\t\t\t\t<a:ext uri=\"{28A0092B-C50C-407E-A947-70E740481C1C}\">\n\t\t\t\t\t\t\t\t\t\t<a14:useLocalDpi xmlns:a14=\"http://schemas.microsoft.com/office/drawing/2010/main\" val=\"0\"/>\n\t\t\t\t\t\t\t\t\t</a:ext>\n\t\t\t\t\t\t\t\t</a:extLst>\n\t\t\t\t\t\t\t</a:blip>\n\t\t\t\t\t\t\t<a:srcRect/>\n\t\t\t\t\t\t\t<a:stretch>\n\t\t\t\t\t\t\t\t<a:fillRect/>\n\t\t\t\t\t\t\t</a:stretch>\n\t\t\t\t\t\t</pic:blipFill>\n\t\t\t\t\t\t<pic:spPr bwMode=\"auto\">\n\t\t\t\t\t\t\t<a:xfrm>\n\t\t\t\t\t\t\t\t<a:off x=\"0\" y=\"0\"/>\n\t\t\t\t\t\t\t\t<a:ext cx=\"" + size[0] + "\" cy=\"" + size[1] + "\"/>\n\t\t\t\t\t\t\t</a:xfrm>\n\t\t\t\t\t\t\t<a:prstGeom prst=\"rect\">\n\t\t\t\t\t\t\t\t<a:avLst/>\n\t\t\t\t\t\t\t</a:prstGeom>\n\t\t\t\t\t\t\t<a:noFill/>\n\t\t\t\t\t\t\t<a:ln>\n\t\t\t\t\t\t\t\t<a:solidFill>\n\t\t\t\t\t\t\t\t\t<a:srgbClr val=\"" + border + "\"/>\n\t\t\t\t\t\t\t\t</a:solidFill>\n\t\t\t\t\t\t\t</a:ln>\n\t\t\t\t\t\t</pic:spPr>\n\t\t\t\t\t</pic:pic>\n\t\t\t\t</a:graphicData>\n\t\t\t</a:graphic>\n\t\t</wp:inline>\n\t</w:drawing>\n\t\t").replace(/\t|\n/g, "");
},
getImageXmlCentered: function getImageXmlCentered(rId, size) {
return ("<w:p>\n\t\t\t<w:pPr>\n\t\t\t\t<w:jc w:val=\"center\"/>\n\t\t\t</w:pPr>\n\t\t\t<w:r>\n\t\t\t\t<w:rPr/>\n\t\t\t\t<w:drawing>\n\t\t\t\t\t<wp:inline distT=\"0\" distB=\"0\" distL=\"0\" distR=\"0\">\n\t\t\t\t\t<wp:extent cx=\"" + size[0] + "\" cy=\"" + size[1] + "\"/>\n\t\t\t\t\t<wp:docPr id=\"0\" name=\"Picture\" descr=\"\"/>\n\t\t\t\t\t<a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">\n\t\t\t\t\t\t<a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">\n\t\t\t\t\t\t<pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">\n\t\t\t\t\t\t\t<pic:nvPicPr>\n\t\t\t\t\t\t\t<pic:cNvPr id=\"0\" name=\"Picture\" descr=\"\"/>\n\t\t\t\t\t\t\t<pic:cNvPicPr>\n\t\t\t\t\t\t\t\t<a:picLocks noChangeAspect=\"1\" noChangeArrowheads=\"1\"/>\n\t\t\t\t\t\t\t</pic:cNvPicPr>\n\t\t\t\t\t\t\t</pic:nvPicPr>\n\t\t\t\t\t\t\t<pic:blipFill>\n\t\t\t\t\t\t\t<a:blip r:embed=\"rId" + rId + "\"/>\n\t\t\t\t\t\t\t<a:stretch>\n\t\t\t\t\t\t\t\t<a:fillRect/>\n\t\t\t\t\t\t\t</a:stretch>\n\t\t\t\t\t\t\t</pic:blipFill>\n\t\t\t\t\t\t\t<pic:spPr bwMode=\"auto\">\n\t\t\t\t\t\t\t<a:xfrm>\n\t\t\t\t\t\t\t\t<a:off x=\"0\" y=\"0\"/>\n\t\t\t\t\t\t\t\t<a:ext cx=\"" + size[0] + "\" cy=\"" + size[1] + "\"/>\n\t\t\t\t\t\t\t</a:xfrm>\n\t\t\t\t\t\t\t<a:prstGeom prst=\"rect\">\n\t\t\t\t\t\t\t\t<a:avLst/>\n\t\t\t\t\t\t\t</a:prstGeom>\n\t\t\t\t\t\t\t<a:noFill/>\n\t\t\t\t\t\t\t<a:ln w=\"9525\">\n\t\t\t\t\t\t\t\t<a:noFill/>\n\t\t\t\t\t\t\t\t<a:miter lim=\"800000\"/>\n\t\t\t\t\t\t\t\t<a:headEnd/>\n\t\t\t\t\t\t\t\t<a:tailEnd/>\n\t\t\t\t\t\t\t</a:ln>\n\t\t\t\t\t\t\t</pic:spPr>\n\t\t\t\t\t\t</pic:pic>\n\t\t\t\t\t\t</a:graphicData>\n\t\t\t\t\t</a:graphic>\n\t\t\t\t\t</wp:inline>\n\t\t\t\t</w:drawing>\n\t\t\t</w:r>\n\t\t</w:p>\n\t\t").replace(/\t|\n/g, "");
},
......
This source diff could not be displayed because it is too large. You can view the blob instead.
{
"name": "docxtemplater-image-module-free",
"version": "1.1.1",
"name": "docxtemplater-image-module-pwndoc",
"version": "3.0.0",
"description": "Open Source Image Module for docxtemplater",
"main": "js/index.js",
"scripts": {
......@@ -32,9 +32,9 @@
},
"repository": {
"type": "git",
"url": "https://github.com/evilc0des/docxtemplater-image-module-free"
"url": "https://github.com/pwndoc/docxtemplater-image-module-pwndoc"
},
"author": "Dk Saha",
"author": "yeln4ts",
"license": "MIT",
"dependencies": {
"xmldom": "^0.1.27"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment