generator

package
v0.0.0-...-5d5dd80 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2023 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Color

type Color struct {
	R uint8
	G uint8
	B uint8
}

Color represents a specific color in red, green and blue values, each from 0 to 255

type Generator

type Generator interface {
	PrintPdfText(text string, styleStr string, alignStr string)
	PrintLnPdfText(text string, styleStr string, alignStr string)
	DrawLine(x1 float64, y1 float64, x2 float64, y2 float64, color Color, lineWith float64)
	PrintPdfTextFormatted(text string, styleStr string, alignStr string, borderStr string, fill bool, backgroundColor Color, cellHeight float64, cellWidth float64)
	NewLine(oldX float64)
	PreviousLine(oldX float64)

	RegisterMimeImageToPdf(cdnUrl *url.URL) (imageNameStr string)
	PlaceRegisteredImageOnPage(imageNameStr string, alignStr string, scale float64)
	GetRegisteredImageExtent(imageNameStr string) (w float64, h float64)
	ImageIsRegistered(imageNameStr string) bool

	PrintTableHeader(cells []string, columnWidth []float64, columnAlignStrings []string)
	PrintTableBody(cells [][]string, columnWidths []float64, columnAlignStrings []string)
	PrintTableFooter(cells [][]string, columnWidths []float64, columnAlignStrings []string)

	GetPdf() *gofpdf.Fpdf
	GetError() error
	SetError(err error)
	ComputeStringLength(str string) (length float64)

	GetFontName() string
	GetMarginLeft() float64
	GetMarginTop() float64
	GetMarginRight() float64
	GetMarginBottom() float64

	GetFontGapY() float64
	SetFontGapY(fontGapY float64)
	GetFontSize() float64
	SetFontSize(textSize float64)
	GetCursor() (x float64, y float64)
	SetCursor(x float64, y float64)
	SetUnsafeCursor(x float64, y float64)

	NewPage()
	GetCurrentPageNumber() int
	GetTotalNumber() int
	GoToPage(pageNumber int)
}

Generator specify all public methods.

type MetaData

type MetaData struct {
	FontName         string
	FontGapY         float64
	FontSize         float64
	MarginLeft       float64
	MarginTop        float64
	MarginRight      float64
	MarginBottom     float64
	Unit             string
	DefaultLineWidth float64
	DefaultLineColor Color
}

MetaData sums all necessary inputs for NewPDFGenerator().

FontName define font familie used to print character strings. Standard families (case insensitive):

"Courier" for fixed-width,
"Helvetica" or "Arial" for sans serif,
"Times" for serif,
"Symbol" or "ZapfDingbats" for symbolic.
"OpenSans" for TrueType support with utf-8 symbols.

FontGapY defines the gap between two text lines in the Unit of measure.

FontSize defines the font size measured in points.

MarginLeft defines the left page margin in the Unit of measure.

MarginTop defines the top page margin in the Unit of measure.

MarginRight defines the right page margin in the Unit of measure. If the value is less than zero, it is set to the same as the left margin.

MarginBottom defines the bottom page margin in the Unit of measure. On top of the bottom margin is the footer section.

Unit specifies the unit of length used in size parameters for elements other than fonts, which are always measured in points. An empty string will be replaced with "mm". Specify

"pt" for point,
"mm" for millimeter,
"cm" for centimeter, or
"in" for inch.

type PDFGenerator

type PDFGenerator struct {
	// contains filtered or unexported fields
}

PDFGenerator is a light-way PDF document generator witch simplify and enhanced github.com/jung-kurt/gofpdf. The implemented methods focused primary on creating easy clean invoices or B2B letters.

func NewPDFGenerator

func NewPDFGenerator(data MetaData, strictErrorHandling bool, logger *zerolog.Logger, headerFunction func(), footerFunction func(isLastPage bool)) (gen *PDFGenerator, err error)

NewPDFGenerator construct and return a new PDFGenerator instance.

MetaData is used for all necessary inputs.

Set strictErrorHandling to true, to provide execution of any method, if a pdf internal error is set. If strictErrorHandling is set to false, all methods are tried to execute executed, even if a pdf internal error is set. This may cause the PDF internal error to be overwritten by a new error. Use GetError() to get the current pdf internal error.

func (*PDFGenerator) ComputeStringLength

func (core *PDFGenerator) ComputeStringLength(str string) (length float64)

func (*PDFGenerator) DrawLine

func (core *PDFGenerator) DrawLine(x1 float64, y1 float64, x2 float64, y2 float64)

DrawLine draw a user defines line between two points.

x1 and y1 defines the abscissa (x) and ordinate (y) cursor start point.

x2 and y2 defines the abscissa (x) and ordinate (y) cursor end point.

color specifies the color of the line.

lineWith specifies the thinness of the line in the unit of measure specified in NewPDFGenerator().

func (*PDFGenerator) GetCurrentPageNumber

func (core *PDFGenerator) GetCurrentPageNumber() int

