report

package module
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2026 License: GPL-3.0 Imports: 26 Imported by: 1

README

Go PDF report generation library

GoDoc

  • Fully declarative: can be easily modified and used for relative layout (no need to specify the x and y coordinates)
  • Powerful layout engine: row, datagrid, column, cell, image, separator, html, barcode, hline, vgap elements
  • Creating a PDF from a JSON template or GO language code
Quick start

example/example.go

Documentation

Overview

Go PDF report generation library

  • Fully declarative: can be easily modified and used for relative layout (no need to specify the x and y coordinates)
  • Powerful layout engine: row, datagrid, column, cell, image, separator, html, barcode, hline, vgap elements
  • Creating a PDF from a JSON template or GO language code

Quick start: example/example.go

Index

Constants

This section is empty.

Variables

View Source
var Fonts embed.FS

Functions

func Contains

func Contains(a []string, x string) bool

Contains tells whether a contains x.

func ConvertFromByte

func ConvertFromByte(data []byte, result interface{}) error

func ConvertToByte

func ConvertToByte(data interface{}) ([]byte, error)

func ToBoolean

func ToBoolean(value interface{}, defValue bool) bool

ToBoolean - safe bool conversion

func ToFloat

func ToFloat(value interface{}, defValue float64) float64

ToFloat - safe float64 conversion

func ToInteger

func ToInteger(value interface{}, defValue int64) int64

ToInteger - safe int64 conversion

func ToRGBA

func ToRGBA(value interface{}, defValue color.RGBA) color.RGBA

ToRGBA - safe RGBA conversion

func ToString

func ToString(value interface{}, defValue string) string

ToString - safe string conversion

Types

type Barcode

type Barcode struct {
	CodeType     string  `xml:"code-type,attr" json:"code-type"`
	Value        string  `xml:"value,attr" json:"value"`
	VisibleValue bool    `xml:"visible-value,attr" json:"visible-value"`
	Width        float64 `xml:"wide,attr" json:"wide"`
	Height       float64 `xml:"narrow,attr" json:"narrow"`
	Extend       bool    `xml:"extend,attr" json:"extend"`
}

Barcode - Row unit

type Cell

type Cell struct {
	Name            string     `xml:"name,attr" json:"name"`
	Value           string     `xml:"value,attr" json:"value"`
	Width           string     `xml:"width,attr" json:"width"`
	Border          string     `xml:"border,attr" json:"border"`
	Align           string     `xml:"align,attr" json:"align"`
	Multiline       bool       `xml:"multiline,attr" json:"multiline"`
	FontStyle       string     `xml:"font-style,attr" json:"font-style"`
	FontSize        float64    `xml:"font-size,attr" json:"font-size"`
	TextColor       color.RGBA `xml:"color,attr" json:"color"`
	BorderColor     color.RGBA `xml:"border-color,attr" json:"border-color"`
	BackgroundColor color.RGBA `xml:"background-color,attr" json:"background-color"`
}

Cell - Row unit

type Column

type Column struct {
	Fieldname   string `xml:"fieldname,attr" json:"fieldname"`
	Label       string `xml:"label,attr" json:"label"`
	Width       string `xml:"width,attr" json:"width"`
	Align       string `xml:"align,attr" json:"align"`
	HeaderAlign string `xml:"header-align,attr" json:"header-align"`
	FooterAlign string `xml:"footer-align,attr" json:"footer-align"`
	Footer      string `xml:"footer,attr" json:"footer"`
}

Column - Datagrid unit

type Datagrid

