pdfkit

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2026 License: MIT Imports: 18 Imported by: 0

README

pdfkit-go

Pure Go PDF library inspired by PDFKit and pdf-lib.

No CGO. Dependencies are limited to pure-Go font tooling (tdewolff/font, boxesandglue/textshape).

Features

  • Create documents; stream with Save(io.Writer) (HTTP-friendly)
  • Open, stamp, and merge existing unencrypted PDFs
  • Vector graphics: canvas API, SVG paths, transforms, gradients, opacity, rounded rects
  • Text: soft-hyphen wrapping, alignments, lists, Standard 14 fonts
  • Font embedding + subsetting (TTF/OTF/WOFF/WOFF2/TTC) with OpenType shaping
  • Images: JPEG pass-through, PNG (including alpha)

Install

go get github.com/Cheersupzoo/pdfkit-go

Quick start

package main

import (
	"log"
	"net/http"

	pdfkit "github.com/Cheersupzoo/pdfkit-go"
)

func main() {
	http.HandleFunc("/pdf", func(w http.ResponseWriter, r *http.Request) {
		doc := pdfkit.New(pdfkit.WithPageSize(pdfkit.A4))
		doc.AddPage()
		doc.Font("Helvetica").FontSize(24)
		doc.Text("Hello from pdfkit-go", pdfkit.TextOptions{X: 72, Y: 750})
		doc.MoveTo(72, 720).LineTo(300, 720).Stroke()

		w.Header().Set("Content-Type", "application/pdf")
		if err := doc.Save(w); err != nil {
			http.Error(w, err.Error(), 500)
		}
	})
	log.Fatal(http.ListenAndServe(":8080", nil))
}

Examples

go test ./...
go run ./examples/demo/
go run ./examples/cover/
go run ./examples/cover-th/   # Thai + TH Sarabun

Design

See docs/DESIGN.md.

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	A4     = PageSize{595.28, 841.89}
	Letter = PageSize{612, 792}
	Legal  = PageSize{612, 1008}
	A3     = PageSize{841.89, 1190.55}
)

Functions

This section is empty.

Types

type Align

type Align int

Align is horizontal text alignment.

const (
	AlignLeft Align = iota
	AlignCenter
	AlignRight
	AlignJustify
)

type Color

type Color struct {
	R, G, B float64
}

Color is an RGB color with components in 0..1.

func HexColor

func HexColor(hex string) Color

func RGB

func RGB(r, g, b float64) Color

type Document

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

Document is a PDF being created or modified.

func LoadPDFBytes

func LoadPDFBytes(b []byte) (*Document, error)

helper used by tests

func New

func New(opts ...Option) *Document

New creates an empty document.

func Open

func Open(r io.Reader) (*Document, error)

Open reads an existing PDF for modification / page copying. Non-seekable readers are spooled to a temp file. Objects are loaded lazily from the file until pages are materialized (Merge or Save).

func OpenFile

func OpenFile(path string) (*Document, error)

OpenFile opens a PDF from disk without copying it into memory up front. The file remains open until Document.Close or after pages are materialized and the source is released.

func (*Document) AddPage

func (d *Document) AddPage(size ...PageSize) *Page

AddPage appends a page and makes it current.

func (*Document) Bytes

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

Bytes returns the PDF as a byte slice.

func (*Document) Circle

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

func (*Document) Clip

func (d *Document) Clip() *Document

func (*Document) Close added in v0.5.0

func (d *Document) Close() error

Close releases any file/temp resources held by an opened PDF. After Close, Save still works if pages were already materialized (e.g. after Merge); otherwise Open'd pages must be saved before Close, or Save will materialize first while the source is open.

func (*Document) ClosePath

func (d *Document) ClosePath() *Document

func (*Document) CurveTo

func (d *Document) CurveTo(x1, y1, x2, y2, x3, y3 float64) *Document

func (*Document) Dash

func (d *Document) Dash(pattern []float64, phase float64) *Document

func (*Document) Err

func (d *Document) Err() error

func (*Document) Fill

func (d *Document) Fill() *Document

func (*Document) FillAndStroke

func (d *Document) FillAndStroke() *Document

func (*Document) FillColor

func (d *Document) FillColor(c Color) *Document

func (*Document) FillEvenOdd

func (d *Document) FillEvenOdd() *Document

func (*Document) FillShading

func (d *Document) FillShading(name string) *Document

func (*Document) Font

func (d *Document) Font(name string) *Document

Font selects a standard font by name or previously registered embedded font family.

func (*Document) FontSize

func (d *Document) FontSize(size float64) *Document

FontSize sets the current font size in points.

func (*Document) HasLiveImportSource added in v0.5.0

func (d *Document) HasLiveImportSource() bool

HasLiveImportSource reports whether any page still references an open source model. Intended for tests and diagnostics.

func (*Document) Image

func (d *Document) Image(name string, x, y, w, h float64) *Document

Image draws a registered image at x,y with width w (height scaled if h<=0).

func (*Document) ImageFile

func (d *Document) ImageFile(path string, x, y, w, h float64) *Document

ImageFile is a convenience to register and draw an image.

