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 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"` //Values: "CODE_39"/"code39", "ITF"/"i2of5", "CODE_128"/"code128", "EAN"/"ean", "QR"/"qr"
Value string `xml:"value,attr" json:"value"` //barcode text value
VisibleValue bool `xml:"visible-value,attr" json:"visible-value"` //show or not the value of text
Width float64 `xml:"wide,attr" json:"wide"` //barcode width (default width of the value string + padding)
Height float64 `xml:"narrow,attr" json:"narrow"` //barcode height (default 10).
Extend bool `xml:"extend,attr" json:"extend"` //barcode width extends up to the right margin (default false)
}
Barcode - Row unit
type Cell ¶
type Cell struct {
Name string `xml:"name,attr" json:"name"` //XML output node name
Value string `xml:"value,attr" json:"value"` //static text or databind value
Width string `xml:"width,attr" json:"width"` //number or percent value (e.g. "10" or "10%")
Border string `xml:"border,attr" json:"border"` //values: "0"(no border, default), "1"(all) or some or all of the following characters: "L"(left), "T"(top), "R"(right),"B"(bottom)
Align string `xml:"align,attr" json:"align"` //values: "L" (default) or "left", "R" or "right", "C" or "center"
Multiline bool `xml:"multiline,attr" json:"multiline"` //if true, print text with line breaks (default false)
FontStyle string `xml:"font-style,attr" json:"font-style"` //values: "" (default), "bold", "italic", "bolditalic"
FontSize float64 `xml:"font-size,attr" json:"font-size"` //Default value: Report.FontSize
TextColor color.RGBA `xml:"color,attr" json:"color"` //JSON or XML value: in hexadecimal (e.g. #A0522D) or in decimal (e.g 10506797), default "black"
BorderColor color.RGBA `xml:"border-color,attr" json:"border-color"` //JSON or XML value: integer gray color (in range from 0 "black" to 255 "white"), default "black"
BackgroundColor color.RGBA `xml:"background-color,attr" json:"background-color"` //JSON or XML value: integer gray color (in range from 0 "black" to 255 "white"), default "black"
}
Cell - Row unit
type Column ¶
type Column struct {
Fieldname string `xml:"fieldname,attr" json:"fieldname"` //datasource dictonary key (special value: "counter")
Label string `xml:"label,attr" json:"label"` //Column caption
Width string `xml:"width,attr" json:"width"` //number or percent value (e.g. "10" or "10%")
Align string `xml:"align,attr" json:"align"` //values: "L" (default) or "left", "R" or "right", "C" or "center"
HeaderAlign string `xml:"header-align,attr" json:"header-align"` //values: "L" (default) or "left", "R" or "right", "C" or "center"
}
Column - Datagrid unit
type Datagrid ¶
type Datagrid struct {
Name string `xml:"name,attr" json:"name"` //XML output node name
Databind string `xml:"databind,attr" json:"databind"` //table data source name
Width string `xml:"width,attr" json:"width"` //number or percent value (e.g. "10" or "10%")
Merge bool `xml:"merge,attr" json:"merge"` //if true then all fields will be displayed in a single column (default false)
Border string `xml:"border,attr" json:"border"` //values: "0"(no border, default), "1"(all) or some or all of the following characters: "L"(left), "T"(top), "R"(right),"B"(bottom)
FontSize float64 `xml:"font-size,attr" json:"font-size"` //Default value: Report.FontSize
TextColor color.RGBA `xml:"color,attr" json:"color"` //JSON or XML value: in hexadecimal (e.g. #A0522D) or in decimal (e.g 10506797), default "black"
BorderColor color.RGBA `xml:"border-color,attr" json:"border-color"` //JSON or XML value: integer gray color (in range from 0 "black" to 255 "white"), default "black"
BackgroundColor color.RGBA `xml:"background-color,attr" json:"background-color"` //JSON or XML value: integer gray color (in range from 0 "black" to 255 "white"), default "black"
HeaderBackground color.RGBA `xml:"header-background,attr" json:"header-background"` //JSON or XML value: integer gray color (in range from 0 "black" to 255 "white"), default "black"
Columns []PageItem `xml:"columns" json:"columns"` //columns list of the datagrid
}
Datagrid - Create a table from a data list.
type Generator ¶
type Generator interface {
Init(rpt *Report)
// GetPageSize returns the current page's width and height.
GetPageSize() (width, height float64)
// PageNo returns the current page number.
PageNo() int
// AddPage adds a new page to the document
AddPage()
// AddImage draws a image
AddImage(image *Image, x, y float64, options IM)
LoadImage(img image.Image, x, y, h, w float64) error
// AddFont imports a font and makes it available
AddFont(familyStr, styleStr, fileStr string, rd io.Reader)
// GetFontSize returns the size of the current font in points.
GetFontSize() (ptSize float64)
// SetFont sets the font used to print character strings
SetFont(familyStr, styleStr string, size float64)
// SetFontSize defines the size of the current font.
SetFontSize(size float64)
// GetTextWidth returns the length of a string in user units.
GetTextWidth(s string) float64
// SetDrawColor defines the color used for all drawing operations
SetDrawColor(r, g, b int)
// SetFillColor defines the color used for all filling operations
SetFillColor(r, g, b int)
// SetTextColor defines the color used for text.
SetTextColor(r, g, b int)
// SetProperties - general report props. (title, author etc.)
SetProperties(rpt *Report)
// Text - Write prints text from the current position.
Text(txtStr string, pageBreak float64)
// Rect outputs a rectangle of width w and height h with the upper left corner positioned at point (x, y)
Rect(x, y, w, h float64, styleStr string)
// Line draws a line between points (x1, y1) and (x2, y2) using the current draw color, line width and cap style.
Line(x1, y1, x2, y2 float64)
// GetX returns the abscissa of the current position.
GetX() float64
// GetY returns the ordinate of the current position.
GetY() float64
// SetX defines the abscissa of the current position.
SetX(x float64)
// SetY : set current position y
SetY(y float64)
// SetXY defines the abscissa and ordinate of the current position.
SetXY(x, y float64)
SetText(x, y float64, value string) error
// Ln performs a line break.
Ln(h float64)
// Cell prints a rectangular cell with optional borders, background color and character string.
Cell(options IM)
// MultiCell supports printing text with line breaks.
MultiCell(options IM)
// Save2Pdf creates a PDF output.
Save2Pdf() ([]byte, error)
// Save2PdfFile writes the PDF document to file
Save2PdfFile(filename string) error
}
Generator the PDF generator interface
type HLine ¶
type HLine struct {
Width string `xml:"width,attr" json:"width"` //number or percent value (e.g. "10" or "10%")
Gap float64 `xml:"gap,attr" json:"gap"` // greater than 0 then double line
BorderColor color.RGBA `xml:"border-color,attr" json:"border-color"` //JSON or XML value: integer gray color (in range from 0 "black" to 255 "white"), default "black"
}
HLine - a horizontal line.
type HTML ¶
type HTML struct {
Fieldname string `xml:"fieldname,attr" json:"fieldname"` //databind fieldname
Value string `xml:",cdata" json:"html"` //html text
}
HTML - a basic HTML elements rendering. It supports only hyperlinks and bold, italic and underscore attributes.
type Image ¶
type Image struct {
Src string `xml:"src,attr" json:"src"` //JPEG or PNG image file path and name (e.g. "test/logo.jpg") or image data
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"` //image height (default height of parent Row).
Width float64 `xml:"width,attr" json:"width"` //image width will be calculated from the height dimension so that the aspect ratio is maintained.
}
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"` //values: "times"(default), "helvetica", "courier" or custom font
FontStyle string `xml:"font-style,attr" json:"font-style"` //values: "" (default), "bold", "italic", "bolditalic"
FontSize float64 `xml:"font-size,attr" json:"font-size"` //Default value: 10
TextColor color.RGBA `xml:"color,attr" json:"color"` //JSON or XML value: in hexadecimal (e.g. #A0522D) or in decimal (e.g 10506797), default "black"
BorderColor color.RGBA `xml:"border-color,attr" json:"border-color"` //JSON or XML value: integer gray color (in range from 0 "black" to 255 "white"), default "black"
BackgroundColor color.RGBA `xml:"background-color,attr" json:"background-color"` //JSON or XML value: integer gray color (in range from 0 "black" to 255 "white"), default "black"
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"` //row height
HGap float64 `xml:"hgap,attr" json:"hgap"` //default gap between these two elements
Visible string `xml:"visible,attr" json:"visible"` //table data source name
Columns []PageItem `xml:"columns,attr" json:"columns"` //Cell, Image, Barcode, Separator
}
Row - Horizontal logical group. The last element width extends up to the right margin.