write

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2026 License: MIT Imports: 22 Imported by: 0

Documentation

Overview

Package writer provides PDF writing capabilities including page content streams

Package writer provides PDF writing capabilities including image embedding

Package writer provides PDF writing capabilities including page creation

Package writer provides PDF writing capabilities

Index

Constants

This section is empty.

Variables

View Source
var (
	PageSizeLetter = PageSize{612, 792}  // 8.5 x 11 inches
	PageSizeA4     = PageSize{595, 842}  // 210 x 297 mm
	PageSizeLegal  = PageSize{612, 1008} // 8.5 x 14 inches
	PageSizeA3     = PageSize{842, 1191} // 297 x 420 mm
	PageSizeA5     = PageSize{420, 595}  // 148 x 210 mm
)

Standard page sizes

Functions

func CreateEncryptionDictionary

func CreateEncryptionDictionary(encrypt *types.PDFEncryption) []byte

CreateEncryptionDictionary creates the encryption dictionary object content for V5

func SetupAES256Encryption

func SetupAES256Encryption(userPassword, ownerPassword []byte, fileID []byte, permissions int32, encryptMetadata bool) (*types.PDFEncryption, error)

SetupAES256Encryption creates an encryption dictionary for AES-256 (V5/R5/R6) Based on ISO 32000-2 section 7.6.4

Types

type ContentStream

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

ContentStream builds PDF page content streams

func NewContentStream

func NewContentStream() *ContentStream

NewContentStream creates a new content stream builder

func (*ContentStream) BeginText

func (cs *ContentStream) BeginText() *ContentStream

BeginText starts a text object (BT operator)

func (*ContentStream) Bytes

func (cs *ContentStream) Bytes() []byte

Bytes returns the content stream data

func (*ContentStream) ClosePath

func (cs *ContentStream) ClosePath() *ContentStream

ClosePath closes the current subpath (h operator)

func (*ContentStream) ClosePathStroke

func (cs *ContentStream) ClosePathStroke() *ContentStream

ClosePathStroke closes and strokes the path (s operator)

func (*ContentStream) CurveTo added in v0.8.0

func (cs *ContentStream) CurveTo(x1, y1, x2, y2, x3, y3 float64) *ContentStream

CurveTo appends a cubic Bezier curve (c operator)

func (*ContentStream) CurveToV added in v0.8.0

func (cs *ContentStream) CurveToV(x2, y2, x3, y3 float64) *ContentStream

CurveToV appends a cubic Bezier curve (v operator) - first control point = current point

func (*ContentStream) CurveToY added in v0.8.0

func (cs *ContentStream) CurveToY(x1, y1, x3, y3 float64) *ContentStream

CurveToY appends a cubic Bezier curve (y operator) - second control point = end point

func (*ContentStream) DrawImage

func (cs *ContentStream) DrawImage(imageName string) *ContentStream

DrawImage draws an image XObject (Do operator) imageName should be a resource name like "/Im1" The image is drawn with its lower-left corner at the origin You should use SetMatrix to position and scale it first

func (*ContentStream) DrawImageAt

func (cs *ContentStream) DrawImageAt(imageName string, x, y, width, height float64) *ContentStream

DrawImageAt draws an image at a specific position and size

func (*ContentStream) EndText

func (cs *ContentStream) EndText() *ContentStream

EndText ends a text object (ET operator)

func (*ContentStream) Fill

func (cs *ContentStream) Fill() *ContentStream

Fill fills the current path (f operator)

func (*ContentStream) FillStroke

func (cs *ContentStream) FillStroke() *ContentStream

FillStroke fills and strokes the current path (B operator)

func (*ContentStream) LineTo

func (cs *ContentStream) LineTo(x, y float64) *ContentStream

LineTo appends a line segment (l operator)

func (*ContentStream) MoveTextPosition added in v0.8.0

func (cs *ContentStream) MoveTextPosition(tx, ty float64) *ContentStream

MoveTextPosition moves the text position relative to current position (TD operator) This is equivalent to: SetTextLeading(-ty); SetTextPosition(tx, ty)

func (*ContentStream) MoveTo

func (cs *ContentStream) MoveTo(x, y float64) *ContentStream

MoveTo starts a new subpath (m operator)

func (*ContentStream) NextLine

