Documentation ¶
Overview ¶
tps is a package to hide a lot of the complicated details of creating PDFs with fpdf. It relies on a grid specification and content placement is similar to a spreadsheet.
For example, a grid specification includes the page margins, # of columns, the gutter size (the space between columns), and line height. Using this tps calculates a grid coordinate system similar to a speadsheet. Content placement is then done with the row, column, and width (determined by the # of horizontal cells to take up).
Index ¶
- Constants
- type Block
- type Cell
- type Grid
- type Point
- type Report
- func (r *Report) AddBlock(name string, width, height int)
- func (r *Report) AddFont(filename, encoding string) error
- func (r *Report) AddPage()
- func (r *Report) AddStyle(name string, fontFamily string, fontStyle string, fontSize float64, ...)
- func (r *Report) CompileEncoding(encoding string) (filename string, err error)
- func (r *Report) CompileFont(filename, encoding string) (string, error)
- func (r *Report) Content(x int, y int, blockName string, styleName string, content string) (lineCount int, err error)
- func (r *Report) IsCompiledFile(filename string) bool
- func (r *Report) IsSourcedFont(filename string) bool
- func (r *Report) PrepareFontCompiledPath() error
- func (r *Report) SetFontPath(fontSourcePath string)
- func (r *Report) SetGrid(orientation int, pageSize int, unit int, margin float64, columnCount int, ...)
- type Style
Constants ¶
const ( OrientationPortrait = iota OrientationLandscape )
const ( PageSizeA3 = iota PageSizeA4 PageSizeA5 PageSizeLetter PageSizeLegal )
const ( UnitPt = iota UnitMm UnitCm UnitIn )
const ( AlignLeft AlignCenter AlignRight AlignTop AlignMiddle AlignBottom )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Block ¶
Block is a specification of a line placed in the PDF. The Width and Height fields are integers which mean they are the multiples of Grid specs. Width indicates # of columns plus the gutters between columns, and Height indicates multiples of Grid.LineHeight.
type Cell ¶
type Cell struct {
Width, Height float64
}
Cell is the width and height in the Grid.Unit system relative to the PDF's measurement system converted from tps' speadsheet-like sizing.
type Grid ¶
type Grid struct { ColumnCount int ColumnWidth float64 GutterCount int GutterWidth float64 LineHeight float64 Margin float64 Orientation int PageWidth float64 PageHeight float64 PageSize int Unit int }
Grid holds all the page and grid specification required for the Report to create new pages and place content.
func (*Grid) CalculateColumns ¶
CalculateColumns calculates the remaining column related specs based on ColumnCount and GutterWidth.
type Point ¶
type Point struct {
X, Y float64
}
Point is the X, Y coordinates in the Grid.Unit system relative to the PDF's coordinate system converted from tps' spreadsheet-like coordinates.
type Report ¶
type Report struct { Grid Grid Pdf *gofpdf.Fpdf Styles map[string]Style Blocks map[string]Block FontSourcePath string FontCompiledPath string }
Report is the main struct type that holds all information to generate a PDF.
func (*Report) AddBlock ¶
AddBlock adds a new block specification to use when placing content in this report. The width and height are the number of columns and lines of the block respectively. The height is the number of lineHeight per line that the content placement takes up.
func (*Report) AddFont ¶
AddFont takes a font filename and compiles it into Report.FontCompiledPath with the encoding specified. It strips the filename extension and replaces it with .json automatically. The extension-less string becomes the name of the font family to use with Report.AddStyle(). For example:
r.AddFont("OpenSans-Bold.ttf", "cp1252") r.AddStyle("header", "OpenSans-Bold", "", 64, AlignmentTop | AlignmentLeft)
The following encodings are supported:
cp1250 cp1251 cp1252 cp1253 cp1254 cp1255 cp1257 cp1258 cp874 iso-8859-1 iso-8859-11 iso-8859-15 iso-8859-16 iso-8859-2 iso-8859-4 iso-8859-5 iso-8859-7 iso-8859-9 koi8-r koi8-u
func (*Report) AddPage ¶
func (r *Report) AddPage()
AddPage creates new page in the report. The previous page is now set if it exists, and all placement will take place in this new page.
func (*Report) AddStyle ¶
func (r *Report) AddStyle( name string, fontFamily string, fontStyle string, fontSize float64, alignment int, )
AddStyle adds a new style to use when placing content in this report.
All specs are set. So even small differences will require different styles. An example set of styles can look like the following:
r.AddStyle("header", "OpenSans", "", 24, AlignmentCenter | AlignmentTop) r.AddStyle("subheader", "OpenSans", "", 18, AlignmentLeft | AlignmentTop)
func (*Report) CompileEncoding ¶
CompileEncoding creates the encoding map file in Report.FontCompiledPath so the underlying Fpdf object can correctly use it to compile fonts.
func (*Report) CompileFont ¶
CompileFont takes a font file in Report.FontSourcePath and converts it into .json format if it doesn't exist in Report.FontCompiledPath.
func (*Report) Content ¶
func (r *Report) Content( x int, y int, blockName string, styleName string, content string, ) (lineCount int, err error)
Place a string based on the x, y coordinates on the grid, using the named block and style specifications. Returns the # of lines (different from Block.Height) taken up by this call to help dynamically place following content.
func (*Report) IsCompiledFile ¶
IsCompiledFile checks if the filename exists in Report.FontCompiledPath. This has the suffix "File" instead of "Font" like Report.IsSourcedFont() because this method might be used to check encoding map files as well.
func (*Report) IsSourcedFont ¶
IsSourcedFont checks if the font filename exists in Report.FontSourcePath.
func (*Report) PrepareFontCompiledPath ¶
PrepareFontCompiledPath creates the "_compiled" subdirectory.
func (*Report) SetFontPath ¶
SetFontPath tells the Report where to find fonts specified with AddFont().
func (*Report) SetGrid ¶
func (r *Report) SetGrid( orientation int, pageSize int, unit int, margin float64, columnCount int, gutterWidth float64, lineHeight float64, )
SetGrid sets all page and grid related specifications required to place content. This must be set before any Content() calls are made.