func (*PDFGenerator) GetCursor

func (core *PDFGenerator) GetCursor() (x float64, y float64)

GetCursor returns the abscissa (x) and ordinate (y) cursor point

func (*PDFGenerator) GetError

func (core *PDFGenerator) GetError() error

GetError returns the internal PDF error; this will be nil if no error has occurred.

func (*PDFGenerator) GetFontGapY

func (core *PDFGenerator) GetFontGapY() float64

GetFontGapY returns the specified gap between two lines in the unit of measure specified in NewPDFGenerator().

func (*PDFGenerator) GetFontName

func (core *PDFGenerator) GetFontName() string

GetFontName returns the specified font name in the unit of measure specified in NewPDFGenerator().

func (*PDFGenerator) GetFontSize

func (core *PDFGenerator) GetFontSize() float64

GetFontSize returns the current font size in the unit of measure specified in NewPDFGenerator().

func (*PDFGenerator) GetMarginBottom

func (core *PDFGenerator) GetMarginBottom() float64

GetMarginBottom returns the specified bottom margin in the unit of measure specified in NewPDFGenerator().

func (*PDFGenerator) GetMarginLeft

func (core *PDFGenerator) GetMarginLeft() float64

GetMarginLeft returns the specified left margin in the unit of measure specified in NewPDFGenerator().

func (*PDFGenerator) GetMarginRight

func (core *PDFGenerator) GetMarginRight() float64

GetMarginRight returns the specified right margin in the unit of measure specified in NewPDFGenerator().

func (*PDFGenerator) GetMarginTop

func (core *PDFGenerator) GetMarginTop() float64

GetMarginTop returns the specified top margin in the unit of measure specified in NewPDFGenerator().

func (*PDFGenerator) GetPdf

func (core *PDFGenerator) GetPdf() *gofpdf.Fpdf

GetPdf returns the full PDF.

Usually used at the end of all manipulations.

func (*PDFGenerator) GetRegisteredImageExtent

func (core *PDFGenerator) GetRegisteredImageExtent(imageNameStr string) (w float64, h float64)

func (*PDFGenerator) GetTotalNumber

func (core *PDFGenerator) GetTotalNumber() int

func (*PDFGenerator) GoToPage

func (core *PDFGenerator) GoToPage(pageNumber int)

func (*PDFGenerator) ImageIsRegistered

func (core *PDFGenerator) ImageIsRegistered(imageNameStr string) bool

func (*PDFGenerator) NewLine

func (core *PDFGenerator) NewLine(oldX float64)

NewLine sets the cursor on the next line dependent on the given X-position. (mostly use the start X-point of the current line.)

func (*PDFGenerator) NewPage

func (core *PDFGenerator) NewPage()

func (*PDFGenerator) PlaceRegisteredImageOnPage

func (core *PDFGenerator) PlaceRegisteredImageOnPage(imageNameStr string, alignStr string, scale float64)

PlaceRegisteredImageOnPage place a registered image (see RegisterMimeImageToPdf) on the current pdf page. The top side of the image will be snap to the current cursor position.

imageNameStr specifies the registered image identifier.

scale specifies the scaling factor into which the image is drawn. The value must be grater then 0. Use scaling of 1 for no scaling. E.g. a value of 0.5 means draw the image in half the size of the original and a value of 3 means draw the image in the triple size of the original.

alignStr specifies the horizontal align type. Use:

"L" for align the left side of the image to the cursor,
"R" for align the right side of the image to the cursor, and
"C" for align the center of the image to the cursor.

func (*PDFGenerator) PreviousLine

func (core *PDFGenerator) PreviousLine(oldX float64)

PreviousLine sets the cursor on the previous line dependent on the given X-position. (mostly use the start X-point of the current line.)

func (*PDFGenerator) PrintLnPdfText

func (core *PDFGenerator) PrintLnPdfText(text string, styleStr string, alignStr string)

PrintLnPdfText prints from the current cursor position a simple text cell in the PDF and call NewLine() at the end.

text passed the string to print. Use \n escape character to trigger NewLine() inside the text.

styleStr defines the font style:

"" non-specific font style,
"l" light font,
"i" italic font,
"b" bold font, or
"m" medium font.

alignStr set the align mode:

"L" align the left side of the text to the current cursor position,
"R" align the right side of the text to the current cursor position, or
"C" align the center of the text to the current cursor position.

func (*PDFGenerator) PrintPdfText

func (core *PDFGenerator) PrintPdfText(text string, styleStr string, alignStr string)

PrintPdfText prints from the current cursor position a simple text cell in the PDF.

text passed the string to print.

styleStr defines the font style:

"" non-specific font style
"l" light font
"i" italic font
"b" bold font
"m" medium font

alignStr set the align mode:

"L" align the left side of the text to the current cursor position
"R" align the right side of the text to the current cursor position
"C" align the center of the text to the current cursor position