type Datagrid struct {
	Name             string     `xml:"name,attr" json:"name"`
	Databind         string     `xml:"databind,attr" json:"databind"`
	Width            string     `xml:"width,attr" json:"width"`
	Merge            bool       `xml:"merge,attr" json:"merge"`
	Border           string     `xml:"border,attr" json:"border"`
	FontSize         float64    `xml:"font-size,attr" json:"font-size"`
	TextColor        color.RGBA `xml:"color,attr" json:"color"`
	BorderColor      color.RGBA `xml:"border-color,attr" json:"border-color"`
	BackgroundColor  color.RGBA `xml:"background-color,attr" json:"background-color"`
	HeaderBackground color.RGBA `xml:"header-background,attr" json:"header-background"`
	FooterBackground color.RGBA `xml:"footer-background,attr" json:"footer-background"`
	Columns          []PageItem `xml:"columns" json:"columns"`
}

Datagrid - Create a table from a data list.

type ElementPropertySetter

type ElementPropertySetter func(item interface{}, value interface{}) error

ElementPropertySetter sets a property on an element. Used by the element registry.

type Generator

type Generator interface {
	Init(rpt *Report)
	GetPageSize() (width, height float64)
	PageNo() int
	AddPage()
	AddImage(image *Image, x, y float64, options IM)
	LoadImage(img image.Image, x, y, h, w float64) error
	AddFont(familyStr, styleStr, fileStr string, rd io.Reader)
	GetFontSize() (ptSize float64)
	SetFont(familyStr, styleStr string, size float64)
	SetFontSize(size float64)
	GetTextWidth(s string) float64
	SetDrawColor(r, g, b int)
	SetFillColor(r, g, b int)
	SetTextColor(r, g, b int)
	SetProperties(rpt *Report)
	Text(txtStr string, pageBreak float64)
	Rect(x, y, w, h float64, styleStr string)
	Line(x1, y1, x2, y2 float64)
	GetX() float64
	GetY() float64
	SetX(x float64)
	SetY(y float64)
	SetXY(x, y float64)
	SetText(x, y float64, value string) error
	Ln(h float64)
	Cell(options IM)
	MultiCell(options IM)
	Save2Pdf() ([]byte, error)
	Save2PdfFile(filename string) error
}

Generator the PDF generator interface

type HLine

type HLine struct {
	Width       string     `xml:"width,attr" json:"width"`
	Gap         float64    `xml:"gap,attr" json:"gap"`
	BorderColor color.RGBA `xml:"border-color,attr" json:"border-color"`
}

HLine - a horizontal line.

type HTML

type HTML struct {
	Fieldname string `xml:"fieldname,attr" json:"fieldname"`
	Value     string `xml:",cdata" json:"html"`
}

HTML - a basic HTML elements rendering.

type IM

type IM = map[string]interface{}

IM is a map[string]interface{} type short alias

type Image

type Image struct {
	Src       string  `xml:"src,attr" json:"src"`
	Data      []byte  `xml:"data,attr" json:"data"`
	MaxWidth  float64 `xml:"max-width,attr" json:"max-width"`
	MaxHeight float64 `xml:"max-height,attr" json:"max-height"`
	Height    float64 `xml:"height,attr" json:"height"`
	Width     float64 `xml:"width,attr" json:"width"`
}

Image - Row unit

type PageItem

type PageItem struct {
	ItemType string
	Item     interface{}
}

PageItem - element interface wrapper

type Report

type Report struct {
	Pdf Generator

	Title           string     `xml:"title,attr" json:"title"`
	Author          string     `xml:"author,attr" json:"author"`
	Creator         string     `xml:"creator,attr" json:"creator"`
	Subject         string     `xml:"subject,attr" json:"subject"`
	Keywords        string     `xml:"keywords,attr" json:"keywords"`
	LeftMargin      float64    `xml:"left-margin,attr" json:"left-margin"`
	RightMargin     float64    `xml:"right-margin,attr" json:"right-margin"`
	TopMargin       float64    `xml:"top-margin,attr" json:"top-margin"`
	BottomMargin    float64    `xml:"bottom-margin,attr" json:"bottom-margin"`
	FontFamily      string     `xml:"font-family,attr" json:"font-family"`
	FontStyle       string     `xml:"font-style,attr" json:"font-style"`
	FontSize        float64    `xml:"font-size,attr" json:"font-size"`
	TextColor       color.RGBA `xml:"color,attr" json:"color"`
	BorderColor     color.RGBA `xml:"border-color,attr" json:"border-color"`
	BackgroundColor color.RGBA `xml:"background-color,attr" json:"background-color"`
	ImagePath       string     `xml:"image-path,attr" json:"image-path"`
	// contains filtered or unexported fields
}

