pdf

package
v0.7.4 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package pdf renders HTML documents to PDF using Chromium.

Package pdf renders HTML documents to PDF using Chromium.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckChromiumAvailable

func CheckChromiumAvailable() error

CheckChromiumAvailable reports whether Chrome or Chromium is installed.

func WarnIfCJKFontsMissing added in v0.3.1

func WarnIfCJKFontsMissing(htmlContent string, logger interface{ Warn(string, ...any) })

WarnIfCJKFontsMissing checks whether the HTML content contains CJK characters and warns the user if no CJK fonts are installed on the system. This is a best-effort check — it logs a warning but does not block PDF generation.

Types

type Generator

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

Generator converts HTML into PDF files.

func NewGenerator

func NewGenerator(opts ...GeneratorOption) *Generator

NewGenerator creates a PDF generator. By default, it generates a document outline (clickable bookmarks) and tagged PDF.

func (*Generator) Generate

func (g *Generator) Generate(htmlContent string, outputPath string) error

Generate renders an HTML string to a PDF file.

func (*Generator) GenerateFromFile

func (g *Generator) GenerateFromFile(htmlFilePath string, outputPath string) error

GenerateFromFile renders a local HTML file to PDF.

type GeneratorOption

type GeneratorOption func(*Generator)

GeneratorOption customizes a PDF generator.

func WithDocumentOutline

func WithDocumentOutline(enable bool) GeneratorOption

WithDocumentOutline toggles PDF bookmark/outline generation from heading hierarchy. Enabled by default. Requires Chrome 128+ for full support.

func WithFooterTemplate added in v0.4.0

func WithFooterTemplate(tmpl string) GeneratorOption

WithFooterTemplate sets a custom HTML footer template for PDF pages. The template is rendered by Chrome's PrintToPDF and supports CSS styling. Chrome provides special classes: "pageNumber", "totalPages", "date", "title", "url".

func WithHeaderFooter

func WithHeaderFooter(enable bool) GeneratorOption

WithHeaderFooter toggles header and footer rendering.

func WithMarginStrings added in v0.4.0

func WithMarginStrings(left, right, top, bottom string) GeneratorOption

WithMarginStrings sets page margins from string values (e.g., "20mm", "1in"). Uses default values if parsing fails.

func WithMargins

func WithMargins(left, right, top, bottom float64) GeneratorOption

WithMargins sets page margins in millimeters.

func WithPageSize

func WithPageSize(width, height float64) GeneratorOption

WithPageSize sets the page size in millimeters.

func WithPrintBackground

func WithPrintBackground(print bool) GeneratorOption

WithPrintBackground toggles background printing.

func WithTaggedPDF

func WithTaggedPDF(enable bool) GeneratorOption

WithTaggedPDF toggles tagged (accessible) PDF generation. Enabled by default. Tagged PDFs include structural metadata for screen readers.

func WithTimeout

func WithTimeout(d time.Duration) GeneratorOption

WithTimeout sets the operation timeout.

type PDFRenderer

type PDFRenderer interface {
	Generate(htmlContent string, outputPath string) error
	GenerateFromFile(htmlFilePath string, outputPath string) error
}

PDFRenderer abstracts PDF generation so tests can use a mock.

Jump to

Keyboard shortcuts

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