pdf

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: May 4, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// PageSizeLetter is 8.5x11 inches at 72 dpi.
	PageSizeLetter = PageSize{Width: 612, Height: 792}
	// PageSizeA4 is the standard A4 paper size at 72 dpi.
	PageSizeA4 = PageSize{Width: 595, Height: 842}
)

Functions

func CreateBackgroundPDF added in v0.15.0

func CreateBackgroundPDF(
	imageReader io.Reader,
	markdownText string,
	opts BackgroundPDFOptions,
) ([]byte, error)

CreateBackgroundPDF creates a PDF with a background image, optional logo, and text overlay. The background image is scaled to cover the page (minimum scaling to fill, then center-cropped). Text is rendered from either TextBlocks (if provided) or Markdown with basic formatting support. Logo is placed according to LogoOptions if provided.

func CreateBackgroundPDFFile added in v0.15.0

func CreateBackgroundPDFFile(
	imagePath string,
	markdownText string,
	opts BackgroundPDFOptions,
	outPath string,
) error

CreateBackgroundPDFFile is a convenience wrapper that reads an image file, creates the PDF, and writes it to an output file. If opts.LogoPath is set and opts.Logo is configured, the logo will be added.

func CreateMultiPagePDF added in v0.15.0

func CreateMultiPagePDF(pages []Page, pageSize PageSize) ([]byte, error)

CreateMultiPagePDF creates a PDF with multiple pages, each with its own background, logo, and text.

func CreateMultiPagePDFFile added in v0.15.0

func CreateMultiPagePDFFile(pages []Page, pageSize PageSize, outPath string) error

CreateMultiPagePDFFile creates a multi-page PDF and writes it to outPath.

func StripMarkdown added in v0.15.0

func StripMarkdown(md string) string

StripMarkdown removes all markdown formatting and returns plain text.

func WriteFileMerged

func WriteFileMerged(filename string, srcFilenames []string, conf *model.Configuration) error

Types

type BackgroundPDFOptions added in v0.15.0

type BackgroundPDFOptions struct {
	PageSize   PageSize
	TextStyle  TextStyle
	TextBlocks []TextBlock  // If set, used instead of markdown text for precise control
	LogoReader io.Reader    // Logo image reader (used with CreateBackgroundPDF)
	LogoPath   string       // Logo image path (used with CreateBackgroundPDFFile)
}

BackgroundPDFOptions configures PDF generation.

func DefaultBackgroundPDFOptions added in v0.15.0

func DefaultBackgroundPDFOptions() BackgroundPDFOptions

DefaultBackgroundPDFOptions returns default options for letter size with standard text styling.

type LogoOptions added in v0.15.0

type LogoOptions struct {
	Position LogoPosition // Anchor position (e.g., "tl", "br", "c")
	OffsetX  float64      // Horizontal offset from anchor in points
	OffsetY  float64      // Vertical offset from anchor in points
	Scale    float64      // Scale factor (0.0-1.0 relative, or absolute if ScaleAbs is true)
	ScaleAbs bool         // If true, Scale represents absolute width in points
	Opacity  float64      // Opacity (0.0-1.0), defaults to 1.0 if zero
}

LogoOptions configures logo placement on the PDF.

func DefaultLogoOptions added in v0.15.0

func DefaultLogoOptions() LogoOptions

DefaultLogoOptions returns sensible defaults for logo placement.

type LogoPosition added in v0.15.0

type LogoPosition string

LogoPosition represents anchor positions for logo placement.

const (
	LogoPositionTopLeft      LogoPosition = "tl"
	LogoPositionTopCenter    LogoPosition = "tc"
	LogoPositionTopRight     LogoPosition = "tr"
	LogoPositionLeft         LogoPosition = "l"
	LogoPositionCenter       LogoPosition = "c"
	LogoPositionRight        LogoPosition = "r"
	LogoPositionBottomLeft   LogoPosition = "bl"
	LogoPositionBottomCenter LogoPosition = "bc"
	LogoPositionBottomRight  LogoPosition = "br"
)

type Page added in v0.15.0

type Page struct {
	BackgroundReader io.Reader    // Background image reader (nil = white page)
	BackgroundPath   string       // Background image path (file-based alternative)
	LogoReader       io.Reader    // Logo image reader
	LogoPath         string       // Logo image path
	TextStyle        TextStyle    // Base text style
	TextBlocks       []TextBlock  // Styled text blocks (takes precedence over MarkdownText)
	MarkdownText     string       // Markdown text (used if TextBlocks is empty)
}

Page represents a single page in a multi-page PDF.

type PageSize added in v0.15.0

type PageSize struct {
	Width  float64
	Height float64
}

PageSize represents standard page dimensions in points (72 dpi).

type TextAlign added in v0.15.0

type TextAlign string

TextAlign represents horizontal text alignment.

const (
	TextAlignLeft   TextAlign = "left"
	TextAlignCenter TextAlign = "center"
	TextAlignRight  TextAlign = "right"
)

type TextBlock added in v0.15.0

type TextBlock struct {
	Text      string    // The text to render
	FontName  string    // Override font (empty = inherit from TextStyle)
	FontSize  float64   // Override font size in points (0 = inherit from TextStyle)
	Color     string    // Override color (empty = inherit from TextStyle)
	MarginTop float64   // Additional top margin before this block in points
	Align     TextAlign // Override alignment (empty = inherit from TextStyle)
}

TextBlock represents a single block of text with optional style overrides. Empty values inherit from the parent TextStyle.

type TextSegment added in v0.15.0

type TextSegment struct {
	Text        string
	IsHeader    bool
	HeaderLevel int  // 1-6 for headers
	IsBold      bool // text was wrapped in **
	IsItalic    bool // text was wrapped in *
}

TextSegment represents a parsed segment of markdown text with styling information.

func ParseMarkdownSegments added in v0.15.0

func ParseMarkdownSegments(md string) []TextSegment

ParseMarkdownSegments parses markdown text into a slice of TextSegments. This is a simple line-by-line parser that handles:

  • Headers (# to ######)
  • Bold (**text**)
  • Italic (*text*)
  • Plain paragraphs

Note: This strips formatting markers for plain text rendering since pdfcpu's TextWatermark doesn't support rich text styling. The IsBold and IsItalic flags are preserved for potential future use with fonts that support it.

type TextStyle added in v0.15.0

type TextStyle struct {
	FontName    string    // e.g., "Helvetica", "Courier"
	FontSize    float64   // points
	Color       string    // e.g., "0 0 0" (RGB) or "#000000"
	MarginTop   float64   // points from top
	MarginLeft  float64   // points from left
	MarginRight float64   // points from right (used to calculate line width)
	LineHeight  float64   // multiplier (e.g., 1.5)
	Align       TextAlign // horizontal alignment (default: left)
}

TextStyle defines styling for text overlay.

func DefaultTextStyle added in v0.15.0

func DefaultTextStyle() TextStyle

DefaultTextStyle returns a reasonable default text style.

Jump to

Keyboard shortcuts

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