pdf

package module
v0.0.39 Latest Latest
Warning

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

Go to latest
Published: May 16, 2026 License: MIT Imports: 6 Imported by: 0

README

TinyPDF

Overview

This library is designed for web rendering with WebAssembly. It is optimized for TinyGo compatibility, with all standard library components that are not compatible with TinyGo being removed or replaced.

The fork of go-pdf https://github.com/jung-kurt/gofpdf

New Flow-First Layout API

TinyPDF now features a declarative, flow-first API that prioritizes composition over absolute positioning.

Example
doc := pdf.NewDocument()
doc.AddPage()

doc.AddTable().
    Cols("30%", "auto").
    Row("Key", "Value").
    Row("Name", "John Doe").
    Draw()

doc.WritePdf("output.pdf")

See examples/contract_layout/main.go for a full example.

Documentation

Index

Constants

View Source
const (
	FontBold    = "B"
	FontItalic  = "I"
	FontRegular = ""
)
View Source
const DefaultFontPath = "fonts/Arial.ttf"

DefaultFontPath is the default path to the Arial UTF-8 font.

Variables

View Source
var DefaultTheme = Theme{
	Accent:     "#1E3C78",
	Header:     "#F0F4FA",
	Gray:       "#646464",
	Body:       "#000000",
	FontFamily: "Arial",
	Sizes: struct {
		H1, H2, H3, Body, Small float64
	}{
		H1:    16,
		H2:    12,
		H3:    10,
		Body:  10,
		Small: 8,
	},
	Spacing: struct {
		Paragraph, Section, Page float64
	}{
		Paragraph: 2,
		Section:   5,
		Page:      20,
	},
}

Functions

This section is empty.

Types

type BarChart added in v0.0.39

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

func (*BarChart) AddBar added in v0.0.39

func (c *BarChart) AddBar(val float64, label string, col ...Color) *BarChart

func (*BarChart) Draw added in v0.0.39

func (c *BarChart) Draw()

func (*BarChart) Height added in v0.0.39

func (c *BarChart) Height(h float64) *BarChart

func (*BarChart) Title added in v0.0.39

func (c *BarChart) Title(t string) *BarChart

func (*BarChart) Width added in v0.0.39

func (c *BarChart) Width(w float64) *BarChart

type CellElement added in v0.0.39

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

Cell container.

func Cell added in v0.0.39

func Cell(children ...Element) *CellElement

func (*CellElement) AlignBottom added in v0.0.39

func (c *CellElement) AlignBottom() *CellElement

func (*CellElement) AlignCenter added in v0.0.39

func (c *CellElement) AlignCenter() *CellElement

func (*CellElement) AlignLeft added in v0.0.39

func (c *CellElement) AlignLeft() *CellElement

func (*CellElement) AlignMiddle added in v0.0.39

func (c *CellElement) AlignMiddle() *CellElement

func (*CellElement) AlignRight added in v0.0.39

func (c *CellElement) AlignRight() *CellElement

func (*CellElement) AlignTop added in v0.0.39

func (c *CellElement) AlignTop() *CellElement

func (*CellElement) Background added in v0.0.39

func (c *CellElement) Background(color Color) *CellElement

func (*CellElement) Border added in v0.0.39

func (c *CellElement) Border(sides string, width float64, color Color) *CellElement

func (*CellElement) Padding added in v0.0.39

func (c *CellElement) Padding(top, right, bottom, left float64) *CellElement

func (*CellElement) Span added in v0.0.39

func (c *CellElement) Span(cols, rows int) *CellElement

type ChartFactory added in v0.0.39

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

func (*ChartFactory) Bar added in v0.0.39

func (f *ChartFactory) Bar() *BarChart

Bar starts building a Bar Chart.

func (*ChartFactory) Line added in v0.0.39

func (f *ChartFactory) Line() *LineChart

Line starts building a Line Chart.

func (*ChartFactory) Pie added in v0.0.39

func (f *ChartFactory) Pie() *PieChart

Pie starts building a Pie Chart.

type Color added in v0.0.39

type Color string

Color is a CSS-style hex string, e.g. "#RRGGBB" or "#RGB".

type Document added in v0.0.39

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

Document wraps the internal fpdf.Fpdf to provide a fluent API.

func NewDocument added in v0.0.39

func NewDocument(opts ...Option) *Document

NewDocument creates a new Document instance with UTF-8 support.

func (*Document) AddHeader1 added in v0.0.39

func (d *Document) AddHeader1(text string) *Document

AddHeader1 adds a level 1 header.

func (*Document) AddHeader2 added in v0.0.39

func (d *Document) AddHeader2(text string) *Document

AddHeader2 adds a level 2 header.

func (*Document) AddHeader3 added in v0.0.39

func (d *Document) AddHeader3(text string) *Document

AddHeader3 adds a level 3 header.

func (*Document) AddImage added in v0.0.39

func (d *Document) AddImage(name string) *ImageElement

AddImage adds an image by name (must be registered/loaded) in flow mode.

func (*Document) AddPage added in v0.0.39

