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
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
33 additions
and
22 deletions
+33
-22
coffee/index.coffee
+9
-11
coffee/test.coffee
+24
-11
No files found.
coffee/index.coffee
View file @
a237acb5
...
...
@@ -6,6 +6,8 @@ fs=require('fs')
class
ImageModule
constructor
:
(
@
options
=
{})
->
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
=
[]
@
imageNumber
=
1
handleEvent
:
(
event
,
eventData
)
->
...
...
@@ -35,29 +37,25 @@ class ImageModule
xmlTemplater
.
replaceXml
(
subContent
,
text
)
convertPixelsToEmus
:
(
pixel
)
->
Math
.
round
(
pixel
*
9525
)
getSizeFromData
:
(
imgData
)
->
[
150
,
150
]
getImageFromData
:
(
imgData
)
->
fs
.
readFileSync
(
imgData
)
replaceTag
:->
scopeManager
=
@
manager
.
getInstance
(
'scopeManager'
)
templaterState
=
@
manager
.
getInstance
(
'templaterState'
)
tag
=
templaterState
.
textInsideTag
.
substr
(
1
)
imgData
=
scopeManager
.
getValueFromScop
e
(
tag
)
tagValue
=
scopeManager
.
getValu
e
(
tag
)
tagXml
=
@
manager
.
getInstance
(
'xmlTemplater'
).
tagXml
startEnd
=
"<
#{
tagXml
}
></
#{
tagXml
}
>"
if
imgData
==
'undefined'
then
return
@
replaceBy
(
startEnd
,
tagXml
)
if
!
tagValue
?
then
return
@
replaceBy
(
startEnd
,
tagXml
)
try
imgBuffer
=
@
getImageFromData
(
imgData
)
imgBuffer
=
@
options
.
getImage
(
tagValue
)
catch
e
return
@
replaceBy
(
startEnd
,
tagXml
)
imageRels
=
@
imgManager
.
loadImageRels
();
if
imageRels
rId
=
imageRels
.
addImageRels
(
@
getNextImageName
(),
imgBuffer
)
sizePixel
=
@
getSizeFromData
(
imgBuffer
)
sizePixel
=
@
options
.
getSize
(
imgBuffer
,
tagValue
)
size
=
[
@
convertPixelsToEmus
(
sizePixel
[
0
]),
@
convertPixelsToEmus
(
sizePixel
[
1
])]
if
@
options
.
centered
==
false
...
...
@@ -72,10 +70,10 @@ class ImageModule
xmlTemplater
=
@
manager
.
getInstance
(
'xmlTemplater'
)
imR
=
new
ImgReplacer
(
xmlTemplater
,
@
imgManager
)
imR
.
getDataFromString
=
(
result
,
cb
)
=>
if
@
getImage
FromData
Async
?
@
getImage
FromData
Async
(
result
,
cb
)
if
@
getImageAsync
?
@
getImageAsync
(
result
,
cb
)
else
cb
(
null
,
@
getImage
FromData
(
result
))
cb
(
null
,
@
getImage
(
result
))
imR
.
pushQrQueue
=
(
num
)
=>
@
qrQueue
.
push
(
num
)
imR
.
popQrQueue
=
(
num
)
=>
...
...
coffee/test.coffee
View file @
a237acb5
...
...
@@ -12,6 +12,15 @@ fileNames=[
'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'
)
docX
=
{}
...
...
@@ -33,7 +42,7 @@ for name in fileNames
describe
'image adding with {% image} syntax'
,
()
->
it
'should work with one image'
,()
->
name
=
'imageExample.docx'
imageModule
=
new
ImageModule
(
{
centered
:
false
}
)
imageModule
=
new
ImageModule
(
opts
)
docX
[
name
].
attachModule
(
imageModule
)
out
=
docX
[
name
]
.
load
(
docX
[
name
].
loadedContent
)
...
...
@@ -61,7 +70,8 @@ describe 'image adding with {% image} syntax', ()->
it
'should work with centering'
,()
->
d
=
new
DocxGen
()
name
=
'imageExample.docx'
imageModule
=
new
ImageModule
({
centered
:
true
})
opts
.
centered
=
true
imageModule
=
new
ImageModule
(
opts
)
d
.
attachModule
(
imageModule
)
out
=
d
.
load
(
docX
[
name
].
loadedContent
)
...
...
@@ -92,7 +102,8 @@ describe 'image adding with {% image} syntax', ()->
it
'should work with loops'
,()
->
name
=
'imageLoopExample.docx'
imageModule
=
new
ImageModule
({
centered
:
true
})
opts
.
centered
=
true
imageModule
=
new
ImageModule
(
opts
)
docX
[
name
].
attachModule
(
imageModule
)
out
=
docX
[
name
]
...
...
@@ -124,10 +135,10 @@ describe 'image adding with {% image} syntax', ()->
buffer
=
zip
.
generate
({
type
:
"nodebuffer"
})
fs
.
writeFile
(
"test_multi.docx"
,
buffer
);
it
'should work with image in header/footer'
,()
->
name
=
'imageHeaderFooterExample.docx'
imageModule
=
new
ImageModule
(
{
centered
:
false
}
)
imageModule
=
new
ImageModule
(
opts
)
docX
[
name
].
attachModule
(
imageModule
)
out
=
docX
[
name
]
.
load
(
docX
[
name
].
loadedContent
)
...
...
@@ -176,10 +187,10 @@ describe 'qrcode replacing',->
zip
=
null
before
(
done
)
->
name
=
'qrExample.docx'
opts
.
qrCode
=
true
imageModule
=
new
ImageModule
(
opts
)
imageModule
=
new
ImageModule
({
qrCode
:
true
})
imageModule
.
getImageFromData
=
(
imgData
)
->
imageModule
.
getImage
=
(
imgData
)
->
d
=
fs
.
readFileSync
(
'examples/'
+
imgData
,
'binary'
)
d
...
...
@@ -211,9 +222,10 @@ describe 'qrcode replacing',->
before
(
done
)
->
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
...
...
@@ -246,7 +258,8 @@ describe 'qrcode replacing',->
before
(
done
)
->
d
=
new
DocxGen
()
name
=
'noImage.docx'
imageModule
=
new
ImageModule
({
qrCode
:
true
})
opts
.
qrCode
=
true
imageModule
=
new
ImageModule
(
opts
)
imageModule
.
finished
=
()
->
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