Commit d177aa48 by Edgar Hipp

Merge pull request #26 from Takeno/master

Passing tagName to getSize and getImage
parents 089bcc81 f23e414d
...@@ -48,14 +48,14 @@ class ImageModule ...@@ -48,14 +48,14 @@ class ImageModule
startEnd= "<#{tagXml}></#{tagXml}>" startEnd= "<#{tagXml}></#{tagXml}>"
if !tagValue? then return @replaceBy(startEnd,tagXml) if !tagValue? then return @replaceBy(startEnd,tagXml)
try try
imgBuffer=@options.getImage(tagValue) imgBuffer=@options.getImage(tagValue, tag)
catch e catch e
return @replaceBy(startEnd,tagXml) return @replaceBy(startEnd,tagXml)
imageRels=@imgManager.loadImageRels(); imageRels=@imgManager.loadImageRels();
if imageRels if imageRels
rId=imageRels.addImageRels(@getNextImageName(),imgBuffer) rId=imageRels.addImageRels(@getNextImageName(),imgBuffer)
sizePixel=@options.getSize(imgBuffer, tagValue) sizePixel=@options.getSize(imgBuffer, tagValue, tag)
size=[@convertPixelsToEmus(sizePixel[0]),@convertPixelsToEmus(sizePixel[1])] size=[@convertPixelsToEmus(sizePixel[0]),@convertPixelsToEmus(sizePixel[1])]
if @options.centered==false if @options.centered==false
......
...@@ -19,11 +19,11 @@ Your docx should contain the text: `{%image}` ...@@ -19,11 +19,11 @@ Your docx should contain the text: `{%image}`
var opts = {} var opts = {}
opts.centered = false; opts.centered = false;
opts.getImage=function(tagValue) { opts.getImage=function(tagValue, tagName) {
return fs.readFileSync(tagValue,'binary'); return fs.readFileSync(tagValue,'binary');
} }
opts.getSize=function(img,tagValue) { opts.getSize=function(img,tagValue, tagName) {
return [150,150]; return [150,150];
} }
...@@ -42,7 +42,7 @@ Your docx should contain the text: `{%image}` ...@@ -42,7 +42,7 @@ Your docx should contain the text: `{%image}`
fs.writeFile("test.docx",buffer); fs.writeFile("test.docx",buffer);
To understand what `img` and `tagValue` mean, lets take an example : To understand what `img`, `tagValue`, `tagName` mean, lets take an example :
If your template is : If your template is :
...@@ -54,7 +54,7 @@ If your template is : ...@@ -54,7 +54,7 @@ If your template is :
"myImage":'sampleImage.png' "myImage":'sampleImage.png'
} }
tagValue will be equal to "sampleImage.png" , and img will be what ever the getImage function returned tagValue will be equal to "sampleImage.png", tagName will be equal to "myImage" and img will be what ever the getImage function returned
One of the most useful cases of this is to set the images to be the size of that image. One of the most useful cases of this is to set the images to be the size of that image.
...@@ -77,6 +77,26 @@ then, write: ...@@ -77,6 +77,26 @@ then, write:
You can center the images using opts.centered=true instead You can center the images using opts.centered=true instead
# Size and path based on placeholder
You can have customizable image loader using the template's placeholder name.
opts.getImage = function (tagValue, tagName) {
if(tagName === 'logo')
return fs.readFileSync(__dirname + '/logos/' + tagValue);
return fs.readFileSync(__dirname + '/images/' + tagValue);
};
The same thing can be used to customize image size.
opts.getSize = function (img, tagValue, tagName) {
if(tagName === 'logo')
return [100, 100];
return [300, 300];
};
# Notice # Notice
For the imagereplacer to work, the image tag: `{%image}` needs to be in its own `<w:p>`, so that means that you have to put a new line after and before the tag. For the imagereplacer to work, the image tag: `{%image}` needs to be in its own `<w:p>`, so that means that you have to put a new line after and before the tag.
......
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