report

package module
v0.0.0-...-f128b3c Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2019 License: GPL-3.0 Imports: 18 Imported by: 0

README

Nervatura Go Report

PDF generation library for Go.

  • PDF printing in pure Go using the gofpdf library. gofpdf has no dependencies other than the Go standard 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
  • Many types of declaration: XML, JSON, Go
  • Several output options: PDF, XML

Installation

To install the package on your system, run

go get github.com/nervatura/go-report

Later, to receive updates, run

go get -u -v github.com/nervatura/go-report/...

Quick Start

See the functions in the report_test.go file for PDF examples.

API: GoDoc

More Resources and Sample Applications: Nervatura Framework

Documentation

Overview

Package report implements the

Nervatura Report

• PDF printing in pure Go using the https://github.com/jung-kurt/gofpdf library. gofpdf has no dependencies other than the Go standard 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

• Many types of declaration: XML, JSON, Go

• Several output options: PDF, XML

Installation

To install the package on your system, run

go get github.com/nervatura/go-report

Later, to receive updates, run

go get -u -v github.com/nervatura/go-report/...

Quick Start

See the functions in the report_test.go (https://github.com/nervatura/go-report/blob/master/report_test.go) file for examples.

More Resources and Sample Applications: Nervatura Framework (https://github.com/nervatura/nervatura)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

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"
	FooterAlign string `xml:"footer-align,attr" json:"footer-align"` //values: "L" (default) or "left", "R" or "right", "C" or "center"
	Footer      string `xml:"footer,attr" json:"footer"`             //static text or databind value
}

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"
	FooterBackground color.RGBA `xml:"footer-background,attr" json:"footer-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 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, PNG or GIF image file path and name (e.g. "test/logo.jpg").
	Height float64 `xml:"height,attr" json:"height"` //image height (default height of parent Row). 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 {
	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"`
	CodePage        string     `xml:"codepage,attr" json:"codepage"`
	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

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".
  • codePage - Optional Default: cp1252
  • fontFamily - Optional Default: times
  • fontDir - Optional Default: "./font"

Example:

rpt := report.New("P", "A4", "iso-8859-2", "Roboto")

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) LoadDefinition

func (rpt *Report) LoadDefinition(xmlString string) (bool, error)

LoadDefinition load to the report an XML definition.

func (*Report) LoadJSONDefinition

func (rpt *Report) LoadJSONDefinition(jsonString string) (bool, 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"`   //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.

type Separator

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

Separator - Row unit, A horizontal separator line.

type VGap

type VGap struct {
	Height float64 `xml:"height,attr" json:"height"` //distance size
}

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