fumiama / go-docx

One of the most functional libraries to partially read and write .docx files (a.k.a. Microsoft Word documents or ECMA-376 Office Open XML) in Go.
GNU Affero General Public License v3.0
109 stars 14 forks source link

.docx get corrupted after writing file #26

Open cyberpunx opened 4 months ago

cyberpunx commented 4 months ago

I'm not sure of what's happening here. I have a perfectly functioning .docx as input file. But after parse it (didn't mody a thing) and write it as new file.. the output file gets corrupted)

func OpenDocx(path string) *docx.Docx {
    readFile, err := os.Open(path)
    util.Panic(err)
    fileInfo, err := readFile.Stat()
    util.Panic(err)
    size := fileInfo.Size()
    doc, err := docx.Parse(readFile, size)
    util.Panic(err)
    return doc
}

func WriteDocx(doc *docx.Docx, path string) {
    f, err := os.Create(path)
    util.Panic(err)
    _, err = doc.WriteTo(f)
    util.Panic(err)
    err = f.Close()
    util.Panic(err)
}

and somwhere in my main.go

...
...
doc := docxLib.OpenDocx(inputFile)
docxLib.WriteDocx(doc, "copy.docx")
...
...

when I try to open copy.docx in Word, I cannot because of corrupted file.

fumiama commented 4 months ago

It is a possible problem if there are some complicated elements in your docx file because it's impossible for me to test every situation. You can delete a few elements in that file and run this code again to find out where the problem really coming from. Then you can fix it or put more detail to this issue to certain it.

cyberpunx commented 4 months ago

Ok, that makes sense. After what you said, I tried a simplier docx and had no issue, so definitely is something in my original docx, which is fairly complicated... it has tables, differente footers, headers, images and what not. I'll inform if I found the conflicting element. Ty for your answer!