pdfextract

package
v2.9.0 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Extract

func Extract(ctx context.Context, pdfBackend PDFBackend, ocrBackend OCRBackend, filePath string) ([]string, error)

Extract processes a PDF file and returns its extracted text. If an OCRBackend is provided and a page is sparse, it will fall back to OCR.

Types

type OCRBackend

type OCRBackend interface {
	// Available returns true if the OCR backend is installed and executable.
	Available() bool
	// ValidateLang checks if the configured language data is available.
	ValidateLang(ctx context.Context) error
	// OCRPage performs OCR on the specified image file and returns the extracted text.
	OCRPage(ctx context.Context, imagePath string) (string, error)
}

OCRBackend is the interface for performing OCR on images.

type PDFBackend

type PDFBackend interface {
	// ExtractText extracts text from the PDF file, returning a slice where each element is a page's text.
	ExtractText(ctx context.Context, filePath string) ([]string, error)
	// RenderPage renders the specified page (1-indexed) to a temporary image file and returns the path.
	RenderPage(ctx context.Context, filePath string, pageNum int) (string, error)
	// Close releases any resources associated with the backend.
	Close() error
}

PDFBackend is the interface for extracting text and rendering pages from PDF files.

type PDFChunk

type PDFChunk struct {
	PageNum int
	Text    string
}

PDFChunk represents a chunk of text extracted from a PDF.

func ChunkPages

func ChunkPages(pages []string, minWords, maxRunes, overlap int) []PDFChunk

ChunkPages takes a slice of page texts, strips repetitive headers/footers, drops sparse pages, and splits long pages into smaller overlapping chunks.

type PDFiumBackend

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

PDFiumBackend implements PDFBackend using go-pdfium via WASM.

func NewPDFiumBackend

func NewPDFiumBackend() (*PDFiumBackend, error)

NewPDFiumBackend initializes a new PDFium WebAssembly backend.

func (*PDFiumBackend) Close

func (b *PDFiumBackend) Close() error

Close releases the PDFium instance and pool.

func (*PDFiumBackend) ExtractText

func (b *PDFiumBackend) ExtractText(_ context.Context, filePath string) ([]string, error)

ExtractText returns the text of each page in the PDF document as a slice of strings.

func (*PDFiumBackend) RenderPage

func (b *PDFiumBackend) RenderPage(_ context.Context, filePath string, pageNum int) (string, error)

RenderPage renders the specified page of a PDF document to a JPEG image file in a temporary directory.

type TesseractBackend

type TesseractBackend struct {
	BinPath string
	Lang    string
}

TesseractBackend implements OCRBackend using the tesseract CLI tool.

func NewTesseractBackend

func NewTesseractBackend(binPath, lang string) *TesseractBackend

NewTesseractBackend creates a new TesseractBackend.

func (*TesseractBackend) Available

func (b *TesseractBackend) Available() bool

Available returns true if the tesseract binary is executable.

func (*TesseractBackend) OCRPage

func (b *TesseractBackend) OCRPage(ctx context.Context, imagePath string) (string, error)

OCRPage performs OCR on the specified image file and returns the extracted text.

func (*TesseractBackend) ValidateLang

func (b *TesseractBackend) ValidateLang(ctx context.Context) error

ValidateLang checks that the configured language data is available.

Jump to

Keyboard shortcuts

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