tps

package module
v0.0.0-...-10beb9d Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2015 License: MIT Imports: 9 Imported by: 0

README

tps

For when you want a simple PDF made quickly and get it over with

GoDoc

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

View Source
const (
	OrientationPortrait = iota
	OrientationLandscape
)
View Source
const (
	PageSizeA3 = iota
	PageSizeA4
	PageSizeA5
	PageSizeLetter
	PageSizeLegal
)
View Source
const (
	UnitPt = iota
	UnitMm
	UnitCm
	UnitIn
)
View Source
const (
	AlignLeft
	AlignCenter
	AlignRight
	AlignTop
	AlignMiddle
	AlignBottom
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Block

type Block struct {
	Width  int
	Height int
}

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

func (g *Grid) CalculateColumns() error

CalculateColumns calculates the remaining column related specs based on ColumnCount and GutterWidth.

func (*Grid) GetCell

func (g *Grid) GetCell(block Block) Cell

GetCell returns a Cell struct for use in lower level Fpdf calls

func (*Grid) GetPoint

func (g *Grid) GetPoint(x, y int) Point

GetPoint returns a Point struct for use in lower level Fpdf calls

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 NewReport

func NewReport() *Report

func (*Report) AddBlock

func (r *Report) AddBlock(name string, width, height int)

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

func (r *Report) AddFont(filename, encoding string) error

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

func (r *Report) CompileEncoding(encoding string) (filename string, err error)

CompileEncoding creates the encoding map file in Report.FontCompiledPath so the underlying Fpdf object can correctly use it to compile fonts.

func (*Report) CompileFont

func (r *Report) CompileFont(filename, encoding string) (string, error)

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

func (r *Report) IsCompiledFile(filename string) bool

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

func (r *Report) IsSourcedFont(filename string) bool

IsSourcedFont checks if the font filename exists in Report.FontSourcePath.

func (*Report) PrepareFontCompiledPath

func (r *Report) PrepareFontCompiledPath() error

PrepareFontCompiledPath creates the "_compiled" subdirectory.

func (*Report) SetFontPath

func (r *Report) SetFontPath(fontSourcePath string)

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.

type Style

type Style struct {
	FontFamily string
	FontStyle  string
	FontSize   float64
	Alignment  int
}

Style is a specification of the content visuals. All content placement requires a style name, and cannot be provided dynamically.

Jump to

Keyboard shortcuts

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