Report is the principal structure for creating a single PDF document

func New

func New(options ...string) (rpt *Report)

New returns a pointer to a new Report instance. Options:

  • orientation - Optional. Default value:"P" Values: "P","portrait","L","landscape".
  • format - Optional. Defaut value: "A4" Values: "A3","A4","A5","letter","legal".
  • fontFamily - Optional Default: Cabin
  • fontDir - Optional Default: ""

Example:

rpt := report.New("P", "A4")

func (*Report) AppendElement

func (rpt *Report) AppendElement(options ...interface{}) (*[]PageItem, error)

AppendElement - Append an element in the template.

  • parent - Optional. The parent elemnt. Values: "header","details","footer" or result value (row, datagrid) Default value: "details"
  • ename - Optional. An Element type: "row", "datagrid", "vgap", "hline", "html", "column", "cell", "image", "separator", "barcode". Default value: "row"
  • values - Optional. Element attributes

Example:

row_data := rpt.AppendElement("header", "row", map[string]interface{}{"height": 10})
rpt.AppendElement(row_data, "image", map[string]interface{}{"src": "test/logo.jpg"})

func (*Report) CreateReport

func (rpt *Report) CreateReport() bool

CreateReport - the report template processing, databind replacement.

func (*Report) LoadJSONDefinition

func (rpt *Report) LoadJSONDefinition(jsonString string) error

LoadJSONDefinition load to the report an JSON definition.

func (*Report) Save2DataURLString

func (rpt *Report) Save2DataURLString(filename string) (string, error)

Save2DataURLString creates a base64 data URI scheme.

func (*Report) Save2Pdf

func (rpt *Report) Save2Pdf() ([]byte, error)

Save2Pdf creates a PDF output.

func (*Report) Save2PdfFile

func (rpt *Report) Save2PdfFile(filename string) error

Save2PdfFile creates or truncates the file specified by fileStr and writes the PDF document to it.

func (*Report) Save2Xml

func (rpt *Report) Save2Xml() string

Save2Xml creates an XML output. Only the values of cells and datagrid rows from header and details. The node name of the cell name (except when name="label"), or datagrid name/column fieldname.

func (*Report) SetData

func (rpt *Report) SetData(key string, value interface{}) (bool, error)

SetData - Set the template data. Parameters:

  • key - string
  • value - interface{} Valid interface type: string or dictonary (map[string]string) or record list ([]map[string]string)

Example:

rpt.SetData("items_footer", map[string]string{"items_total": "3 703 680"})

func (*Report) SetReportValue

func (rpt *Report) SetReportValue(fieldname string, value interface{}) error

SetReportValue - You can set the Report properties safely and type independent.

type Row

type Row struct {
	Height  float64    `xml:"height,attr" json:"height"`
	HGap    float64    `xml:"hgap,attr" json:"hgap"`
	Visible string     `xml:"visible,attr" json:"visible"`
	Columns []PageItem `xml:"columns,attr" json:"columns"`
}

Row - Horizontal logical group. The last element width extends up to the right margin.

type SM

type SM = map[string]string

SM is a map[string]string type short alias

type Separator

type Separator struct {
	Gap float64 `xml:"gap,attr" json:"gap"`
}

Separator - Row unit, A horizontal separator line.

type VGap

type VGap struct {
	Height    float64 `xml:"height,attr" json:"height"`
	PageBreak bool    `xml:"page-break,attr" json:"page-break"`
}

VGap - a vertical gap.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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