pdf

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2026 License: Apache-2.0 Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package pdf writes small, self-contained PDF documents for CipherHorizon's reports: A4 pages, the four standard fonts (no font files to embed), filled and stroked shapes, links and a flat outline. It needs nothing beyond the standard library and x/text, which the module already uses.

The API places things from the top-left corner of the page, in points (1/72 inch), the way a layout is designed; the writer converts to PDF's bottom-left coordinates.

Index

Constants

View Source
const (
	PageWidth  = 595.28
	PageHeight = 841.89
)

A4 in points.

Variables

This section is empty.

Functions

func ChipHeight

func ChipHeight(c Chip) float64

ChipHeight is the drawn height of a chip.

func ChipWidth

func ChipWidth(c Chip) float64

ChipWidth is the drawn width of a chip.

func Clean

func Clean(s string) string

Clean collapses runs of white space, as a paragraph shows them.

func DrawChip

func DrawChip(p *Page, x, y float64, c Chip) float64

DrawChip draws a chip with its top-left corner at x, y and returns its width.

func DrawLine

func DrawLine(p *Page, spans []Span, l Line, x, y float64, align int)

DrawLine draws a wrapped line with its left edge at x and its baseline at y. align: 0 left, 1 right (x is then the right edge), 2 centre (x is the centre).

func Encode

func Encode(s string) []byte

Encode converts text to the WinAnsi bytes the fonts are declared with. Control characters become spaces; anything the fonts cannot show becomes "?".

func Width

func Width(f Font, size float64, s string) float64

Width is the advance width of s in points.

Types

type Block

type Block interface {
	Height(w float64) float64
	Draw(p *Page, x, y, w float64)
}

Block is something a Flow places: it knows its height at a width and how to draw itself with its top-left corner at x, y.

type BoxStyle

type BoxStyle struct {
	Fill, Border, Accent Color
	HasFill, HasBorder   bool
	AccentWidth          float64 // a coloured bar down the left edge
	Pad                  float64
	Radius               float64
	After                float64
}

BoxStyle styles a boxed group of blocks.

type Cell

type Cell struct {
	Spans []Span
	Chip  *Chip
	Dot   *Color
}

Cell is a table cell: text, or a chip, or a coloured dot before text.

type Chip

type Chip struct {
	Text   string
	FG, BG Color
	Size   float64
}

Chip is a small rounded label.

type Col

type Col struct {
	Title string
	W     float64 // a share of the table width
	Align int     // 0 left, 1 right, 2 centre
}

Col is a table column.

type Color

type Color struct{ R, G, B float64 }

Color is an RGB colour with components from 0 to 1.

func Hex

func Hex(s string) Color

Hex parses "#rrggbb". An unreadable value is black.

type Custom

type Custom struct {
	H float64
	F func(p *Page, x, y, w float64)
}

Custom is a block drawn by a function.

func (Custom) Draw

func (c Custom) Draw(p *Page, x, y, w float64)

Draw implements Block.

func (Custom) Height

func (c Custom) Height(float64) float64

Height implements Block.

type Doc

type Doc struct {
	Title   string
	Subject string
	Creator string
	Created time.Time
	// contains filtered or unexported fields
}

Doc is a document being written.

func New

func New() *Doc

New starts a document. Content streams are compressed.

func (*Doc) AddPage

func (d *Doc) AddPage() *Page

AddPage appends a blank A4 page and returns it.

func (*Doc) Bookmark

func (d *Doc) Bookmark(title string, p *Page, y float64)

Bookmark adds an outline entry pointing at y on the page.

func (*Doc) Pages

func (d *Doc) Pages() []*Page

Pages returns the pages in order.

func (*Doc) Write

func (d *Doc) Write(w io.Writer) error

Write serialises the document.

type Flow

type Flow struct {
	Doc                      *Doc
	Page                     *Page
	Left, Right, Top, Bottom float64
	Y                        float64
	// Header, when set, runs on every page the flow starts and may move Y.
	Header func(f *Flow)
}

Flow places blocks down the page and starts new pages as needed.

func (*Flow) Add

func (f *Flow) Add(blocks ...Block)

Add places blocks in order. A paragraph that does not fit continues on the next page line by line; other blocks move whole.

func (*Flow) Box

func (f *Flow) Box(st BoxStyle, blocks ...Block)

Box draws blocks inside a box. A box taller than a page is drawn without its frame so that it can break.

func (*Flow) Need

func (f *Flow) Need(h float64)

Need starts a new page unless h more points fit on this one.

func (*Flow) NewPage

func (f *Flow) NewPage()

NewPage starts a page.

func (*Flow) Table

func (f *Flow) Table(cols []Col, rows [][]Cell, st TableStyle)

Table draws a table; a header row is repeated on every page it spans.

func (*Flow) Width

func (f *Flow) Width() float64

Width is the width of the text column.

type Font

type Font int

Font is one of the four standard fonts the document uses.

const (
	Regular Font = iota
	Bold
	Italic
	Mono
)

The fonts. Regular, Bold and Italic are Helvetica; Mono is Courier.

type Gap

type Gap float64

Gap is vertical space.

func (Gap) Draw

func (Gap) Draw(*Page, float64, float64, float64)

Draw implements Block.

func (Gap) Height

func (g Gap) Height(float64) float64

Height implements Block.

type Line

type Line struct {
	Width float64
	Size  float64 // the largest font size on the line
	// contains filtered or unexported fields
}

Line is one wrapped line of a paragraph.

func Wrap

func Wrap(spans []Span, maxW float64) []Line

Wrap breaks spans into lines no wider than maxW. A word longer than a line is broken where it must be.

type Page

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

Page is one page; draw on it with its methods.

func (*Page) Circle

func (p *Page) Circle(cx, cy, r float64, fill Color)

Circle fills a circle centred on cx, cy.

func (*Page) Index

func (p *Page) Index() int

Index is the zero-based position of the page in its document.

func (*Page) Line

func (p *Page) Line(x1, y1, x2, y2, lw float64, c Color)

Line draws a straight line.

func (p *Page) Link(x, y, w, h float64, url string)

Link makes a rectangle (top-left x, y) open url when clicked. Only http and https addresses are linked.

func (*Page) Polygon

func (p *Page) Polygon(fill Color, pts ...[2]float64)

Polygon fills the closed shape through the points (x, y pairs, top-left origin).

func (*Page) Rect

func (p *Page) Rect(x, y, w, h float64, fill Color)

Rect fills (and optionally strokes) a rectangle whose top-left corner is x, y.

func (*Page) RoundRect

func (p *Page) RoundRect(x, y, w, h, r float64, fill Color, lw float64, stroke Color)

RoundRect fills a rectangle with rounded corners of radius r; with lw > 0 it also strokes it in stroke.

func (*Page) StrokeRect

func (p *Page) StrokeRect(x, y, w, h, lw float64, c Color)

StrokeRect outlines a rectangle.

func (*Page) Text

func (p *Page) Text(x, y float64, f Font, size float64, c Color, s string)

Text draws s with its baseline at y.

func (*Page) TextRight

func (p *Page) TextRight(x, y float64, f Font, size float64, c Color, s string)

TextRight draws s so that it ends at x.

type Para

type Para struct {
	Spans   []Span
	Leading float64 // line height as a multiple of the font size; 1.35 when zero
	Indent  float64 // left indent
	After   float64 // space after the paragraph
	Bullet  string  // drawn in the indent, e.g. "•" or "1."
	Align   int
}

Para is a paragraph block.

func (Para) Draw

func (b Para) Draw(p *Page, x, y, w float64)

Draw implements Block.

func (Para) Height

func (b Para) Height(w float64) float64

Height implements Block.

type Span

type Span struct {
	Text  string
	Font  Font
	Size  float64
	Color Color
	URL   string
}

Span is a run of text in one style. A span with a URL is drawn as a link.

type TableStyle

type TableStyle struct {
	Size                       float64
	HeaderFG, HeaderBG, Stripe Color
	Rule                       Color
	Pad                        float64
	After                      float64
	NoHeader                   bool // a key-value table has no header row
}

TableStyle styles a table.

Directories

Path Synopsis
Package pdfcheck reads back the PDFs package pdf writes, for tests: it checks the cross-reference table points at every object, then returns the page count, the text shown, the links and the outline titles.
Package pdfcheck reads back the PDFs package pdf writes, for tests: it checks the cross-reference table points at every object, then returns the page count, the text shown, the links and the outline titles.

Jump to

Keyboard shortcuts

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