Commit 538f2066 by Edgar Hipp

qrCodeModule works now

parent 41ea55c4
......@@ -7,7 +7,7 @@ JSZip=require('jszip')
QrCode=require('qrcode-reader')
module.exports= class DocxQrCode
constructor:(imageData, @xmlTemplater,@imgName="",@num,@callback)->
constructor:(imageData, @xmlTemplater,@imgName="",@num,@getDataFromString)->
@callbacked=false
@data=imageData
if @data==undefined then throw new Error("data of qrcode can't be undefined")
......@@ -20,9 +20,9 @@ module.exports= class DocxQrCode
@qr= new QrCode()
@qr.callback= () ->
_this.ready= true
_this.result= this.result
testdoc= new _this.xmlTemplater.currentClass this.result, _this.xmlTemplater.toJson()
testdoc.applyTags()
_this.result= @result
testdoc= new _this.xmlTemplater.currentClass @result,{Tags:_this.xmlTemplater.Tags}
testdoc.render()
_this.result=testdoc.content
_this.searchImage()
if DocUtils.env=='browser'
......@@ -34,4 +34,4 @@ module.exports= class DocxQrCode
if err then console.error err
@callback(this,@imgName,@num)
if !@result? then return cb()
@xmlTemplater.DocxGen.qrCode(@result,cb)
@getDataFromString(@result,cb)
......@@ -73,7 +73,7 @@ module.exports = class ImgManager
relationships.appendChild newTag
@setImage("word/_rels/#{@endFileName}.xml.rels",DocUtils.encode_utf8 DocUtils.xml2Str @xmlDoc)
@maxRid
getImageName:(id)->
getImageName:(id=0)->
nameCandidate="Copie_"+id+".png"
fullPath=@getFullPath(nameCandidate)
if @hasImage(fullPath)
......
DocUtils=require('docxtemplater').DocUtils
DocUtils=require('./docUtils')
DocxQrCode=require('./docxQrCode')
PNG=require('png-js')
JSZip=require('jszip')
module.exports= class ImgReplacer
constructor: (@xmlTemplater)->
constructor: (@xmlTemplater,@imgManager)->
@imgMatches=[]
@xmlTemplater.numQrCode=0
this
......@@ -20,15 +20,16 @@ module.exports= class ImgReplacer
@xmlTemplater.numQrCode+=@imgMatches.length
@replaceImage(match,u) for match,u in @imgMatches
this
imageSetter:(docxqrCode) ->
imageSetter:(docxqrCode) =>
if docxqrCode.callbacked==true then return
docxqrCode.callbacked=true
docxqrCode.xmlTemplater.numQrCode--
docxqrCode.xmlTemplater.imgManager.setImage("word/media/#{docxqrCode.imgName}",docxqrCode.data,{binary:true})
docxqrCode.xmlTemplater.DocxGen.qrCodeCallBack(docxqrCode.xmlTemplater.fileName+'-'+docxqrCode.num,false)
@imgManager.setImage("word/media/#{docxqrCode.imgName}",docxqrCode.data,{binary:true})
@popQrQueue(@imgManager.fileName+'-'+docxqrCode.num,false)
replaceImage:(match,u)->
num=@xmlTemplater.DocxGen.qrCodeNumCallBack
@xmlTemplater.DocxGen.qrCodeNumCallBack++
num=parseInt(Math.random()*10000)
# num=@xmlTemplater.DocxGen.qrCodeNumCallBack
# @xmlTemplater.DocxGen.qrCodeNumCallBack++
try
xmlImg= DocUtils.Str2xml '<?xml version="1.0" ?><w:document mc:Ignorable="w14 wp14" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape">'+match[0]+'</w:document>',(_i,type)->if _i=='fatalError' then throw "fatalError"
catch e
......@@ -36,15 +37,15 @@ module.exports= class ImgReplacer
tagrId= xmlImg.getElementsByTagName("a:blip")[0]
if tagrId==undefined then throw new Error('tagRiD undefined !')
rId = tagrId.getAttribute('r:embed')
oldFile= @xmlTemplater.imgManager.getImageByRid(rId)
oldFile= @imgManager.getImageByRid(rId)
tag= xmlImg.getElementsByTagName("wp:docPr")[0]
if tag==undefined then throw new Error('tag undefined')
if tag.getAttribute("name").substr(0,6)=="Copie_" then return #if image is already a replacement then do nothing
imgName= @xmlTemplater.imgManager.getImageName(@xmlTemplater.imageId)
@xmlTemplater.DocxGen.qrCodeCallBack(@xmlTemplater.fileName+'-'+num,true)
newId= @xmlTemplater.imgManager.addImageRels(imgName,"")
imgName= @imgManager.getImageName()
@pushQrQueue(@imgManager.fileName+'-'+num,true)
newId= @imgManager.addImageRels(imgName,"")
@xmlTemplater.imageId++
@xmlTemplater.imgManager.setImage(@xmlTemplater.imgManager.getFullPath(imgName),oldFile.data,{binary:true})
@imgManager.setImage(@imgManager.getFullPath(imgName),oldFile.data,{binary:true})
tag.setAttribute('name',"#{imgName}")
tagrId.setAttribute('r:embed',"rId#{newId}")
imageTag=xmlImg.getElementsByTagName('w:drawing')[0]
......@@ -52,7 +53,7 @@ module.exports= class ImgReplacer
replacement= DocUtils.xml2Str imageTag
@xmlTemplater.content= @xmlTemplater.content.replace(match[0], replacement)
if DocUtils.env=='browser'
@qr[u]= new DocxQrCode(oldFile.asBinary(),@xmlTemplater,imgName,num)
@qr[u]= new DocxQrCode(oldFile.asBinary(),@xmlTemplater,imgName,num,@getDataFromString)
@qr[u].decode(@imageSetter)
else
mockedQrCode={xmlTemplater:@xmlTemplater,imgName:imgName,data:oldFile.asBinary(),num:num}
......@@ -64,7 +65,7 @@ module.exports= class ImgReplacer
finished= (a) =>
png.decoded= a
try
@qr[u]= new DocxQrCode(png,@xmlTemplater,imgName,num)
@qr[u]= new DocxQrCode(png,@xmlTemplater,imgName,num,@getDataFromString)
@qr[u].decode(@imageSetter)
catch e
@imageSetter(mockedQrCode)
......
SubContent=require('docxtemplater').SubContent
ImgManager=require('./imgManager')
ImgReplacer=require('./imgReplacer')
fs=require('fs')
class ImageModule
constructor:(@options={})->
if !@options.centered? then @options.centered=false
@qrQueue=[]
@imageNumber=1
handleEvent:(event,eventData)->
if event=='rendering-file'
@renderingFileName=eventData
gen=@manager.getInstance('gen')
@imgManager=new ImgManager(gen.zip,@renderingFileName)
@imgManager.loadImageRels()
get:(data)->
if data=='loopType'
templaterState=@manager.getInstance('templaterState')
......@@ -34,36 +37,57 @@ class ImageModule
[150,150]
getImageFromData:(imgData)->
fs.readFileSync(imgData)
handle:(type,data)->
if type=='replaceTag' and data=='image'
scopeManager=@manager.getInstance('scopeManager')
templaterState=@manager.getInstance('templaterState')
replaceTag:->
scopeManager=@manager.getInstance('scopeManager')
templaterState=@manager.getInstance('templaterState')
tag = templaterState.textInsideTag.substr(1)
imgData=scopeManager.getValueFromScope(tag)
tag = templaterState.textInsideTag.substr(1)
imgData=scopeManager.getValueFromScope(tag)
tagXml=@manager.getInstance('xmlTemplater').tagXml
startEnd= "<#{tagXml}></#{tagXml}>"
if imgData=='undefined' then return @replaceBy(startEnd,tagXml)
try
imgBuffer=@getImageFromData(imgData)
catch e
return @replaceBy(startEnd,tagXml)
rId=@imgManager
.loadImageRels()
.addImageRels(@getNextImageName(),imgBuffer)
tagXml=@manager.getInstance('xmlTemplater').tagXml
startEnd= "<#{tagXml}></#{tagXml}>"
if imgData=='undefined' then return @replaceBy(startEnd,tagXml)
try
imgBuffer=@getImageFromData(imgData)
catch e
return @replaceBy(startEnd,tagXml)
rId=@imgManager
.loadImageRels()
.addImageRels(@getNextImageName(),imgBuffer)
sizePixel=@getSizeFromData(imgBuffer)
size=[@convertPixelsToEmus(sizePixel[0]),@convertPixelsToEmus(sizePixel[1])]
sizePixel=@getSizeFromData(imgBuffer)
size=[@convertPixelsToEmus(sizePixel[0]),@convertPixelsToEmus(sizePixel[1])]
if @options.centered==false
outsideElement=tagXml
newText=@getImageXml(rId,size)
if @options.centered==true
outsideElement=tagXml.substr(0,1)+':p'
newText=@getImageXmlCentered(rId,size)
if @options.centered==false
outsideElement=tagXml
newText=@getImageXml(rId,size)
if @options.centered==true
outsideElement=tagXml.substr(0,1)+':p'
newText=@getImageXmlCentered(rId,size)
@replaceBy(newText,outsideElement)
@replaceBy(newText,outsideElement)
replaceQr:->
xmlTemplater=@manager.getInstance('xmlTemplater')
imR=new ImgReplacer(xmlTemplater,@imgManager)
imR.getDataFromString=(result,cb)=>
cb(null,@getImageFromData(result))
imR.pushQrQueue=(num)=>
@qrQueue.push(num)
imR.popQrQueue=(num)=>
found = @qrQueue.indexOf(num)
if found!=-1
@qrQueue.splice(found)
else throw new Error("qrqueue #{num} is not in qrqueue")
if @qrQueue.length==0 then @finished()
imR
.findImages()
.replaceImages()
handle:(type,data)->
if type=='replaceTag' and data=='image'
@replaceTag()
if type=='xmlRendered' and @options.qrCode
@replaceQr()
null
getImageXml:(rId,size)->
return """
......
......@@ -6,6 +6,7 @@ fileNames=[
'imageExample.docx',
'imageLoopExample.docx',
'imageInlineExample.docx',
'qrExample.docx',
]
ImageModule=require('../js/index.js')
......@@ -121,6 +122,62 @@ describe 'image adding with {% image} syntax', ()->
buffer=zip.generate({type:"nodebuffer"})
fs.writeFile("test_multi.docx",buffer);
describe 'qrcode replacing',->
before (done)->
console.log 'before'
name='qrExample.docx'
imageModule=new ImageModule({qrCode:true})
imageModule.getImageFromData=(imgData)->
d=fs.readFileSync('examples/'+imgData,'binary')
d
imageModule.finished=()->
console.log 'finished'
zip=doc.getZip()
buffer=zip.generate({type:"nodebuffer"})
fs.writeFile("test_qr.docx",buffer);
done()
doc=docX[name]
.load(docX[name].loadedContent)
.setData({image:'image'})
doc.attachModule(imageModule)
doc
.render()
it 'shoud work with simple',->
it 'should be 1',->
expect(1).to.equal(1)
# imageFile=zip.files['word/media/image_generated_1.png']
# expect(imageFile?).to.equal(true)
# expect(imageFile.asText().length).to.equal(17417)
# imageFile2=zip.files['word/media/image_generated_2.png']
# expect(imageFile2?).to.equal(true)
# expect(imageFile2.asText().length).to.equal(7177)
# relsFile=zip.files['word/_rels/document.xml.rels']
# expect(relsFile?).to.equal(true)
# relsFileContent=relsFile.asText()
# expect(relsFileContent).to.equal("""<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\"><Relationship Id=\"rId1\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles\" Target=\"styles.xml\"/><Relationship Id=\"rId2\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering\" Target=\"numbering.xml\"/><Relationship Id=\"rId3\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings\" Target=\"settings.xml\"/><Relationship Id=\"rId4\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes\" Target=\"footnotes.xml\"/><Relationship Id=\"rId5\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/endnotes\" Target=\"endnotes.xml\"/><Relationship Id=\"hId0\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/header\" Target=\"header0.xml\"/><Relationship Id=\"rId6\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image\" Target=\"media/image_generated_1.png\"/><Relationship Id=\"rId7\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image\" Target=\"media/image_generated_2.png\"/></Relationships>""")
# documentFile=zip.files['word/document.xml']
# expect(documentFile?).to.equal(true)
# documentContent=documentFile.asText()
# # expect(documentContent).to.equal("""<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><w:document xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:wx=\"http://schemas.microsoft.com/office/word/2003/auxHint\" xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" xmlns:m=\"http://schemas.openxmlformats.org/officeDocument/2006/math\" xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"><w:body><w:p><w:pPr></w:pPr><w:r><w:rPr></w:rPr><w:t xml:space=\"preserve\"></w:t></w:r></w:p><w:p><w:pPr></w:pPr></w:p><w:p><w:pPr></w:pPr><w:r><w:rPr></w:rPr><w:drawing>\n <wp:inline distT=\"0\" distB=\"0\" distL=\"0\" distR=\"0\">\n <wp:extent cx=\"1905000\" cy=\"1905000\"/>\n <wp:effectExtent l=\"0\" t=\"0\" r=\"0\" b=\"0\"/>\n <wp:docPr id=\"2\" name=\"Image 2\" descr=\"description\"/>\n <wp:cNvGraphicFramePr>\n <a:graphicFrameLocks xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" noChangeAspect=\"1\"/>\n </wp:cNvGraphicFramePr>\n <a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">\n <a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">\n <pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">\n <pic:nvPicPr>\n <pic:cNvPr id=\"0\" name=\"Picture 1\" descr=\"description\"/>\n <pic:cNvPicPr>\n <a:picLocks noChangeAspect=\"1\" noChangeArrowheads=\"1\"/>\n </pic:cNvPicPr>\n </pic:nvPicPr>\n <pic:blipFill>\n <a:blip r:embed=\"rId6\">\n <a:extLst>\n <a:ext uri=\"{28A0092B-C50C-407E-A947-70E740481C1C}\">\n <a14:useLocalDpi xmlns:a14=\"http://schemas.microsoft.com/office/drawing/2010/main\" val=\"0\"/>\n </a:ext>\n </a:extLst>\n </a:blip>\n <a:srcRect/>\n <a:stretch>\n <a:fillRect/>\n </a:stretch>\n </pic:blipFill>\n <pic:spPr bwMode=\"auto\">\n <a:xfrm>\n <a:off x=\"0\" y=\"0\"/>\n <a:ext cx=\"1905000\" cy=\"1905000\"/>\n </a:xfrm>\n <a:prstGeom prst=\"rect\">\n <a:avLst/>\n </a:prstGeom>\n <a:noFill/>\n <a:ln>\n <a:noFill/>\n </a:ln>\n </pic:spPr>\n </pic:pic>\n </a:graphicData>\n </a:graphic>\n </wp:inline>\n</w:drawing></w:r></w:p><w:p><w:pPr></w:pPr></w:p><w:p><w:pPr></w:pPr><w:r><w:rPr></w:rPr><w:t xml:space=\"preserve\"></w:t></w:r></w:p><w:p><w:pPr></w:pPr></w:p><w:p><w:pPr></w:pPr><w:r><w:rPr></w:rPr><w:drawing>\n <wp:inline distT=\"0\" distB=\"0\" distL=\"0\" distR=\"0\">\n <wp:extent cx=\"1905000\" cy=\"1905000\"/>\n <wp:effectExtent l=\"0\" t=\"0\" r=\"0\" b=\"0\"/>\n <wp:docPr id=\"2\" name=\"Image 2\" descr=\"description\"/>\n <wp:cNvGraphicFramePr>\n <a:graphicFrameLocks xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" noChangeAspect=\"1\"/>\n </wp:cNvGraphicFramePr>\n <a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">\n <a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">\n <pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">\n <pic:nvPicPr>\n <pic:cNvPr id=\"0\" name=\"Picture 1\" descr=\"description\"/>\n <pic:cNvPicPr>\n <a:picLocks noChangeAspect=\"1\" noChangeArrowheads=\"1\"/>\n </pic:cNvPicPr>\n </pic:nvPicPr>\n <pic:blipFill>\n <a:blip r:embed=\"rId7\">\n <a:extLst>\n <a:ext uri=\"{28A0092B-C50C-407E-A947-70E740481C1C}\">\n <a14:useLocalDpi xmlns:a14=\"http://schemas.microsoft.com/office/drawing/2010/main\" val=\"0\"/>\n </a:ext>\n </a:extLst>\n </a:blip>\n <a:srcRect/>\n <a:stretch>\n <a:fillRect/>\n </a:stretch>\n </pic:blipFill>\n <pic:spPr bwMode=\"auto\">\n <a:xfrm>\n <a:off x=\"0\" y=\"0\"/>\n <a:ext cx=\"1905000\" cy=\"1905000\"/>\n </a:xfrm>\n <a:prstGeom prst=\"rect\">\n <a:avLst/>\n </a:prstGeom>\n <a:noFill/>\n <a:ln>\n <a:noFill/>\n </a:ln>\n </pic:spPr>\n </pic:pic>\n </a:graphicData>\n </a:graphic>\n </wp:inline>\n</w:drawing></w:r></w:p><w:p><w:pPr></w:pPr></w:p><w:p><w:pPr></w:pPr><w:r><w:rPr></w:rPr><w:t xml:space=\"preserve\"></w:t></w:r><w:bookmarkStart w:id=\"20\" w:name=\"_GoBack\"/><w:bookmarkEnd w:id=\"20\"/></w:p><w:sectPr><w:headerReference w:type=\"default\" r:id=\"hId0\"/><w:type w:val=\"continuous\"/><w:pgSz w:w=\"12240\" w:h=\"15840\" w:orient=\"portrait\"/><w:pgMar w:top=\"2810\" w:left=\"1800\" w:right=\"1800\" w:bottom=\"1440\"/><w:cols w:num=\"1\" w:sep=\"off\" w:equalWidth=\"1\"/></w:sectPr></w:body></w:document>""")
# it 'should work with inline images',()->
# name='imageInlineExample.docx'
......
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