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 ¶
- Variables
- func Contains(a []string, x string) bool
- func ConvertFromByte(data []byte, result interface{}) error
- func ConvertToByte(data interface{}) ([]byte, error)
- func ToBoolean(value interface{}, defValue bool) bool
- func ToFloat(value interface{}, defValue float64) float64
- func ToInteger(value interface{}, defValue int64) int64
- func ToRGBA(value interface{}, defValue color.RGBA) color.RGBA
- func ToString(value interface{}, defValue string) string
- type Barcode
- type Cell
- type Column
- type Datagrid
- type ElementPropertySetter
- type Generator
- type HLine
- type HTML
- type IM
- type Image
- type PageItem
- type Report
- func (rpt *Report) AppendElement(options ...interface{}) (*[]PageItem, error)
- func (rpt *Report) CreateReport() bool
- func (rpt *Report) LoadJSONDefinition(jsonString string) error
- func (rpt *Report) Save2DataURLString(filename string) (string, error)
- func (rpt *Report) Save2Pdf() ([]byte, error)
- func (rpt *Report) Save2PdfFile(filename string) error
- func (rpt *Report) Save2Xml() string
- func (rpt *Report) SetData(key string, value interface{}) (bool, error)
- func (rpt *Report) SetReportValue(fieldname string, value interface{}) error
- type Row
- type SM
- type Separator
- type VGap
Constants ¶
This section is empty.
Variables ¶
var Fonts embed.FS
Functions ¶
func ConvertFromByte ¶
func ConvertToByte ¶
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"`
}
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"`
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 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 ¶
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 ¶
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 ¶
CreateReport - the report template processing, databind replacement.
func (*Report) LoadJSONDefinition ¶
LoadJSONDefinition load to the report an JSON definition.
func (*Report) Save2DataURLString ¶
Save2DataURLString creates a base64 data URI scheme.
func (*Report) Save2PdfFile ¶
Save2PdfFile creates or truncates the file specified by fileStr and writes the PDF document to it.
func (*Report) Save2Xml ¶
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 ¶
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 ¶
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.