Commit 2edab7f2 by Bernhard Matz

don't try to call addImageRels if loadImageRels() result is falsy,

added zip.files fallback to "word/_rels/document.xml.rels",
added test for image replacement in header/footer
parent ba9ab98f
......@@ -22,7 +22,7 @@ module.exports = class ImgManager
hasImage:(fileName)->
@zip.files[fileName]?
loadImageRels: () ->
file=@zip.files["word/_rels/#{@endFileName}.xml.rels"]
file=@zip.files["word/_rels/#{@endFileName}.xml.rels"] || @zip.files["word/_rels/document.xml.rels"]
if file==undefined then return
content= DocUtils.decode_utf8 file.asText()
@xmlDoc= DocUtils.Str2xml content
......
......@@ -53,21 +53,21 @@ class ImageModule
imgBuffer=@getImageFromData(imgData)
catch e
return @replaceBy(startEnd,tagXml)
rId=@imgManager
.loadImageRels()
.addImageRels(@getNextImageName(),imgBuffer)
imageRels=@imgManager.loadImageRels();
if imageRels
rId=imageRels.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)
......
......@@ -6,6 +6,7 @@ fileNames=[
'imageExample.docx',
'imageLoopExample.docx',
'imageInlineExample.docx',
'imageHeaderFooterExample.docx',
'qrExample.docx',
'noImage.docx',
'qrExample2.docx',
......@@ -123,7 +124,52 @@ 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})
docX[name].attachModule(imageModule)
out=docX[name]
.load(docX[name].loadedContent)
.setData({image:'examples/image.png'})
.render()
zip=out.getZip()
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(17417)
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"?>
<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/header" Target="header1.xml"/><Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer" Target="footer1.xml"/><Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable" Target="fontTable.xml"/><Relationship Id="rId5" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings" Target="settings.xml"/>
</Relationships>""")
headerRelsFile=zip.files['word/_rels/header1.xml.rels']
expect(headerRelsFile?).to.equal(true)
headerRelsFileContent=headerRelsFile.asText()
expect(headerRelsFileContent).to.equal("""<?xml version="1.0" encoding="UTF-8"?><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/header" Target="header1.xml"/><Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer" Target="footer1.xml"/><Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable" Target="fontTable.xml"/><Relationship Id="rId5" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings" Target="settings.xml"/>
<Relationship Id="rId6" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image_generated_2.png"/></Relationships>""")
footerRelsFile=zip.files['word/_rels/footer1.xml.rels']
expect(footerRelsFile?).to.equal(true)
footerRelsFileContent=footerRelsFile.asText()
expect(footerRelsFileContent).to.equal("""<?xml version="1.0" encoding="UTF-8"?><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/header" Target="header1.xml"/><Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer" Target="footer1.xml"/><Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable" Target="fontTable.xml"/><Relationship Id="rId5" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings" Target="settings.xml"/>
<Relationship Id="rId6" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image_generated_1.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: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:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"><w:body><w:p><w:pPr><w:pStyle w:val="Normal"/><w:rPr></w:rPr></w:pPr><w:r><w:rPr></w:rPr></w:r></w:p><w:sectPr><w:headerReference w:type="default" r:id="rId2"/><w:footerReference w:type="default" r:id="rId3"/><w:type w:val="nextPage"/><w:pgSz w:w="12240" w:h="15840"/><w:pgMar w:left="1800" w:right="1800" w:header="720" w:top="2810" w:footer="1440" w:bottom="2003" w:gutter="0"/><w:pgNumType w:fmt="decimal"/><w:formProt w:val="false"/><w:textDirection w:val="lrTb"/><w:docGrid w:type="default" w:linePitch="249" w:charSpace="2047"/></w:sectPr></w:body></w:document>""")
fs.writeFile("test_header_footer.docx",zip.generate({type:"nodebuffer"}));
describe 'qrcode replacing',->
describe 'shoud work without loops',->
......
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