func (cs *ContentStream) NextLine() *ContentStream

NextLine moves to the next line (T* operator)

func (*ContentStream) Raw

func (cs *ContentStream) Raw(data string) *ContentStream

Raw writes raw content stream data

func (*ContentStream) Rectangle

func (cs *ContentStream) Rectangle(x, y, width, height float64) *ContentStream

Rectangle appends a rectangle (re operator)

func (*ContentStream) RestoreState

func (cs *ContentStream) RestoreState() *ContentStream

RestoreState restores the previous graphics state (Q operator)

func (*ContentStream) SaveState

func (cs *ContentStream) SaveState() *ContentStream

SaveState saves the current graphics state (q operator)

func (*ContentStream) Scale

func (cs *ContentStream) Scale(sx, sy float64) *ContentStream

Scale scales the coordinate system

func (*ContentStream) SetCharSpacing

func (cs *ContentStream) SetCharSpacing(spacing float64) *ContentStream

SetCharSpacing sets character spacing (Tc operator)

func (*ContentStream) SetFillColorGray

func (cs *ContentStream) SetFillColorGray(gray float64) *ContentStream

SetFillColorGray sets the fill color to grayscale (g operator)

func (*ContentStream) SetFillColorRGB

func (cs *ContentStream) SetFillColorRGB(r, g, b float64) *ContentStream

SetFillColorRGB sets the fill color (rg operator)

func (*ContentStream) SetFont

func (cs *ContentStream) SetFont(fontName string, size float64) *ContentStream

SetFont sets the font and size (Tf operator) fontName should be a resource name like "/F1"

func (*ContentStream) SetLineDash added in v0.8.0

func (cs *ContentStream) SetLineDash(dashArray []float64, dashPhase float64) *ContentStream

SetLineDash sets the line dash pattern (d operator) dashArray is the dash pattern, dashPhase is the phase offset

func (*ContentStream) SetLineWidth

func (cs *ContentStream) SetLineWidth(width float64) *ContentStream

SetLineWidth sets the line width (w operator)

func (*ContentStream) SetMatrix

func (cs *ContentStream) SetMatrix(a, b, c, d, e, f float64) *ContentStream

SetMatrix sets the current transformation matrix (cm operator)

func (*ContentStream) SetStrokeColorGray

func (cs *ContentStream) SetStrokeColorGray(gray float64) *ContentStream

SetStrokeColorGray sets the stroke color to grayscale (G operator)

func (*ContentStream) SetStrokeColorRGB

func (cs *ContentStream) SetStrokeColorRGB(r, g, b float64) *ContentStream

SetStrokeColorRGB sets the stroke color (RG operator)

func (*ContentStream) SetTextLeading

func (cs *ContentStream) SetTextLeading(leading float64) *ContentStream

SetTextLeading sets the text leading (TL operator)

func (*ContentStream) SetTextMatrix

func (cs *ContentStream) SetTextMatrix(a, b, c, d, e, f float64) *ContentStream

SetTextMatrix sets the text matrix (Tm operator)

func (*ContentStream) SetTextPosition

func (cs *ContentStream) SetTextPosition(x, y float64) *ContentStream

SetTextPosition sets the text position (Td operator)

func (*ContentStream) SetTextRenderMode added in v0.8.0

func (cs *ContentStream) SetTextRenderMode(mode int) *ContentStream

SetTextRenderMode sets the text rendering mode (Tr operator) Mode: 0=fill, 1=stroke, 2=fill+stroke, 3=invisible, 4=fill+clip, 5=stroke+clip, 6=fill+stroke+clip, 7=clip

func (*ContentStream) SetTextRise

func (cs *ContentStream) SetTextRise(rise float64) *ContentStream

SetTextRise sets text rise (Ts operator)

func (*ContentStream) SetWordSpacing

func (cs *ContentStream) SetWordSpacing(spacing float64) *ContentStream

SetWordSpacing sets word spacing (Tw operator)

func (*ContentStream) ShowText

func (cs *ContentStream) ShowText(text string) *ContentStream

ShowText displays a string (Tj operator)

func (*ContentStream) ShowTextArray added in v0.8.0

func (cs *ContentStream) ShowTextArray(elements []interface{}) *ContentStream

