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 {
}
getRelsFile
(
fileName
)
{
let
relsFilePath
;
le
t
relsFileName
=
this
.
getRelsFileName
(
fileName
);
le
t
fileType
=
this
.
getFileType
(
fileName
);
if
(
fileType
==
"ppt"
)
{
cons
t
relsFileName
=
this
.
getRelsFileName
(
fileName
);
cons
t
fileType
=
this
.
getFileType
(
fileName
);
if
(
fileType
==
=
"ppt"
)
{
relsFilePath
=
"ppt/slides/_rels/"
+
relsFileName
;
}
else
{
}
else
{
relsFilePath
=
"word/_rels/"
+
relsFileName
;
}
return
relsFilePath
;
...
...
@@ -100,8 +101,9 @@ module.exports = class ImgManager {
newTag
.
setAttribute
(
"Id"
,
`rId
${
maxRid
}
`
);
newTag
.
setAttribute
(
"Type"
,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"
);
if
(
this
.
fileType
===
"ppt"
)
{
newTag
.
setAttribute
(
"Target"
,
`../media/
${
realImageName
}
`
)
}
else
{
newTag
.
setAttribute
(
"Target"
,
`../media/
${
realImageName
}
`
);
}
else
{
newTag
.
setAttribute
(
"Target"
,
`media/
${
realImageName
}
`
);
}
relationships
.
appendChild
(
newTag
);
...
...
es6/index.js
View file @
f9614e62
...
...
@@ -10,29 +10,23 @@ function isNaN(number) {
const
ImgManager
=
require
(
"./imgManager"
);
const
moduleName
=
"open-xml-templating/docxtemplater-image-module"
;
function
getInner
({
part
,
left
,
right
,
postparsed
,
index
})
{
le
t
xmlString
=
postparsed
.
slice
(
left
+
1
,
right
).
reduce
(
function
(
concat
,
item
)
{
return
concat
+
item
.
value
;
},
""
);
part
.
off
=
{
};
part
.
ext
=
{};
var
xmlDoc
=
new
DOMParser
().
parseFromString
(
"<xml>"
+
xmlString
+
"</xml>"
);
var
off
=
xmlDoc
.
getElementsByTagName
(
"a:off"
);
if
(
off
.
length
>
0
)
{
part
.
off
.
x
=
off
[
0
].
getAttribute
(
"x"
);
part
.
off
.
y
=
off
[
0
].
getAttribute
(
"y"
);
}
var
ext
=
xmlDoc
.
getElementsByTagName
(
"a:ext"
);
function
getInner
({
part
,
left
,
right
,
postparsed
})
{
cons
t
xmlString
=
postparsed
.
slice
(
left
+
1
,
right
).
reduce
(
function
(
concat
,
item
)
{
return
concat
+
item
.
value
;
},
""
);
part
.
off
=
{
x
:
0
,
y
:
0
};
part
.
ext
=
{
cx
:
0
,
cy
:
0
};
const
xmlDoc
=
new
DOMParser
().
parseFromString
(
"<xml>"
+
xmlString
+
"</xml>"
);
const
off
=
xmlDoc
.
getElementsByTagName
(
"a:off"
);
if
(
off
.
length
>
0
)
{
part
.
off
.
x
=
off
[
0
].
getAttribute
(
"x"
);
part
.
off
.
y
=
off
[
0
].
getAttribute
(
"y"
);
}
const
ext
=
xmlDoc
.
getElementsByTagName
(
"a:ext"
);
if
(
ext
.
length
>
0
)
{
part
.
ext
.
cx
=
ext
[
0
].
getAttribute
(
"cx"
);
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
;
}
...
...
@@ -65,19 +59,20 @@ class ImageModule {
parse
(
placeHolderContent
)
{
const
module
=
moduleName
;
const
type
=
"placeholder"
;
if
(
placeHolderContent
.
substring
(
0
,
2
)
===
"%%"
)
{
return
{
type
,
value
:
placeHolderContent
.
substr
(
2
),
module
,
centered
:
true
};
}
if
(
placeHolderContent
.
substring
(
0
,
1
)
===
"%"
)
{
return
{
type
,
value
:
placeHolderContent
.
substr
(
1
),
module
,
centered
:
false
};
}
if
(
placeHolderContent
.
substring
(
0
,
2
)
===
"%%"
)
{
return
{
type
,
value
:
placeHolderContent
.
substr
(
2
),
module
,
centered
:
true
};
}
if
(
placeHolderContent
.
substring
(
0
,
1
)
===
"%"
)
{
return
{
type
,
value
:
placeHolderContent
.
substr
(
1
),
module
,
centered
:
false
};
}
return
null
;
}
postparse
(
parsed
)
{
let
expandTo
;
if
(
this
.
options
.
fileType
==
"pptx"
)
{
if
(
this
.
options
.
fileType
==
=
"pptx"
)
{
expandTo
=
"p:sp"
;
}
else
{
}
else
{
expandTo
=
this
.
options
.
centered
?
"w:p"
:
"w:t"
;
}
return
DocUtils
.
traits
.
expandToOne
(
parsed
,
{
moduleName
,
getInner
,
expandTo
});
...
...
@@ -87,44 +82,49 @@ class ImageModule {
if
(
!
part
.
type
===
"placeholder"
||
part
.
module
!==
moduleName
)
{
return
null
;
}
const
tagValue
=
options
.
scopeManager
.
getValue
(
part
.
value
);
const
tagXml
=
this
.
fileTypeConfig
.
tagTextXml
;
if
(
tagValue
==
null
)
{
return
{
value
:
tagXml
};
}
let
imgBuffer
;
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
)
{
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
centered
=
(
this
.
options
.
centered
||
part
.
centered
);
let
newText
;
if
(
this
.
options
.
fileType
==
"pptx"
)
{
let
offset
=
{
x
:
parseInt
(
part
.
off
.
x
,
10
),
y
:
parseInt
(
part
.
off
.
y
,
10
)};
let
cellCX
=
parseInt
(
part
.
ext
.
cx
,
10
)
||
1
;
let
cellCY
=
parseInt
(
part
.
ext
.
cy
,
10
)
||
1
;
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
);
if
(
this
.
options
.
fileType
===
"pptx"
)
{
newText
=
this
.
getPptRender
(
part
,
rId
,
size
,
centered
);
}
else
{
newText
=
this
.
getDocxRender
(
part
,
rId
,
size
,
centered
);
}
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
()
{
const
name
=
`image_generated_
${
this
.
imageNumber
}
.png`
;
this
.
imageNumber
++
;
...
...
@@ -241,7 +241,7 @@ class ImageModule {
</w:p>
`
.
replace
(
/
\t
|
\n
/g
,
""
);
}
getP
PT
ImageXml
(
rId
,
size
,
off
)
{
getP
pt
ImageXml
(
rId
,
size
,
off
)
{
if
(
isNaN
(
rId
))
{
throw
new
Error
(
"rId is NaN, aborting"
);
}
...
...
es6/test.js
View file @
f9614e62
...
...
@@ -21,7 +21,7 @@ const fileNames = [
"withoutRels.docx"
,
"expectedWithoutRels.docx"
,
"tagImage.pptx"
,
"expectedTagImage.pptx"
"expectedTagImage.pptx"
,
];
beforeEach
(
function
()
{
...
...
@@ -32,15 +32,15 @@ beforeEach(function () {
getSize
:
function
()
{
return
[
150
,
150
];
},
centered
:
false
centered
:
false
,
};
this
.
loadAndRender
=
function
()
{
var
fileType
=
(
testutils
.
pptX
[
this
.
name
])
?
'pptx'
:
'docx'
;
var
file
=
(
fileType
==
'pptx'
)
?
testutils
.
pptX
[
this
.
name
]
:
testutils
.
docX
[
this
.
name
];
const
fileType
=
(
testutils
.
pptX
[
this
.
name
])
?
"pptx"
:
"docx"
;
const
file
=
(
fileType
===
"pptx"
)
?
testutils
.
pptX
[
this
.
name
]
:
testutils
.
docX
[
this
.
name
];
this
.
doc
=
new
Docxtemplater
();
this
.
doc
.
setOptions
({
fileType
});
this
.
opts
.
fileType
=
fileType
;
this
.
doc
.
setOptions
({
fileType
});
this
.
opts
.
fileType
=
fileType
;
const
inputZip
=
new
JSZip
(
file
.
loadedContent
);
this
.
doc
.
loadZip
(
inputZip
).
setData
(
this
.
data
);
const
imageModule
=
new
ImageModule
(
this
.
opts
);
...
...
@@ -57,7 +57,7 @@ function testStart() {
this
.
name
=
"imageExample.docx"
;
this
.
expectedName
=
"expectedOneImage.docx"
;
this
.
data
=
{
image
:
"examples/image.png"
};
this
.
fileType
=
'docx'
;
this
.
fileType
=
"docx"
;
this
.
loadAndRender
();
});
...
...
@@ -98,22 +98,23 @@ function testStart() {
this
.
loadAndRender
();
});
it
(
"should work with PPTX documents"
,
function
()
{
this
.
name
=
"tagImage.pptx"
;
this
.
expectedName
=
"expectedTagImage.pptx"
;
this
.
data
=
{
image
:
"examples/image.png"
};
this
.
loadAndRender
();
});
it
(
"should work with PPTX documents"
,
function
()
{
this
.
name
=
"tagImage.pptx"
;
this
.
expectedName
=
"expectedTagImage.pptx"
;
this
.
data
=
{
image
:
"examples/image.png"
};
this
.
loadAndRender
();
});
});
}
testutils
.
setExamplesDirectory
(
path
.
resolve
(
__dirname
,
".."
,
"examples"
));
testutils
.
setStartFunction
(
testStart
);
fileNames
.
forEach
(
function
(
filename
)
{
if
(
filename
.
endsWith
(
'pptx'
))
{
testutils
.
loadFile
(
filename
,
testutils
.
loadPptx
);
}
else
{
testutils
.
loadFile
(
filename
,
testutils
.
loadDocx
);
}
if
(
filename
.
indexOf
(
".pptx"
)
===
filename
.
length
-
5
)
{
testutils
.
loadFile
(
filename
,
testutils
.
loadPptx
);
}
else
{
testutils
.
loadFile
(
filename
,
testutils
.
loadDocx
);
}
});
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