forked from evilc0des/docxtemplater-image-module-free
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
184 lines (170 loc) · 6.32 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
"use strict";
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 (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) {
this.imgManagers[options.filePath] = this.imgManagers[options.filePath] || new ImgManager(this.zip, options.filePath, this.xmlDocuments, this.fileType);
var imgManager = this.imgManagers[options.filePath];
if (!part.type === "placeholder" || part.module !== moduleName) {
return null;
}
var tagValue = options.scopeManager.getValue(part.value);
if (!tagValue) {
return { value: this.fileTypeConfig.tagTextXml };
}
var imgBuffer = this.options.getImage(tagValue, part.value);
if (!imgBuffer) {
return { value: this.fileTypeConfig.tagTextXml };
}
var rId = imgManager.addImageRels(this.getNextImageName(), imgBuffer);
var sizePixel = this.options.getSize(imgBuffer, tagValue, part.value);
return this.getRenderedPart(part, rId, 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;