godtemplate

package module
v0.0.0-...-2c1e887 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2025 License: MIT Imports: 10 Imported by: 0

README

gODTemplate

A Go library for manipulating OpenDocumentText files

Features

  • Render invoices from ODT templates
  • Supports JSON and base64 encoded data
  • Provides a CLI tool and a server implementation
  • Can be used as a library in Go applications
  • Docker image available for easy deployment

Usage

As Library
package main

import (
	"github.com/mheers/godtemplate/invoicerenderer"
)

func main() {
	err := invoicerenderer.RenderInvoice(
		"templates/template.odt",
		invoicerenderer.Invoice{
			Salutation:     "Mr.",
			Name:           "John Doe",
			Street:         "123 Main St",
			ZIP:            "12345",
			City:           "Anytown",
			DocumentType:   "Invoice",
			DocumentNumber: "1000251",
			DocumentDate:   "2025-05-01",
			CustomerNumber: "C123456",
			Net:            36.00,
			VATRate:        12.0,
			VAT:            4.32,
			Total:          40.32,
			DueDate:        "2025-06-01",
			TableName:      "Listing",
		},
		[]invoicerenderer.InvoiceItem{
			{Description: "Water", Quantity: 2, Unit: "L", UnitPrice: 15.00, TotalPrice: 30.00},
			{Description: "Shoes", Quantity: 3, Unit: "pcs", UnitPrice: 2.00, TotalPrice: 6.00},
		},
		"/tmp/output_invoice.odt",
	)

	if err != nil {
		panic(err)
	}
}
As CLI Tool
godtemplate render --template templates/template.odt --output /tmp/output_invoice.odt --invoice $(cat example/data.json| jq .invoice -r | base64 -w 0) --items $(cat example/data.json | jq .items -r | base64 -w 0)
As Docker Image
docker run --rm -v $(pwd):/data mheers/godtemplate render --template /data/templates/template.odt --output /data/output_invoice.odt --invoice <base64-encoded-invoice-json> --items <base64-encoded-items-json>

Instead of odt, you can also specify pdf as output format. The tool will convert the ODT file to PDF using LibreOffice. This requires LibreOffice to be installed on your system. LibreOffice is already included in the Docker image.

As Server

You can also run the tool as a server that provides HTTP endpoints to render invoices as PDF documents from ODT templates.

For detailed instructions on how to run the server, see the SERVER.md file.

License

This project is licensed under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertODTToPDF

func ConvertODTToPDF(odtPath, pdfPath string) error

Types

type Replacer

type Replacer struct{}

func (*Replacer) BackupLastXRows

func (r *Replacer) BackupLastXRows(table *etree.Element, x int) []*etree.Element

Backup last X rows from the table

func (*Replacer) CleanXMLTemplate

func (r *Replacer) CleanXMLTemplate(xml string) string

func (*Replacer) GetCell

func (r *Replacer) GetCell(doc *etree.Document, value, tableStyle, textStyle string) *etree.Element

Create a cell with styled paragraphs

func (*Replacer) GetDocument

func (r *Replacer) GetDocument(zipReader *zip.ReadCloser) (*etree.Document, *zip.File, error)

Read and parse content.xml

func (*Replacer) GetStylesOfRow

func (r *Replacer) GetStylesOfRow(row *etree.Element) []TableEntryStyle

func (*Replacer) GetTableElement

func (r *Replacer) GetTableElement(doc *etree.Document, name string) *etree.Element

Find a table by name

func (*Replacer) OpenFile

func (r *Replacer) OpenFile(fileName string) (*zip.ReadCloser, error)

Open the .odt/.zip file

func (*Replacer) ReinsertRows

func (r *Replacer) ReinsertRows(table *etree.Element, rows []*etree.Element)

Reinsert previously backed up rows

func (*Replacer) ReplaceValues

func (r *Replacer) ReplaceValues(xml string, mapping [][2]string) string

Replace values in XML string using a key/value mapping

func (*Replacer) TableInsert

func (r *Replacer) TableInsert(doc *etree.Document, tableElement *etree.Element, values []string, designValues []TableEntryStyle)

Add a new row to the table

func (*Replacer) WriteContent

func (r *Replacer) WriteContent(srcZipPath, dstZipPath string, xmlContent string) error

Write new content.xml into a new zip file

type TableEntryStyle

type TableEntryStyle struct {
	CellStyle string
	TextStyle string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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