func (*Document) LineCap

func (d *Document) LineCap(style int) *Document

func (*Document) LineJoin

func (d *Document) LineJoin(style int) *Document

func (*Document) LineTo

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

func (*Document) LineWidth

func (d *Document) LineWidth(w float64) *Document

func (*Document) LinearGradient

func (d *Document) LinearGradient(x1, y1, x2, y2 float64, stops []GradientStop) string

LinearGradient creates an axial shading. Returns a shading name for FillShading.

func (*Document) List

func (d *Document) List(items []string, opts ...TextOptions) *Document

List draws a bulleted list.

func (*Document) Merge

func (d *Document) Merge(other *Document) error

Merge appends all pages from other into d. Imported pages are materialized into d (cloned into d.importCat) so other can be Closed and its source file released without keeping all merge inputs in memory.

func (*Document) MergeFiles

func (d *Document) MergeFiles(paths ...string) error

MergeFiles opens and merges PDF files into d one at a time, releasing each source after its pages are materialized into d.

func (*Document) MoveDown

func (d *Document) MoveDown(lines float64) *Document

func (*Document) MoveTo

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

func (*Document) Opacity

func (d *Document) Opacity(alpha float64) *Document

Opacity sets fill/stroke alpha via ExtGState.

func (*Document) Page

func (d *Document) Page() *Page

Page returns the current page, creating one if needed.

func (*Document) PageCount

func (d *Document) PageCount() int

func (*Document) Path

func (d *Document) Path(dpath string) *Document

Path appends an SVG path data string.

func (*Document) QuadraticCurveTo

func (d *Document) QuadraticCurveTo(x1, y1, x, y float64) *Document

func (*Document) RadialGradient

func (d *Document) RadialGradient(x1, y1, r1, x2, y2, r2 float64, stops []GradientStop) string

RadialGradient creates a radial shading.

func (*Document) Rect

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

func (*Document) RegisterFont

func (d *Document) RegisterFont(family string, data []byte, index int) error

RegisterFont loads a TTF/OTF/WOFF/WOFF2/TTC font from bytes. index selects a face in a collection (usually 0).

func (*Document) RegisterFontFile

func (d *Document) RegisterFontFile(family, path string, index int) error

RegisterFontFile loads a font from disk.

func (*Document) RegisterImage

func (d *Document) RegisterImage(name string, data []byte) (string, error)

RegisterImage loads JPEG or PNG from bytes and returns a resource name.

func (*Document) RegisterImageFile

func (d *Document) RegisterImageFile(name, path string) (string, error)

RegisterImageFile loads an image from disk.

func (*Document) RestoreGraphics

func (d *Document) RestoreGraphics() *Document

func (*Document) Rotate

func (d *Document) Rotate(angleDeg float64) *Document

func (*Document) RoundedRect

func (d *Document) RoundedRect(x, y, w, h, r float64) *Document

RoundedRect appends a rectangle with corner radius r (clamped to half sides).

func (*Document) Save

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

Save writes the PDF to w.

func (*Document) SaveGraphics

func (d *Document) SaveGraphics() *Document

func (*Document) Scale

func (d *Document) Scale(sx, sy float64) *Document

func (*Document) Stroke

func (d *Document) Stroke() *Document

func (*Document) StrokeColor

func (d *Document) StrokeColor(c Color) *Document

func (*Document) SwitchToPage

func (d *Document) SwitchToPage(i int) *Page

SwitchToPage selects a page by 0-based index and makes it current for subsequent draws.

func (*Document) Text

func (d *Document) Text(s string, opts ...TextOptions) *Document

Text draws text at the current text position or options.X/Y.

func (*Document) TextXY

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

func (*Document) Transform

func (d *Document) Transform(a, b, c, e, f, g float64) *Document

func (*Document) Translate

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

func (*Document) Undash

func (d *Document) Undash() *Document

func (*Document) WriteFile

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

WriteFile saves to a path.

type GradientStop

type GradientStop struct {
	Offset float64
	Color  Color
}

GradientStop is a color stop for gradients (Offset in 0..1).

type Info

type Info struct {
	Title, Author, Subject, Keywords, Creator, Producer string
}

Info holds document metadata.

type Option

type Option func(*Document)

Option configures a Document.

func WithInfo

func WithInfo(info Info) Option

func WithMargins

func WithMargins(all float64) Option

func WithPageSize

func WithPageSize(s PageSize) Option

type Page

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

Page is a single PDF page with a content stream builder.

func (*Page) Height

func (p *Page) Height() float64

func (*Page) Width

func (p *Page) Width() float64

type PageSize

type PageSize struct {
	Width, Height float64
}

PageSize in PDF points (1/72 inch).

type TextOptions

type TextOptions struct {
	Width     float64
	Align     Align
	LineGap   float64
	Paragraph float64
	X, Y      float64 // if set (Y!=0 or continued), absolute position; zero X with continued uses text cursor
	Continued bool
}

TextOptions configures Text drawing.

Directories

Path Synopsis
examples
cover command
cover-th command
demo command
internal
pdf

Jump to

Keyboard shortcuts

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