ShowTextArray displays text with individual character positioning (TJ operator) Each element in the array can be either a string or a number (adjustment in thousandths of a text space unit) Example: ShowTextArray([]interface{}{"Hello", -50, "World"}) moves "World" 0.05 units to the left

func (*ContentStream) ShowTextHex added in v0.8.0

func (cs *ContentStream) ShowTextHex(hexString string) *ContentStream

ShowTextHex displays text using hexadecimal string notation (TJ with hex) Useful for characters that are difficult to escape in literal strings

func (*ContentStream) ShowTextNextLine

func (cs *ContentStream) ShowTextNextLine(text string) *ContentStream

ShowTextNextLine moves to next line and shows text (' operator)

func (*ContentStream) String

func (cs *ContentStream) String() string

String returns the content stream as a string

func (*ContentStream) Stroke

func (cs *ContentStream) Stroke() *ContentStream

Stroke strokes the current path (S operator)

func (*ContentStream) Translate

func (cs *ContentStream) Translate(tx, ty float64) *ContentStream

Translate moves the origin

type Dictionary

type Dictionary map[string]interface{}

Dictionary represents a PDF dictionary

type ImageInfo

type ImageInfo struct {
	ObjectNum  int    // Object number of the image XObject
	Width      int    // Image width in pixels
	Height     int    // Image height in pixels
	ColorSpace string // PDF color space name (e.g., "/DeviceRGB")
	Name       string // Resource name (e.g., "/Im1")
}

ImageInfo contains information about an embedded image

type PDFObject

type PDFObject struct {
	Number     int
	Generation int
	Content    []byte     // Raw content (dictionary, stream, etc.)
	Stream     []byte     // Stream data (if this is a stream object)
	Dict       Dictionary // Parsed dictionary (for convenience)
	IsFree     bool
}

PDFObject represents a PDF object with its content

type PDFWriter

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

PDFWriter builds PDF files from scratch

func NewPDFWriter

func NewPDFWriter() *PDFWriter

NewPDFWriter creates a new PDF writer

func (*PDFWriter) AddImage

func (w *PDFWriter) AddImage(imgData []byte, name string) (*ImageInfo, error)

AddImage adds a generic image (PNG, etc.) to the PDF The image is converted to raw RGB/Gray data and compressed with FlateDecode

func (*PDFWriter) AddJPEGImage

func (w *PDFWriter) AddJPEGImage(jpegData []byte, name string) (*ImageInfo, error)

AddJPEGImage adds a JPEG image to the PDF and returns its info JPEG images are embedded directly without re-encoding (DCTDecode)

func (*PDFWriter) AddObject

func (w *PDFWriter) AddObject(content []byte) int

AddObject adds a new object and returns its object number

func (*PDFWriter) AddStreamObject

func (w *PDFWriter) AddStreamObject(dict Dictionary, data []byte, compress bool) int

AddStreamObject adds a stream object with dictionary and data

func (*PDFWriter) Bytes

func (w *PDFWriter) Bytes() ([]byte, error)

Bytes returns the complete PDF as a byte slice

func (*PDFWriter) GetObject

func (w *PDFWriter) GetObject(objNum int) ([]byte, error)

GetObject returns an object's content (for reading existing objects)

func (*PDFWriter) NewPageBuilder

func (w *PDFWriter) NewPageBuilder(size PageSize) *PageBuilder

NewPageBuilder creates a new page builder

func (*PDFWriter) NextObjectNumber

func (w *PDFWriter) NextObjectNumber() int

NextObjectNumber returns the next available object number

func (*PDFWriter) SetEncryptRef

func (w *PDFWriter) SetEncryptRef(objNum int)

SetEncryptRef sets the encrypt dictionary object reference

func (*PDFWriter) SetEncryption

func (w *PDFWriter) SetEncryption(encryptInfo *types.PDFEncryption, fileID []byte)

SetEncryption enables encryption for the output PDF

func (*PDFWriter) SetInfo

func (w *PDFWriter) SetInfo(objNum int)

SetInfo sets the info dictionary object reference

func (*PDFWriter) SetObject

func (w *PDFWriter) SetObject(objNum int, content []byte)

SetObject sets or replaces an object at a specific number

func (*PDFWriter) SetRoot

func (w *PDFWriter) SetRoot(objNum int)

SetRoot sets the root (catalog) object reference

func (*PDFWriter) SetStreamObject

func (w *PDFWriter) SetStreamObject(objNum int, dict Dictionary, data []byte, compress bool)

SetStreamObject sets a stream object at a specific number

func (*PDFWriter) SetVersion

func (w *PDFWriter) SetVersion(version string)

SetVersion sets the PDF version (e.g., "1.7")

func (*PDFWriter) SetupEncryptionWithPasswords

func (w *PDFWriter) SetupEncryptionWithPasswords(userPassword, ownerPassword []byte, permissions int32, encryptMetadata bool) (int, error)

SetupEncryptionWithPasswords is a convenience function that sets up encryption and creates the encryption dictionary object Returns the encryption dictionary object number

func (*PDFWriter) Write

func (w *PDFWriter) Write(out io.Writer) error

Write outputs the complete PDF to the given writer

type PageBuilder

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

PageBuilder helps build PDF pages

func (*PageBuilder) AddEmbeddedFont

func (pb *PageBuilder) AddEmbeddedFont(f *font.Font) (string, error)

AddEmbeddedFont adds an embedded TrueType/OpenType font and returns the resource name The font will be subset to include only the characters added via font.AddString() or font.AddRune()

func (*PageBuilder) AddImage

func (pb *PageBuilder) AddImage(info *ImageInfo) string

AddImage adds an image and returns the resource name

func (*PageBuilder) AddStandardFont

func (pb *PageBuilder) AddStandardFont(fontName string) string

AddStandardFont adds a standard PDF font (Helvetica, Times-Roman, etc.) Returns the resource name to use (e.g., "/F1")

func (*PageBuilder) Build

func (pb *PageBuilder) Build(pagesObjNum int) int

Build finalizes the page and returns the page object number

func (*PageBuilder) Content

func (pb *PageBuilder) Content() *ContentStream

Content returns the content stream for adding graphics/text

type PageSize

type PageSize struct {
	Width  float64
	Height float64
}

PageSize represents standard page dimensions in points (1 point = 1/72 inch)

type SimplePDFBuilder

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

SimplePDFBuilder provides a high-level API for creating simple PDFs

func NewSimplePDFBuilder

func NewSimplePDFBuilder() *SimplePDFBuilder

NewSimplePDFBuilder creates a new simple PDF builder

func (*SimplePDFBuilder) AddPage

func (b *SimplePDFBuilder) AddPage(size PageSize) *PageBuilder

AddPage adds a new page and returns a page builder

func (*SimplePDFBuilder) Bytes

func (b *SimplePDFBuilder) Bytes() ([]byte, error)

Bytes returns the complete PDF

func (*SimplePDFBuilder) FinalizePage

func (b *SimplePDFBuilder) FinalizePage(pb *PageBuilder)

FinalizePage adds a built page to the document

func (*SimplePDFBuilder) Pages

func (b *SimplePDFBuilder) Pages() []int

Pages returns the list of page object numbers

func (*SimplePDFBuilder) PagesObjNum

func (b *SimplePDFBuilder) PagesObjNum() int

PagesObjNum returns the pages object number

func (*SimplePDFBuilder) Writer

func (b *SimplePDFBuilder) Writer() *PDFWriter

Writer returns the underlying PDF writer for advanced operations

type XFABuilder

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

XFABuilder builds PDFs with XFA content

func NewXFABuilder

func NewXFABuilder(verbose bool) *XFABuilder

NewXFABuilder creates a new XFA PDF builder

func (*XFABuilder) BuildFromOriginal

func (b *XFABuilder) BuildFromOriginal(originalPDF []byte, updatedStreams map[string][]byte, encryptInfo *types.PDFEncryption) ([]byte, error)

BuildFromOriginal rebuilds a PDF preserving structure but updating XFA streams

func (*XFABuilder) BuildFromXFA

func (b *XFABuilder) BuildFromXFA(streams []XFAStreamData) ([]byte, error)

BuildFromXFA creates a complete PDF from XFA stream data

type XFAStreamData

type XFAStreamData struct {
	Name     string
	Data     []byte
	Compress bool
}

XFAStreamData represents XFA stream data with its name

Jump to

Keyboard shortcuts

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