cetak

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 2, 2020 License: MIT Imports: 5 Imported by: 0

README

Cetak

Golang package that helps you generate Word document (.docx) from a template. It utilises go's text/template package behind the scene so you can use the same annotation in your template.

Usage

To use this package, simply create new Docx object and call Generate with your data dan destination path.

...
d, err := cetak.New("/path/to/template.docx")
if err != nil {
    // handle error
}

type TemplateData struct {
    Title   string
	Content string
}

data := TemplateData{
    Title:   "This is The Title",
    Content: "Hello World!",
}

err = d.Generate(data, "/path/to/result.docx")
if err != nil {
    // handle error
}
...

In the above sample, we assume to have a template ready in /path/to/template.docx. We will inject data which has an arbitrary TemplateData type. Note that the data type can be anything where the fields correspond to annotations inside the template. In this sample, template.docx has {{.Title}} and {{.Content}} inside of it. The result will be stored in the given result path, ie /path/to/result.docx.

License

This package is licensed under MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Docx

type Docx interface {
	// Generate the document using given data.
	// The result will be written to the given destination path.
	Generate(data interface{}, destination string) error
}

Docx is the interface used to interact with cetak's doc generator

func New

func New(templatePath string) (Docx, error)

New creates a new Docx object. It will return error if template file could not be opened.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL