Giter Site home page Giter Site logo

How to convert to []byte about go-docx HOT 4 CLOSED

shehuaqigai avatar shehuaqigai commented on May 22, 2024
How to convert to []byte

from go-docx.

Comments (4)

shehuaqigai avatar shehuaqigai commented on May 22, 2024 1

Thank you very much!

from go-docx.

lukasjarosch avatar lukasjarosch commented on May 22, 2024

Could you please be a bit more elaborate on what you are trying to do?

from go-docx.

shehuaqigai avatar shehuaqigai commented on May 22, 2024

wordDocx, docxErr := docx.Open(docxPath)
if docxErr != nil {
ctx.StopWithStatus(500)
return
}

		replaceMap := docx.PlaceholderMap{
			"year":  "2024",
			"month": "04",
			"day":   "03",
		}
		// replace the keys with values from replaceMap
		docxErr = wordDocx.ReplaceAll(replaceMap)
		if docxErr != nil {
			ctx.StopWithStatus(500)
			return
		}
		ctx.Header("Content-Type", "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
		ctx.Header("Content-Disposition", "attachment; filename=example.docx")

		ctx.Write([]byte)//iris.go  I need to type directly back to the file stream, not write to the file

from go-docx.

lukasjarosch avatar lukasjarosch commented on May 22, 2024

Ahh, i see. Thanks for elaborating.

This should already be possible. You can use document.Write for that. Something like this should work:

// ...
out := []bytes{}
buf := bytes.NewBuffer(out)
err = wordDocx.Write(buf)

// now you can continue work with just the byte slice of the rendered document
ctx.Write(buf.Bytes())

from go-docx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.