render

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package render provides PDF-to-image rendering via go-pdfium. Used by ONNX layout detection and OCR fallback to produce page images.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrRendererNotInitialized = errors.New("renderer not initialized")
	ErrInvalidPage            = errors.New("invalid page number")
	ErrRenderFailed           = errors.New("failed to render page")
	ErrPDFNotLoaded           = errors.New("PDF not loaded")
)

Common errors for the render package.

Functions

func ClosePdfiumPool

func ClosePdfiumPool()

ClosePdfiumPool closes the global pdfium pool. Call this at program exit to cleanly release resources.

Types

type NewRendererOptions

type NewRendererOptions struct {
	// PoolSize is the number of renderer instances to pool.
	// go-pdfium benefits from pooling for concurrent rendering.
	PoolSize int
}

NewRendererOptions holds options for creating a new renderer.

func DefaultRendererOptions

func DefaultRendererOptions() *NewRendererOptions

DefaultRendererOptions returns the default renderer options.

type PageRenderer

type PageRenderer interface {
	// RenderPage renders a specific page to an image.
	// pageIndex is 0-based.
	// dpi controls the resolution (72 = 1:1, 150 = 2x, etc.)
	RenderPage(pageIndex int, dpi float64) (image.Image, error)

	// PageCount returns the total number of pages in the document.
	PageCount() int

	// PageSize returns the size of a page in PDF points (1/72 inch).
	// pageIndex is 0-based.
	PageSize(pageIndex int) (width, height float64, err error)

	// Close releases all resources.
	Close() error
}

PageRenderer renders PDF pages to images. This interface abstracts the PDF rendering implementation, allowing for different backends (go-pdfium, poppler, etc.).

func DefaultRendererFactory

func DefaultRendererFactory(pdfPath string) (PageRenderer, error)

DefaultRendererFactory creates a PdfiumRenderer. This is the default factory for PageRenderer creation.

type PdfiumRenderer

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

PdfiumRenderer implements PageRenderer using go-pdfium WebAssembly backend.

func NewPdfiumRenderer

func NewPdfiumRenderer(pdfPath string, config *RenderConfig) (*PdfiumRenderer, error)

NewPdfiumRenderer creates a new renderer for the given PDF file. Uses go-pdfium with WebAssembly/Wazero backend for cross-platform compatibility.

func (*PdfiumRenderer) Close

func (r *PdfiumRenderer) Close() error

Close releases all resources.

func (*PdfiumRenderer) PageCount

func (r *PdfiumRenderer) PageCount() int

PageCount returns the total number of pages in the document.

func (*PdfiumRenderer) PageSize

func (r *PdfiumRenderer) PageSize(pageIndex int) (width, height float64, err error)

PageSize returns the size of a page in PDF points (1/72 inch). pageIndex is 0-based.

func (*PdfiumRenderer) RenderPage

func (r *PdfiumRenderer) RenderPage(pageIndex int, dpi float64) (image.Image, error)

RenderPage renders a specific page to an image. pageIndex is 0-based.

type RenderConfig

type RenderConfig struct {
	// DPI for rendering (default: 150 for good ONNX input quality)
	DPI float64

	// MaxDimension limits the maximum width or height of rendered images.
	// 0 means no limit. This helps manage memory for large pages.
	MaxDimension int

	// BackgroundColor for transparent pages (default: white)
	BackgroundColor [3]uint8
}

RenderConfig holds configuration for page rendering.

func DefaultRenderConfig

func DefaultRenderConfig() *RenderConfig

DefaultRenderConfig returns the default rendering configuration.

func RenderConfigForONNX

func RenderConfigForONNX() *RenderConfig

RenderConfigForONNX returns configuration optimized for ONNX model input. Uses 150 DPI which typically produces images around 1200x1500 pixels for standard letter/A4 pages, suitable for the 1024x1024 YOLO input.

type RendererFactory

type RendererFactory func(pdfPath string) (PageRenderer, error)

RendererFactory creates PageRenderer instances. This is a function type for dependency injection.

func RendererFactoryWithConfig

func RendererFactoryWithConfig(config *RenderConfig) RendererFactory

RendererFactoryWithConfig creates a factory with specific config.

Jump to

Keyboard shortcuts

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