func (d *Document) AddPage() *Document

AddPage adds a new page.

func (*Document) AddSeparator added in v0.0.39

func (d *Document) AddSeparator() *Document

AddSeparator adds a horizontal line.

func (*Document) AddSpace added in v0.0.39

func (d *Document) AddSpace(units float64) *Document

AddSpace adds vertical space.

func (*Document) AddTable added in v0.0.39

func (d *Document) AddTable() *TableBuilder

func (*Document) AddText added in v0.0.39

func (d *Document) AddText(text string) *TextElement

AddText adds a text paragraph in flow mode.

func (*Document) Chart added in v0.0.39

func (d *Document) Chart() *ChartFactory

Chart returns a factory to create various types of charts.

func (*Document) Draw added in v0.0.39

func (d *Document) Draw() *Document

Draw is a placeholder for consistency, though currently operations draw immediately.

func (*Document) Err added in v0.0.39

func (d *Document) Err() error

Err returns the first accumulated error.

func (*Document) Load added in v0.0.39

func (d *Document) Load(cb func(error))

Load loads all registered resources.

func (*Document) Log added in v0.0.39

func (d *Document) Log(message ...any)

Log writes a message to the logger.

func (*Document) OutputTo added in v0.0.39

func (d *Document) OutputTo(w io.Writer) error

OutputTo writes the generated PDF into the provided writer.

func (*Document) RegisterFont added in v0.0.39

func (d *Document) RegisterFont(family, path string) *Document

RegisterFont registers a TTF font for all styles (regular, bold, italic, bold-italic).

func (*Document) RegisterFontStyle added in v0.0.39

func (d *Document) RegisterFontStyle(family, style, path string) *Document

RegisterFontStyle registers a TTF font for a specific style ("", "B", "I", "BI").

func (*Document) RegisterImage added in v0.0.39

func (d *Document) RegisterImage(name, path string) *Document

RegisterImage registers an image to be loaded.

func (*Document) SetDefaultFont added in v0.0.39

func (d *Document) SetDefaultFont(family string) *Document

SetDefaultFont sets the default font family for the document.

func (*Document) SetFont added in v0.0.39

func (d *Document) SetFont(family string, size float64) *Document

func (*Document) SetLog added in v0.0.39

func (d *Document) SetLog(fn func(...any)) *Document

SetLog sets the logger function.

func (*Document) SetPageFooter added in v0.0.39

func (d *Document) SetPageFooter() *PageFooter

func (*Document) SetPageHeader added in v0.0.39

func (d *Document) SetPageHeader() *PageHeader

func (*Document) SetTheme added in v0.0.39

func (d *Document) SetTheme(theme Theme) *Document

SetTheme sets the document theme. Missing numeric fields (Sizes, Spacing) inherit from DefaultTheme so callers can specify only the colors they care about without producing zero-height text.

func (*Document) SpaceBefore added in v0.0.39

func (d *Document) SpaceBefore(u float64) *Document

SpaceBefore is an alias for AddSpace.

func (*Document) WritePdf added in v0.0.39

func (d *Document) WritePdf(path string) error

WritePdf generates the PDF and writes it to the specified path.

type Element added in v0.0.39

type Element interface {
	// contains filtered or unexported methods
}

Element is the interface for all layout components.

type ImageElement added in v0.0.39

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

Image element.

func Image added in v0.0.39

func Image(name string) *ImageElement

func (*ImageElement) AlignCenter added in v0.0.39

func (i *ImageElement) AlignCenter() *ImageElement

func (*ImageElement) AlignLeft added in v0.0.39

func (i *ImageElement) AlignLeft() *ImageElement

func (*ImageElement) AlignRight added in v0.0.39

func (i *ImageElement) AlignRight() *ImageElement

func (*ImageElement) Draw added in v0.0.39

func (i *ImageElement) Draw() *Document

Draw renders this image in flow mode. Requires the element to have been created via Document.AddImage. Returns the document for chaining.

func (*ImageElement) Height added in v0.0.39

func (i *ImageElement) Height(mm float64) *ImageElement

func (*ImageElement) Width added in v0.0.39

func (i *ImageElement) Width(mm float64) *ImageElement

type LineChart added in v0.0.39

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

func (*LineChart) AddSeries added in v0.0.39

func (c *LineChart) AddSeries(name string, data []float64, col Color) *LineChart

func (*LineChart) Draw added in v0.0.39

func (c *LineChart) Draw()

func (*LineChart) Height added in v0.0.39

func (c *LineChart) Height(h float64) *LineChart

func (*LineChart) Title added in v0.0.39

func (c *LineChart) Title(t string) *LineChart

func (*LineChart) Width added in v0.0.39

func (c *LineChart) Width(w float64) *LineChart

type LineElement added in v0.0.39

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

Line element.

func Line added in v0.0.39

func Line() *LineElement

func (*LineElement) Color added in v0.0.39

func (l *LineElement) Color(c Color) *LineElement

func (*LineElement) Thickness added in v0.0.39

func (l *LineElement) Thickness(mm float64) *LineElement

