Documentation
¶
Overview ¶
Package odt generates OpenDocument Text (.odt) files.
ODT is the open standard document format (ISO/IEC 26300) used by LibreOffice, OpenOffice, Google Docs (import/export), and many other applications. This package produces ODF 1.2 compliant files that open cleanly in all major word processors.
Zero dependencies — uses only the Go standard library.
Quick start:
doc := odt.New()
doc.SetTitle("My Document")
doc.SetAuthor("Jane Doe")
doc.AddHeading(1, "Introduction")
doc.AddParagraph("Hello, world!")
data, err := doc.Render()
Index ¶
- type Document
- func (d *Document) AddBlockquote(content ...any)
- func (d *Document) AddCodeBlock(text string)
- func (d *Document) AddHeading(level int, text string)
- func (d *Document) AddHorizontalRule()
- func (d *Document) AddList(items ...any)
- func (d *Document) AddOrderedList(items ...any)
- func (d *Document) AddPageBreak()
- func (d *Document) AddParagraph(content ...any)
- func (d *Document) AddTable(headers []string, rows [][]string)
- func (d *Document) Render() ([]byte, error)
- func (d *Document) SetAuthor(author string)
- func (d *Document) SetFont(family string, sizePt int)
- func (d *Document) SetGenerator(gen string)
- func (d *Document) SetLineSpacing(spacing float64)
- func (d *Document) SetMargins(top, bottom, left, right string)
- func (d *Document) SetTitle(title string)
- func (d *Document) WriteTo(w io.Writer) (int64, error)
- type Span
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Document ¶
type Document struct {
// contains filtered or unexported fields
}
Document represents an ODT document under construction. Create one with New, add content with the Add methods, then call Document.Render to produce the ODT bytes.
func New ¶
func New() *Document
New creates a new empty ODT document with sensible defaults.
Example ¶
doc := odt.New()
doc.SetTitle("My Document")
doc.SetAuthor("Jane Doe")
doc.AddHeading(1, "Introduction")
doc.AddParagraph("Hello, world!")
data, err := doc.Render()
if err != nil {
panic(err)
}
fmt.Println(len(data) > 0)
Output: true
func (*Document) AddBlockquote ¶
AddBlockquote adds a block quotation. Content arguments can be plain strings or Span values.
func (*Document) AddCodeBlock ¶
AddCodeBlock adds a preformatted code block.
func (*Document) AddHeading ¶
AddHeading adds a heading at the given outline level (1–6).
func (*Document) AddHorizontalRule ¶
func (d *Document) AddHorizontalRule()
AddHorizontalRule adds a centered separator (e.g., "* * *").
func (*Document) AddList ¶
AddList adds a bullet list. Each item can be a string, a Span, or a []any of mixed strings and Spans.
func (*Document) AddOrderedList ¶
AddOrderedList adds a numbered list. Arguments are the same as Document.AddList.
func (*Document) AddPageBreak ¶
func (d *Document) AddPageBreak()
AddPageBreak inserts a page break before the next content.
func (*Document) AddParagraph ¶
AddParagraph adds a paragraph. Content arguments can be plain strings or Span values for inline formatting.
doc.AddParagraph("Hello, ", odt.Bold("world"), "!")
Example ¶
doc := odt.New()
doc.AddParagraph("Plain text, ", odt.Bold("bold"), ", and ", odt.Text("bold italic").Bold().Italic(), ".")
doc.Render()
func (*Document) SetGenerator ¶
SetGenerator overrides the generator string in metadata (defaults to "go-odt").
func (*Document) SetLineSpacing ¶
SetLineSpacing sets line height as a multiplier (e.g., 1.5 for 150%).
func (*Document) SetMargins ¶
SetMargins sets page margins. Values use CSS-style units (e.g., "1in", "2.54cm").
func (*Document) WriteTo ¶
WriteTo writes the complete ODT file to w.
Example ¶
doc := odt.New()
doc.AddParagraph("Written to a file.")
f, err := os.CreateTemp("", "example-*.odt")
if err != nil {
panic(err)
}
defer os.Remove(f.Name())
defer f.Close()
n, err := doc.WriteTo(f)
if err != nil {
panic(err)
}
fmt.Println(n > 0)
Output: true
type Span ¶
type Span struct {
// contains filtered or unexported fields
}
Span represents a run of inline text with optional formatting. Create spans with the constructor functions (Text, Bold, Italic, etc.) and combine formats with the chainable methods.
odt.Bold("important") // bold
odt.Text("key point").Bold().Italic() // bold + italic
odt.Link("CivNode", "https://civnode.com") // hyperlink
func Link ¶
Link creates a hyperlink span.
Example ¶
doc := odt.New()
doc.AddParagraph("Visit ", odt.Link("CivNode", "https://civnode.com"), " for writing.")
doc.Render()
func Strikethrough ¶
Strikethrough creates a struck-through text span.
func (Span) Strikethrough ¶
Strikethrough returns a copy of the span with strikethrough enabled.