pdf

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2026 License: MIT Imports: 7 Imported by: 0

README

tinywasm/pdf

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 rendering engine (fpdf/) is adapted from gofpdf, not a fork kept in sync with upstream: the legacy Type1/AFM font lineage and internal/ machinery were removed, and the public surface was rebuilt around a typed, WASM-safe API (LoadDeclared, Canvas, color.Color) — see docs/ARCHITECTURE.md.

New Flow-First Layout API

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

Example
import (
	"github.com/tinywasm/font"
	"github.com/tinywasm/pdf"
)

// Declare the font family and path
d := font.Declare("Roboto", "fonts/")
tf, err := pdf.LoadDeclared(d)
if err != nil {
	panic(err)
}

// Create a new document with the typeface
doc := pdf.NewDocument(tf)
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 = ""
)

Variables

View Source
var DefaultTheme = Theme{
	Accent: "#1E3C78",
	Header: "#F0F4FA",
	Gray:   "#646464",
	Body:   "#000000",
	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 Canvas added in v0.1.1

type Canvas interface {
	GetX() float64
	GetY() float64
	SetY(y float64)
	GetPageSize() (width, height float64)
	GetMargins() (left, top, right, bottom float64)
	SetLineWidth(width float64)
	SetDrawColor(c color.Color)
	SetFillColor(c color.Color)
	SetTextColor(c color.Color)
	Line(x1, y1, x2, y2 float64)
	Rect(x, y, w, h float64, style string)
	Circle(x, y, r float64, style string)
	ArcTo(x, y, rx, ry, angle, startAngle, endAngle float64)
	DrawPath(style string)
	LineTo(x, y float64)
	MoveTo(x, y float64)
	Text(x, y float64, txt string)
	GetStringWidth(txt string) float64
	CellFormat(w, h float64, txt, border string, ln int, align string, fill bool, link int, linkStr string)
	SetDrawingFont(style string, size float64)
}

Canvas defines the drawing surface interface used by external packages (such as tinywasm/chart) to render graphics on the PDF document.

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(col color.Color) *CellElement

func (*CellElement) Border added in v0.0.39

func (c *CellElement) Border(sides string, width float64, col 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 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(t Typeface, 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(id ImageID) *ImageElement

AddImage adds an image by ID 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) AddTypeface added in v0.1.0

func (d *Document) AddTypeface(t Typeface) TypefaceID

func (*Document) ArcTo added in v0.1.1

func (d *Document) ArcTo(x, y, rx, ry, angle, startAngle, endAngle float64)

func (*Document) CellFormat added in v0.1.1

func (d *Document) CellFormat(w, h float64, txt, border string, ln int, align string, fill bool, link int, linkStr string)

func (*Document) Circle added in v0.1.1

func (d *Document) Circle(x, y, r float64, style string)

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) DrawPath added in v0.1.1

func (d *Document) DrawPath(style string)

func (*Document) Err added in v0.0.39

func (d *Document) Err() error

Err returns the first accumulated error.

func (*Document) GetMargins added in v0.1.1

func (d *Document) GetMargins() (left, top, right, bottom float64)

func (*Document) GetPageSize added in v0.1.1

func (d *Document) GetPageSize() (width, height float64)

func (*Document) GetStringWidth added in v0.1.1

func (d *Document) GetStringWidth(txt string) float64

func (*Document) GetX added in v0.1.1

func (d *Document) GetX() float64

func (*Document) GetY added in v0.1.1

func (d *Document) GetY() float64

func (*Document) Line added in v0.1.1

func (d *Document) Line(x1, y1, x2, y2 float64)

func (*Document) LineTo added in v0.1.1

func (d *Document) LineTo(x, y float64)

func (*Document) Log added in v0.0.39

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

Log writes a message to the logger.

func (*Document) MoveTo added in v0.1.1

func (d *Document) MoveTo(x, y float64)

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) Rect added in v0.1.1

func (d *Document) Rect(x, y, w, h float64, style string)

func (*Document) RegisterImage added in v0.0.39

func (d *Document) RegisterImage(path string) (ImageID, error)

RegisterImage registers an image to be loaded immediately.

func (*Document) SetDrawColor added in v0.1.1

func (d *Document) SetDrawColor(c color.Color)

func (*Document) SetDrawingFont added in v0.1.1

func (d *Document) SetDrawingFont(style string, size float64)

SetDrawingFont activates the currently active typeface with the requested style and size. This satisfies the drawing interface constraint: the canvas does not expose SetFont(familyStr, ...), preventing untyped font family strings from being used in drawing contracts.

func (*Document) SetFillColor added in v0.1.1

func (d *Document) SetFillColor(c color.Color)

func (*Document) SetLineWidth added in v0.1.1

func (d *Document) SetLineWidth(width float64)

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) SetSize added in v0.1.0

func (d *Document) SetSize(pt float64) *Document

func (*Document) SetTextColor added in v0.1.1

func (d *Document) SetTextColor(c color.Color)

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) SetY added in v0.1.1

func (d *Document) SetY(y float64)

func (*Document) SpaceBefore added in v0.0.39

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

SpaceBefore is an alias for AddSpace.

func (*Document) Text added in v0.1.1

func (d *Document) Text(x, y float64, txt string)

func (*Document) Use added in v0.1.0

func (d *Document) Use(id TypefaceID) *Document

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(id ImageID) *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 ImageID added in v0.1.0

type ImageID int

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.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 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 Style added in v0.0.39

type Style struct {
	FillColor color.Color
	TextColor color.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.Color) *TableBuilder

func (*TableBuilder) BorderBottom added in v0.0.39

func (t *TableBuilder) BorderBottom(width float64, c color.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.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.Color // color para texto de headers (H1, H2, H3)
	Brand  color.Color // color de marca para elementos decorativos (líneas, bandas)
	Header color.Color
	Gray   color.Color
	Body   color.Color
	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.

type Typeface added in v0.1.0

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

func LoadDeclared added in v0.1.1

func LoadDeclared(d font.Declaration) (Typeface, error)

LoadDeclared loads the four faces named by the font.Declaration. The .ttf extension is appended by this package.

type TypefaceID added in v0.1.0

type TypefaceID int

Directories

Path Synopsis
examples
contract_layout command
list command
web
ui

Jump to

Keyboard shortcuts

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