func (*LineElement) Width added in v0.0.39

func (l *LineElement) Width(mm float64) *LineElement

type Option added in v0.0.39

type Option func(*Document)

func WithDefaultFont added in v0.0.39

func WithDefaultFont(family, path string) Option

func WithLogger added in v0.0.39

func WithLogger(fn func(...any)) Option

func WithMargins added in v0.0.39

func WithMargins(left, top, right, bottom float64) Option

func WithPageSize added in v0.0.39

func WithPageSize(w, h float64, unit string) Option
type PageFooter struct {
	// contains filtered or unexported fields
}

func (*PageFooter) SetCenterText added in v0.0.39

func (pf *PageFooter) SetCenterText(t string) *PageFooter

func (*PageFooter) WithLeftRight added in v0.0.39

func (pf *PageFooter) WithLeftRight(leftText string) *PageFooter

func (*PageFooter) WithPageTotal added in v0.0.39

func (pf *PageFooter) WithPageTotal(align string) *PageFooter
type PageHeader struct {
	// contains filtered or unexported fields
}

func (*PageHeader) SetLeftText added in v0.0.39

func (ph *PageHeader) SetLeftText(t string) *PageHeader

func (*PageHeader) SetRightText added in v0.0.39

func (ph *PageHeader) SetRightText(t string) *PageHeader

type PieChart added in v0.0.39

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

func (*PieChart) AddSlice added in v0.0.39

func (c *PieChart) AddSlice(label string, val float64, col Color) *PieChart

func (*PieChart) Draw added in v0.0.39

func (c *PieChart) Draw()

func (*PieChart) Height added in v0.0.39

func (c *PieChart) Height(h float64) *PieChart

func (*PieChart) Title added in v0.0.39

func (c *PieChart) Title(t string) *PieChart

func (*PieChart) Width added in v0.0.39

func (c *PieChart) Width(w float64) *PieChart

type Style added in v0.0.39

type Style struct {
	FillColor Color
	TextColor Color
	Font      string // "B", "I", ""
	FontSize  float64
}

type TableBuilder added in v0.0.39

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

func (*TableBuilder) Background added in v0.0.39

func (t *TableBuilder) Background(c Color) *TableBuilder

func (*TableBuilder) BorderBottom added in v0.0.39

func (t *TableBuilder) BorderBottom(width float64, c Color) *TableBuilder

func (*TableBuilder) Cols added in v0.0.39

func (t *TableBuilder) Cols(widths ...string) *TableBuilder

func (*TableBuilder) Draw added in v0.0.39

func (t *TableBuilder) Draw() *Document

func (*TableBuilder) KeepTogether added in v0.0.39

func (t *TableBuilder) KeepTogether() *TableBuilder

func (*TableBuilder) Row added in v0.0.39

func (t *TableBuilder) Row(items ...any) *TableBuilder

type TextElement added in v0.0.39

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

Text element represents a paragraph of text.

func Text added in v0.0.39

func Text(s string) *TextElement

func (*TextElement) AlignCenter added in v0.0.39

func (t *TextElement) AlignCenter() *TextElement

func (*TextElement) AlignLeft added in v0.0.39

func (t *TextElement) AlignLeft() *TextElement

func (*TextElement) AlignRight added in v0.0.39

func (t *TextElement) AlignRight() *TextElement

func (*TextElement) Bold added in v0.0.39

func (t *TextElement) Bold() *TextElement

func (*TextElement) Color added in v0.0.39

func (t *TextElement) Color(c Color) *TextElement

func (*TextElement) Draw added in v0.0.39

func (t *TextElement) Draw() *Document

Draw renders this text in flow mode. Requires the element to have been created via Document.AddText. Returns the document for chaining.

func (*TextElement) Italic added in v0.0.39

func (t *TextElement) Italic() *TextElement

func (*TextElement) Justify added in v0.0.39

func (t *TextElement) Justify() *TextElement

func (*TextElement) Size added in v0.0.39

func (t *TextElement) Size(pt float64) *TextElement

type Theme added in v0.0.39

type Theme struct {
	Accent     Color
	Header     Color
	Gray       Color
	Body       Color
	FontFamily string
	Sizes      struct {
		H1, H2, H3, Body, Small float64
	}
	Spacing struct {
		Paragraph, Section, Page float64
	}
	// Page size in mm. Zero values keep the fpdf default (A4).
	Page ThemePage
	// Margin in mm applied to all four sides. Zero values keep the default (20mm).
	Margin ThemeMargin
}

type ThemeMargin added in v0.0.39

type ThemeMargin struct {
	Top, Right, Bottom, Left float64
}

ThemeMargin defines the four page margins in millimetres.

type ThemePage added in v0.0.39

type ThemePage struct {
	Width, Height float64
}

ThemePage defines the page dimensions in millimetres.

Directories

Path Synopsis
examples
contract_layout command
list command
makefont command
Command makefont generates a font definition file.
Command makefont generates a font definition file.
web
ui

Jump to

Keyboard shortcuts

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