Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
docxtemplater-image-module
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
npm
docxtemplater-image-module
Commits
f9614e62
Commit
f9614e62
authored
Dec 02, 2016
by
Maxime Richard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Code quality
parent
2ec78efa
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
84 additions
and
81 deletions
+84
-81
es6/imgManager.js
+8
-6
es6/index.js
+57
-57
es6/test.js
+19
-18
No files found.
es6/imgManager.js
View file @
f9614e62
...
@@ -14,11 +14,12 @@ module.exports = class ImgManager {
...
@@ -14,11 +14,12 @@ module.exports = class ImgManager {
}
}
getRelsFile
(
fileName
)
{
getRelsFile
(
fileName
)
{
let
relsFilePath
;
let
relsFilePath
;
le
t
relsFileName
=
this
.
getRelsFileName
(
fileName
);
cons
t
relsFileName
=
this
.
getRelsFileName
(
fileName
);
le
t
fileType
=
this
.
getFileType
(
fileName
);
cons
t
fileType
=
this
.
getFileType
(
fileName
);
if
(
fileType
==
"ppt"
)
{
if
(
fileType
==
=
"ppt"
)
{
relsFilePath
=
"ppt/slides/_rels/"
+
relsFileName
;
relsFilePath
=
"ppt/slides/_rels/"
+
relsFileName
;
}
else
{
}
else
{
relsFilePath
=
"word/_rels/"
+
relsFileName
;
relsFilePath
=
"word/_rels/"
+
relsFileName
;
}
}
return
relsFilePath
;
return
relsFilePath
;
...
@@ -100,8 +101,9 @@ module.exports = class ImgManager {
...
@@ -100,8 +101,9 @@ module.exports = class ImgManager {
newTag
.
setAttribute
(
"Id"
,
`rId
${
maxRid
}
`
);
newTag
.
setAttribute
(
"Id"
,
`rId
${
maxRid
}
`
);
newTag
.
setAttribute
(
"Type"
,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"
);
newTag
.
setAttribute
(
"Type"
,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"
);
if
(
this
.
fileType
===
"ppt"
)
{
if
(
this
.
fileType
===
"ppt"
)
{
newTag
.
setAttribute
(
"Target"
,
`../media/
${
realImageName
}
`
)
newTag
.
setAttribute
(
"Target"
,
`../media/
${
realImageName
}
`
);
}
else
{
}
else
{
newTag
.
setAttribute
(
"Target"
,
`media/
${
realImageName
}
`
);
newTag
.
setAttribute
(
"Target"
,
`media/
${
realImageName
}
`
);
}
}
relationships
.
appendChild
(
newTag
);
relationships
.
appendChild
(
newTag
);
...
...
es6/index.js
View file @
f9614e62
...
@@ -10,29 +10,23 @@ function isNaN(number) {
...
@@ -10,29 +10,23 @@ function isNaN(number) {
const
ImgManager
=
require
(
"./imgManager"
);
const
ImgManager
=
require
(
"./imgManager"
);
const
moduleName
=
"open-xml-templating/docxtemplater-image-module"
;
const
moduleName
=
"open-xml-templating/docxtemplater-image-module"
;
function
getInner
({
part
,
left
,
right
,
postparsed
,
index
})
{
function
getInner
({
part
,
left
,
right
,
postparsed
})
{
le
t
xmlString
=
postparsed
.
slice
(
left
+
1
,
right
).
reduce
(
function
(
concat
,
item
)
{
cons
t
xmlString
=
postparsed
.
slice
(
left
+
1
,
right
).
reduce
(
function
(
concat
,
item
)
{
return
concat
+
item
.
value
;
return
concat
+
item
.
value
;
},
""
);
},
""
);
part
.
off
=
{
};
part
.
off
=
{
x
:
0
,
y
:
0
};
part
.
ext
=
{};
part
.
ext
=
{
cx
:
0
,
cy
:
0
};
var
xmlDoc
=
new
DOMParser
().
parseFromString
(
"<xml>"
+
xmlString
+
"</xml>"
);
const
xmlDoc
=
new
DOMParser
().
parseFromString
(
"<xml>"
+
xmlString
+
"</xml>"
);
var
off
=
xmlDoc
.
getElementsByTagName
(
"a:off"
);
const
off
=
xmlDoc
.
getElementsByTagName
(
"a:off"
);
if
(
off
.
length
>
0
)
{
if
(
off
.
length
>
0
)
{
part
.
off
.
x
=
off
[
0
].
getAttribute
(
"x"
);
part
.
off
.
x
=
off
[
0
].
getAttribute
(
"x"
);
part
.
off
.
y
=
off
[
0
].
getAttribute
(
"y"
);
part
.
off
.
y
=
off
[
0
].
getAttribute
(
"y"
);
}
}
var
ext
=
xmlDoc
.
getElementsByTagName
(
"a:ext"
);
const
ext
=
xmlDoc
.
getElementsByTagName
(
"a:ext"
);
if
(
ext
.
length
>
0
)
{
if
(
ext
.
length
>
0
)
{
part
.
ext
.
cx
=
ext
[
0
].
getAttribute
(
"cx"
);
part
.
ext
.
cx
=
ext
[
0
].
getAttribute
(
"cx"
);
part
.
ext
.
cy
=
ext
[
0
].
getAttribute
(
"cy"
);
part
.
ext
.
cy
=
ext
[
0
].
getAttribute
(
"cy"
);
}
}
if
(
part
.
off
.
x
==
null
||
part
.
off
.
y
==
null
||
part
.
ext
.
cx
==
null
||
part
.
ext
.
cy
==
null
)
{
part
.
off
.
x
=
0
;
part
.
off
.
y
=
0
;
part
.
ext
.
cx
=
0
;
part
.
ext
.
cy
=
0
;
}
return
part
;
return
part
;
}
}
...
@@ -65,19 +59,20 @@ class ImageModule {
...
@@ -65,19 +59,20 @@ class ImageModule {
parse
(
placeHolderContent
)
{
parse
(
placeHolderContent
)
{
const
module
=
moduleName
;
const
module
=
moduleName
;
const
type
=
"placeholder"
;
const
type
=
"placeholder"
;
if
(
placeHolderContent
.
substring
(
0
,
2
)
===
"%%"
)
{
if
(
placeHolderContent
.
substring
(
0
,
2
)
===
"%%"
)
{
return
{
type
,
value
:
placeHolderContent
.
substr
(
2
),
module
,
centered
:
true
};
return
{
type
,
value
:
placeHolderContent
.
substr
(
2
),
module
,
centered
:
true
};
}
}
if
(
placeHolderContent
.
substring
(
0
,
1
)
===
"%"
)
{
if
(
placeHolderContent
.
substring
(
0
,
1
)
===
"%"
)
{
return
{
type
,
value
:
placeHolderContent
.
substr
(
1
),
module
,
centered
:
false
};
return
{
type
,
value
:
placeHolderContent
.
substr
(
1
),
module
,
centered
:
false
};
}
}
return
null
;
return
null
;
}
}
postparse
(
parsed
)
{
postparse
(
parsed
)
{
let
expandTo
;
let
expandTo
;
if
(
this
.
options
.
fileType
==
"pptx"
)
{
if
(
this
.
options
.
fileType
==
=
"pptx"
)
{
expandTo
=
"p:sp"
;
expandTo
=
"p:sp"
;
}
else
{
}
else
{
expandTo
=
this
.
options
.
centered
?
"w:p"
:
"w:t"
;
expandTo
=
this
.
options
.
centered
?
"w:p"
:
"w:t"
;
}
}
return
DocUtils
.
traits
.
expandToOne
(
parsed
,
{
moduleName
,
getInner
,
expandTo
});
return
DocUtils
.
traits
.
expandToOne
(
parsed
,
{
moduleName
,
getInner
,
expandTo
});
...
@@ -87,44 +82,49 @@ class ImageModule {
...
@@ -87,44 +82,49 @@ class ImageModule {
if
(
!
part
.
type
===
"placeholder"
||
part
.
module
!==
moduleName
)
{
if
(
!
part
.
type
===
"placeholder"
||
part
.
module
!==
moduleName
)
{
return
null
;
return
null
;
}
}
const
tagValue
=
options
.
scopeManager
.
getValue
(
part
.
value
);
const
tagXml
=
this
.
fileTypeConfig
.
tagTextXml
;
if
(
tagValue
==
null
)
{
return
{
value
:
tagXml
};
}
let
imgBuffer
;
try
{
try
{
imgBuffer
=
this
.
options
.
getImage
(
tagValue
,
part
.
value
);
const
tagValue
=
options
.
scopeManager
.
getValue
(
part
.
value
);
if
(
!
tagValue
)
{
throw
new
Error
(
"tagValue is empty"
);
}
const
imgBuffer
=
this
.
options
.
getImage
(
tagValue
,
part
.
value
);
const
rId
=
this
.
imgManager
.
addImageRels
(
this
.
getNextImageName
(),
imgBuffer
);
const
sizePixel
=
this
.
options
.
getSize
(
imgBuffer
,
tagValue
,
part
.
value
);
return
this
.
getRenderedPart
(
part
,
rId
,
sizePixel
);
}
}
catch
(
e
)
{
catch
(
e
)
{
return
{
value
:
t
ag
Xml
};
return
{
value
:
t
his
.
fileTypeConfig
.
tagText
Xml
};
}
}
const
rId
=
this
.
imgManager
.
addImageRels
(
this
.
getNextImageName
(),
imgBuffer
);
}
const
sizePixel
=
this
.
options
.
getSize
(
imgBuffer
,
tagValue
,
part
.
value
);
getRenderedPart
(
part
,
rId
,
sizePixel
)
{
const
size
=
[
this
.
convertPixelsToEmus
(
sizePixel
[
0
]),
this
.
convertPixelsToEmus
(
sizePixel
[
1
])];
const
size
=
[
this
.
convertPixelsToEmus
(
sizePixel
[
0
]),
this
.
convertPixelsToEmus
(
sizePixel
[
1
])];
const
centered
=
(
this
.
options
.
centered
||
part
.
centered
);
let
newText
;
let
newText
;
if
(
this
.
options
.
fileType
===
"pptx"
)
{
if
(
this
.
options
.
fileType
==
"pptx"
)
{
newText
=
this
.
getPptRender
(
part
,
rId
,
size
,
centered
);
let
offset
=
{
x
:
parseInt
(
part
.
off
.
x
,
10
),
y
:
parseInt
(
part
.
off
.
y
,
10
)};
}
let
cellCX
=
parseInt
(
part
.
ext
.
cx
,
10
)
||
1
;
else
{
let
cellCY
=
parseInt
(
part
.
ext
.
cy
,
10
)
||
1
;
newText
=
this
.
getDocxRender
(
part
,
rId
,
size
,
centered
);
let
imgW
=
parseInt
(
size
[
0
],
10
)
||
1
;
let
imgH
=
parseInt
(
size
[
1
],
10
)
||
1
;
if
(
this
.
options
.
centered
||
part
.
centered
)
{
offset
.
x
=
offset
.
x
+
(
cellCX
/
2
)
-
(
imgW
/
2
);
offset
.
y
=
offset
.
y
+
(
cellCY
/
2
)
-
(
imgH
/
2
);
}
newText
=
this
.
getPPTImageXml
(
rId
,
[
imgW
,
imgH
],
offset
);
}
else
{
newText
=
(
this
.
options
.
centered
||
part
.
centered
)
?
this
.
getImageXmlCentered
(
rId
,
size
)
:
this
.
getImageXml
(
rId
,
size
);
}
}
return
{
value
:
newText
};
return
{
value
:
newText
};
}
}
getPptRender
(
part
,
rId
,
size
,
centered
)
{
const
offset
=
{
x
:
parseInt
(
part
.
off
.
x
,
10
),
y
:
parseInt
(
part
.
off
.
y
,
10
)};
const
cellCX
=
parseInt
(
part
.
ext
.
cx
,
10
)
||
1
;
const
cellCY
=
parseInt
(
part
.
ext
.
cy
,
10
)
||
1
;
const
imgW
=
parseInt
(
size
[
0
],
10
)
||
1
;
const
imgH
=
parseInt
(
size
[
1
],
10
)
||
1
;
if
(
centered
)
{
offset
.
x
=
offset
.
x
+
(
cellCX
/
2
)
-
(
imgW
/
2
);
offset
.
y
=
offset
.
y
+
(
cellCY
/
2
)
-
(
imgH
/
2
);
}
return
this
.
getPptImageXml
(
rId
,
[
imgW
,
imgH
],
offset
);
}
getDocxRender
(
part
,
rId
,
size
,
centered
)
{
return
(
centered
)
?
this
.
getImageXmlCentered
(
rId
,
size
)
:
this
.
getImageXml
(
rId
,
size
);
}
getNextImageName
()
{
getNextImageName
()
{
const
name
=
`image_generated_
${
this
.
imageNumber
}
.png`
;
const
name
=
`image_generated_
${
this
.
imageNumber
}
.png`
;
this
.
imageNumber
++
;
this
.
imageNumber
++
;
...
@@ -241,7 +241,7 @@ class ImageModule {
...
@@ -241,7 +241,7 @@ class ImageModule {
</w:p>
</w:p>
`
.
replace
(
/
\t
|
\n
/g
,
""
);
`
.
replace
(
/
\t
|
\n
/g
,
""
);
}
}
getP
PT
ImageXml
(
rId
,
size
,
off
)
{
getP
pt
ImageXml
(
rId
,
size
,
off
)
{
if
(
isNaN
(
rId
))
{
if
(
isNaN
(
rId
))
{
throw
new
Error
(
"rId is NaN, aborting"
);
throw
new
Error
(
"rId is NaN, aborting"
);
}
}
...
...
es6/test.js
View file @
f9614e62
...
@@ -21,7 +21,7 @@ const fileNames = [
...
@@ -21,7 +21,7 @@ const fileNames = [
"withoutRels.docx"
,
"withoutRels.docx"
,
"expectedWithoutRels.docx"
,
"expectedWithoutRels.docx"
,
"tagImage.pptx"
,
"tagImage.pptx"
,
"expectedTagImage.pptx"
"expectedTagImage.pptx"
,
];
];
beforeEach
(
function
()
{
beforeEach
(
function
()
{
...
@@ -32,15 +32,15 @@ beforeEach(function () {
...
@@ -32,15 +32,15 @@ beforeEach(function () {
getSize
:
function
()
{
getSize
:
function
()
{
return
[
150
,
150
];
return
[
150
,
150
];
},
},
centered
:
false
centered
:
false
,
};
};
this
.
loadAndRender
=
function
()
{
this
.
loadAndRender
=
function
()
{
var
fileType
=
(
testutils
.
pptX
[
this
.
name
])
?
'pptx'
:
'docx'
;
const
fileType
=
(
testutils
.
pptX
[
this
.
name
])
?
"pptx"
:
"docx"
;
var
file
=
(
fileType
==
'pptx'
)
?
testutils
.
pptX
[
this
.
name
]
:
testutils
.
docX
[
this
.
name
];
const
file
=
(
fileType
===
"pptx"
)
?
testutils
.
pptX
[
this
.
name
]
:
testutils
.
docX
[
this
.
name
];
this
.
doc
=
new
Docxtemplater
();
this
.
doc
=
new
Docxtemplater
();
this
.
doc
.
setOptions
({
fileType
});
this
.
doc
.
setOptions
({
fileType
});
this
.
opts
.
fileType
=
fileType
;
this
.
opts
.
fileType
=
fileType
;
const
inputZip
=
new
JSZip
(
file
.
loadedContent
);
const
inputZip
=
new
JSZip
(
file
.
loadedContent
);
this
.
doc
.
loadZip
(
inputZip
).
setData
(
this
.
data
);
this
.
doc
.
loadZip
(
inputZip
).
setData
(
this
.
data
);
const
imageModule
=
new
ImageModule
(
this
.
opts
);
const
imageModule
=
new
ImageModule
(
this
.
opts
);
...
@@ -57,7 +57,7 @@ function testStart() {
...
@@ -57,7 +57,7 @@ function testStart() {
this
.
name
=
"imageExample.docx"
;
this
.
name
=
"imageExample.docx"
;
this
.
expectedName
=
"expectedOneImage.docx"
;
this
.
expectedName
=
"expectedOneImage.docx"
;
this
.
data
=
{
image
:
"examples/image.png"
};
this
.
data
=
{
image
:
"examples/image.png"
};
this
.
fileType
=
'docx'
;
this
.
fileType
=
"docx"
;
this
.
loadAndRender
();
this
.
loadAndRender
();
});
});
...
@@ -98,22 +98,23 @@ function testStart() {
...
@@ -98,22 +98,23 @@ function testStart() {
this
.
loadAndRender
();
this
.
loadAndRender
();
});
});
it
(
"should work with PPTX documents"
,
function
()
{
it
(
"should work with PPTX documents"
,
function
()
{
this
.
name
=
"tagImage.pptx"
;
this
.
name
=
"tagImage.pptx"
;
this
.
expectedName
=
"expectedTagImage.pptx"
;
this
.
expectedName
=
"expectedTagImage.pptx"
;
this
.
data
=
{
image
:
"examples/image.png"
};
this
.
data
=
{
image
:
"examples/image.png"
};
this
.
loadAndRender
();
this
.
loadAndRender
();
});
});
});
});
}
}
testutils
.
setExamplesDirectory
(
path
.
resolve
(
__dirname
,
".."
,
"examples"
));
testutils
.
setExamplesDirectory
(
path
.
resolve
(
__dirname
,
".."
,
"examples"
));
testutils
.
setStartFunction
(
testStart
);
testutils
.
setStartFunction
(
testStart
);
fileNames
.
forEach
(
function
(
filename
)
{
fileNames
.
forEach
(
function
(
filename
)
{
if
(
filename
.
endsWith
(
'pptx'
))
{
if
(
filename
.
indexOf
(
".pptx"
)
===
filename
.
length
-
5
)
{
testutils
.
loadFile
(
filename
,
testutils
.
loadPptx
);
testutils
.
loadFile
(
filename
,
testutils
.
loadPptx
);
}
else
{
}
testutils
.
loadFile
(
filename
,
testutils
.
loadDocx
);
else
{
}
testutils
.
loadFile
(
filename
,
testutils
.
loadDocx
);
}
});
});
testutils
.
start
();
testutils
.
start
();
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment