pdf

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2026 License: Apache-2.0, MIT Imports: 11 Imported by: ? 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 simple PDF 1.4 documents in pure Go: flowing text with word wrap, headings, rules, tables that break across pages, page numbers, and document metadata. It is meant for invoices, receipts and certificates of analysis, not for typesetting. Text uses the built-in Helvetica and Helvetica-Bold fonts with WinAnsi encoding, so Western European characters work and others print as "?". It depends only on the standard library.

Index

Constants

This section is empty.

Variables

View Source
var (
	A4     = Size{595.28, 841.89}
	Letter = Size{612, 792}
)

Page sizes in points (1/72 inch).

View Source
var ErrRowTooTall = errors.New("pdf: table row is taller than a page")

ErrRowTooTall is returned for a table row that cannot fit on one page.

Functions

This section is empty.

Types

type Align

type Align int

Align is horizontal text alignment.

const (
	Left Align = iota
	Center
	Right
)

Alignments.

type Color

type Color struct{ R, G, B float64 }

Color is an RGB color with channels from 0 to 1. The zero value is black.

func Gray

func Gray(v float64) Color

Gray returns a gray level from 0 (black) to 1 (white).

func RGB

func RGB(r, g, b uint8) Color

RGB returns a color from 0-255 channels.

type Column

type Column struct {
	Header string
	// Width is the column's share of the table width; zero counts as 1.
	Width float64
	Align Align
}

Column describes one table column.

type Document

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

Document is a PDF under construction. Content flows from the top of the first page and continues on new pages as needed. A Document is not safe for concurrent use.

func New

func New(opts ...Option) *Document

New returns an empty Document with one page.

func (*Document) Bytes

func (d *Document) Bytes() ([]byte, error)

Bytes returns the finished PDF. It can be called more than once; the document is not changed.

func (*Document) Heading

func (d *Document) Heading(level int, s string)

Heading adds a bold heading. Level 1 is 20 pt, 2 is 15 pt, 3 and above 12 pt. A heading is kept with the line that follows it when it fits.

func (*Document) PageBreak

func (d *Document) PageBreak()

PageBreak starts a new page.

func (*Document) Pages

func (d *Document) Pages() int

Pages returns the number of pages so far.

func (*Document) Rule

func (d *Document) Rule()

Rule draws a thin horizontal line across the text width.

func (*Document) Space

func (d *Document) Space(pts float64)

Space moves down by pts points.

func (*Document) Table

func (d *Document) Table(t Table) error

Table draws t. A row never splits across pages; the header repeats on each new page. A row that cannot fit on a page of its own returns ErrRowTooTall.

func (*Document) Text

func (d *Document) Text(s string, style ...Style)

Text adds a wrapped paragraph. It starts a new page when a line does not fit.

func (*Document) WriteTo

func (d *Document) WriteTo(w io.Writer) (int64, error)

WriteTo writes the finished PDF to w.

type Metadata

type Metadata struct {
	Title    string
	Author   string
	Subject  string
	Keywords string
	Creator  string
	// Created defaults to the time of Bytes.
	Created time.Time
}

Metadata is the document information dictionary.

type Option

type Option func(*Document)

Option configures New.

func Compress

func Compress() Option

Compress deflates page content streams.

func Margin

func Margin(pts float64) Option

Margin sets all four margins in points. Default 56 (about 2 cm).

func PageNumbers

func PageNumbers() Option

PageNumbers prints "Page i of n" at the bottom of every page.

func PageSize

func PageSize(s Size) Option

PageSize sets the page size. Default A4.

func WithMetadata

func WithMetadata(m Metadata) Option

WithMetadata sets the document metadata.

type Size

type Size struct{ W, H float64 }

Size is a page size in points.

type Style

type Style struct {
	// Size in points. Default 11.
	Size  float64
	Bold  bool
	Align Align
	Color Color
	// SpaceAfter is extra space below. Default 6.
	SpaceAfter float64
}

Style describes a run of text.

type Table

type Table struct {
	Columns []Column
	Rows    [][]string
	// FontSize defaults to 10.
	FontSize float64
	// HeaderFill defaults to light gray.
	HeaderFill Color
	// BoldLastRow draws the final row in bold, for totals.
	BoldLastRow bool
}

Table is a grid of text with a header row.

Jump to

Keyboard shortcuts

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