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
a237acb5
Commit
a237acb5
authored
Sep 24, 2015
by
Edgar HIPP
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update API version 1:mandatory getImage,getSize
parent
78061767
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
21 deletions
+32
-21
coffee/index.coffee
+9
-11
coffee/test.coffee
+23
-10
No files found.
coffee/index.coffee
View file @
a237acb5
...
@@ -6,6 +6,8 @@ fs=require('fs')
...
@@ -6,6 +6,8 @@ fs=require('fs')
class
ImageModule
class
ImageModule
constructor
:
(
@
options
=
{})
->
constructor
:
(
@
options
=
{})
->
if
!
@
options
.
centered
?
then
@
options
.
centered
=
false
if
!
@
options
.
centered
?
then
@
options
.
centered
=
false
if
!
@
options
.
getImage
?
then
throw
new
Error
(
"You should pass getImage"
)
if
!
@
options
.
getSize
?
then
throw
new
Error
(
"You should pass getSize"
)
@
qrQueue
=
[]
@
qrQueue
=
[]
@
imageNumber
=
1
@
imageNumber
=
1
handleEvent
:
(
event
,
eventData
)
->
handleEvent
:
(
event
,
eventData
)
->
...
@@ -35,29 +37,25 @@ class ImageModule
...
@@ -35,29 +37,25 @@ class ImageModule
xmlTemplater
.
replaceXml
(
subContent
,
text
)
xmlTemplater
.
replaceXml
(
subContent
,
text
)
convertPixelsToEmus
:
(
pixel
)
->
convertPixelsToEmus
:
(
pixel
)
->
Math
.
round
(
pixel
*
9525
)
Math
.
round
(
pixel
*
9525
)
getSizeFromData
:
(
imgData
)
->
[
150
,
150
]
getImageFromData
:
(
imgData
)
->
fs
.
readFileSync
(
imgData
)
replaceTag
:->
replaceTag
:->
scopeManager
=
@
manager
.
getInstance
(
'scopeManager'
)
scopeManager
=
@
manager
.
getInstance
(
'scopeManager'
)
templaterState
=
@
manager
.
getInstance
(
'templaterState'
)
templaterState
=
@
manager
.
getInstance
(
'templaterState'
)
tag
=
templaterState
.
textInsideTag
.
substr
(
1
)
tag
=
templaterState
.
textInsideTag
.
substr
(
1
)
imgData
=
scopeManager
.
getValueFromScop
e
(
tag
)
tagValue
=
scopeManager
.
getValu
e
(
tag
)
tagXml
=
@
manager
.
getInstance
(
'xmlTemplater'
).
tagXml
tagXml
=
@
manager
.
getInstance
(
'xmlTemplater'
).
tagXml
startEnd
=
"<
#{
tagXml
}
></
#{
tagXml
}
>"
startEnd
=
"<
#{
tagXml
}
></
#{
tagXml
}
>"
if
imgData
==
'undefined'
then
return
@
replaceBy
(
startEnd
,
tagXml
)
if
!
tagValue
?
then
return
@
replaceBy
(
startEnd
,
tagXml
)
try
try
imgBuffer
=
@
getImageFromData
(
imgData
)
imgBuffer
=
@
options
.
getImage
(
tagValue
)
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
=
@
getSizeFromData
(
imgBuffer
)
sizePixel
=
@
options
.
getSize
(
imgBuffer
,
tagValue
)
size
=
[
@
convertPixelsToEmus
(
sizePixel
[
0
]),
@
convertPixelsToEmus
(
sizePixel
[
1
])]
size
=
[
@
convertPixelsToEmus
(
sizePixel
[
0
]),
@
convertPixelsToEmus
(
sizePixel
[
1
])]
if
@
options
.
centered
==
false
if
@
options
.
centered
==
false
...
@@ -72,10 +70,10 @@ class ImageModule
...
@@ -72,10 +70,10 @@ class ImageModule
xmlTemplater
=
@
manager
.
getInstance
(
'xmlTemplater'
)
xmlTemplater
=
@
manager
.
getInstance
(
'xmlTemplater'
)
imR
=
new
ImgReplacer
(
xmlTemplater
,
@
imgManager
)
imR
=
new
ImgReplacer
(
xmlTemplater
,
@
imgManager
)
imR
.
getDataFromString
=
(
result
,
cb
)
=>
imR
.
getDataFromString
=
(
result
,
cb
)
=>
if
@
getImage
FromData
Async
?
if
@
getImageAsync
?
@
getImage
FromData
Async
(
result
,
cb
)
@
getImageAsync
(
result
,
cb
)
else
else
cb
(
null
,
@
getImage
FromData
(
result
))
cb
(
null
,
@
getImage
(
result
))
imR
.
pushQrQueue
=
(
num
)
=>
imR
.
pushQrQueue
=
(
num
)
=>
@
qrQueue
.
push
(
num
)
@
qrQueue
.
push
(
num
)
imR
.
popQrQueue
=
(
num
)
=>
imR
.
popQrQueue
=
(
num
)
=>
...
...
coffee/test.coffee
View file @
a237acb5
...
@@ -12,6 +12,15 @@ fileNames=[
...
@@ -12,6 +12,15 @@ fileNames=[
'qrExample2.docx'
,
'qrExample2.docx'
,
]
]
opts
=
{}
beforeEach
()
->
opts
=
{}
opts
.
getImage
=
(
tagValue
)
->
fs
.
readFileSync
(
tagValue
,
'binary'
)
opts
.
getSize
=
(
imgBuffer
,
tagValue
)
->
[
150
,
150
]
opts
.
centered
=
false
ImageModule
=
require
(
'../js/index.js'
)
ImageModule
=
require
(
'../js/index.js'
)
docX
=
{}
docX
=
{}
...
@@ -33,7 +42,7 @@ for name in fileNames
...
@@ -33,7 +42,7 @@ for name in fileNames
describe
'image adding with {% image} syntax'
,
()
->
describe
'image adding with {% image} syntax'
,
()
->
it
'should work with one image'
,()
->
it
'should work with one image'
,()
->
name
=
'imageExample.docx'
name
=
'imageExample.docx'
imageModule
=
new
ImageModule
(
{
centered
:
false
}
)
imageModule
=
new
ImageModule
(
opts
)
docX
[
name
].
attachModule
(
imageModule
)
docX
[
name
].
attachModule
(
imageModule
)
out
=
docX
[
name
]
out
=
docX
[
name
]
.
load
(
docX
[
name
].
loadedContent
)
.
load
(
docX
[
name
].
loadedContent
)
...
@@ -61,7 +70,8 @@ describe 'image adding with {% image} syntax', ()->
...
@@ -61,7 +70,8 @@ describe 'image adding with {% image} syntax', ()->
it
'should work with centering'
,()
->
it
'should work with centering'
,()
->
d
=
new
DocxGen
()
d
=
new
DocxGen
()
name
=
'imageExample.docx'
name
=
'imageExample.docx'
imageModule
=
new
ImageModule
({
centered
:
true
})
opts
.
centered
=
true
imageModule
=
new
ImageModule
(
opts
)
d
.
attachModule
(
imageModule
)
d
.
attachModule
(
imageModule
)
out
=
d
out
=
d
.
load
(
docX
[
name
].
loadedContent
)
.
load
(
docX
[
name
].
loadedContent
)
...
@@ -92,7 +102,8 @@ describe 'image adding with {% image} syntax', ()->
...
@@ -92,7 +102,8 @@ describe 'image adding with {% image} syntax', ()->
it
'should work with loops'
,()
->
it
'should work with loops'
,()
->
name
=
'imageLoopExample.docx'
name
=
'imageLoopExample.docx'
imageModule
=
new
ImageModule
({
centered
:
true
})
opts
.
centered
=
true
imageModule
=
new
ImageModule
(
opts
)
docX
[
name
].
attachModule
(
imageModule
)
docX
[
name
].
attachModule
(
imageModule
)
out
=
docX
[
name
]
out
=
docX
[
name
]
...
@@ -127,7 +138,7 @@ describe 'image adding with {% image} syntax', ()->
...
@@ -127,7 +138,7 @@ describe 'image adding with {% image} syntax', ()->
it
'should work with image in header/footer'
,()
->
it
'should work with image in header/footer'
,()
->
name
=
'imageHeaderFooterExample.docx'
name
=
'imageHeaderFooterExample.docx'
imageModule
=
new
ImageModule
(
{
centered
:
false
}
)
imageModule
=
new
ImageModule
(
opts
)
docX
[
name
].
attachModule
(
imageModule
)
docX
[
name
].
attachModule
(
imageModule
)
out
=
docX
[
name
]
out
=
docX
[
name
]
.
load
(
docX
[
name
].
loadedContent
)
.
load
(
docX
[
name
].
loadedContent
)
...
@@ -176,10 +187,10 @@ describe 'qrcode replacing',->
...
@@ -176,10 +187,10 @@ describe 'qrcode replacing',->
zip
=
null
zip
=
null
before
(
done
)
->
before
(
done
)
->
name
=
'qrExample.docx'
name
=
'qrExample.docx'
opts
.
qrCode
=
true
imageModule
=
new
ImageModule
(
opts
)
imageModule
=
new
ImageModule
({
qrCode
:
true
})
imageModule
.
getImage
=
(
imgData
)
->
imageModule
.
getImageFromData
=
(
imgData
)
->
d
=
fs
.
readFileSync
(
'examples/'
+
imgData
,
'binary'
)
d
=
fs
.
readFileSync
(
'examples/'
+
imgData
,
'binary'
)
d
d
...
@@ -211,9 +222,10 @@ describe 'qrcode replacing',->
...
@@ -211,9 +222,10 @@ describe 'qrcode replacing',->
before
(
done
)
->
before
(
done
)
->
name
=
'qrExample2.docx'
name
=
'qrExample2.docx'
imageModule
=
new
ImageModule
({
qrCode
:
true
})
opts
.
qrCode
=
true
imageModule
=
new
ImageModule
(
opts
)
imageModule
.
getImage
FromData
=
(
imgData
)
->
imageModule
.
getImage
=
(
imgData
)
->
d
=
fs
.
readFileSync
(
'examples/'
+
imgData
,
'binary'
)
d
=
fs
.
readFileSync
(
'examples/'
+
imgData
,
'binary'
)
d
d
...
@@ -246,7 +258,8 @@ describe 'qrcode replacing',->
...
@@ -246,7 +258,8 @@ describe 'qrcode replacing',->
before
(
done
)
->
before
(
done
)
->
d
=
new
DocxGen
()
d
=
new
DocxGen
()
name
=
'noImage.docx'
name
=
'noImage.docx'
imageModule
=
new
ImageModule
({
qrCode
:
true
})
opts
.
qrCode
=
true
imageModule
=
new
ImageModule
(
opts
)
imageModule
.
finished
=
()
->
imageModule
.
finished
=
()
->
zip
=
d
.
getZip
()
zip
=
d
.
getZip
()
...
...
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