func (*PDFGenerator) PrintPdfTextFormatted

func (core *PDFGenerator) PrintPdfTextFormatted(text string, styleStr string, alignStr string, borderStr string, fill bool, backgroundColor Color, cellHeight float64, cellWidth float64)

PrintPdfTextFormatted prints from the current cursor position a formatted text cell in the PDF (e.g. with boarders or background color).

text passed the string to print.

styleStr defines the font style:

 "" non-specific font style
	"l" light font
	"i" italic font
	"b" bold font
	"m" medium font

*alignStr* set the align mode. The default alignment is left middle.

Horizontal alignment is controlled by including "L", "C" or "R" (left, center, right) in alignStr.
Vertical alignment is controlled by including "T", "M", "B" or "A" (top, middle, bottom, baseline) in alignStr.

borderStr specifies how the cell border will be drawn:

An empty string indicates no border,
"1" indicates a full border,
one or more of "L", "T", "R" and "B" indicate the left, top, right and bottom sides of the border.

fill defines, whether the background is set to the background color or not. If false, use a transparent background.

backgroundColor defines the background color using the Color.

cellHeight specifies the total height of the cell in the unit of measure specified in NewPDFGenerator().

cellWidth specifies the total width of the cell in the unit of measure specified in NewPDFGenerator().

func (*PDFGenerator) PrintTableBody

func (core *PDFGenerator) PrintTableBody(cells [][]string, columnWidths []float64, columnAlignStrings []string)

PrintTableBody prints a generic and clean styled table content rows.

cells contains an array with includes all rows. Each row is an array by its self includes the information of each cell. In fact, cells can be described as [rowNumber][columnNumber]contentString.

columnWidths defines the width of each column. NOTE: in general use here the same widths as in PrintTableHeader().

columnAlignStrings specifies the align type of each column. Use:

"L" for align the left side of the text to the left side of the table cell,
"R" for align the right side of the text to the right side of the table cell, and
"C" for align the text to the center of the table cell.

E.g. in an invoice table, typically use "L" for all strings and "R" for salary.

func (*PDFGenerator) PrintTableFooter

func (core *PDFGenerator) PrintTableFooter(cells [][]string, columnWidths []float64, columnAlignStrings []string)

PrintTableFooter prints a generic and clean styled table footer. The last row of the footer will be print in the same style as the table header.

cells contains an array with includes all rows. Each row is an array by its self includes the information of each cell. In fact, cells can be described as [rowNumber][columnNumber]contentString.

columnWidths defines the width of each column.

columnAlignStrings specifies the align type of each column. Use:

"L" for align the left side of the text to the left side of the table cell,
"R" for align the right side of the text to the right side of the table cell, and
"C" for align the text to the center of the table cell.

E.g. in an invoice table, typically use "L" for all strings and "R" for salary.

func (*PDFGenerator) PrintTableHeader

func (core *PDFGenerator) PrintTableHeader(cells []string, columnWidth []float64, columnAlignStrings []string)

PrintTableHeader print a generic and clean styled table header.

cells contain the displayed column names of the table header.

columnWidth defines the width of each column. NOTE: in general use here the same widths as in PrintTableBody()

func (*PDFGenerator) RegisterMimeImageToPdf

func (core *PDFGenerator) RegisterMimeImageToPdf(cdnUrl *url.URL) (imageNameStr string)

RegisterMimeImageToPdf downloade a JPEG, PNG or GIF image (from mostly a Content Delivery Network (CDN)) URL and puts it in the current page. The image will be registered in the PDF but not place on a page! Use PlaceRegisteredImageOnPage to place the image on a page.

cdnUrl specifies a parsed (CDN) URL.

return imageNameStr, the image identifier for placing the image on a pdf page.

func (*PDFGenerator) SetCursor

func (core *PDFGenerator) SetCursor(x float64, y float64)

SetCursor set manual the abscissa (x) and ordinate (y) reference point in the unit of measure specified in NewPDFGenerator() for the next operation. The position must be inside the writing area, restricted by the defined margins in NewPDFGenerator()

func (*PDFGenerator) SetError

func (core *PDFGenerator) SetError(err error)

SetError set an internal PDF error.

func (*PDFGenerator) SetFontGapY

func (core *PDFGenerator) SetFontGapY(fontGapY float64)

SetFontGapY change the gap between two lines in the unit of measure specified in NewPDFGenerator().

func (*PDFGenerator) SetFontSize

func (core *PDFGenerator) SetFontSize(textSize float64)

SetFontSize change the font size in the unit of measure specified in NewPDFGenerator().

func (*PDFGenerator) SetUnsafeCursor

func (core *PDFGenerator) SetUnsafeCursor(x float64, y float64)

SetUnsafeCursor set manual the abscissa (x) and ordinate (y) reference point in the unit of measure specified in NewPDFGenerator() for the next operation. The position must be inside the page area, restricted by the page size.

Jump to

Keyboard shortcuts

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