pdfparser

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

pdf_parser.go - 修复后的版本

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsValidPDFPath

func IsValidPDFPath(pdfPath string) error

IsValidPDFPath 检查PDF文件是否存在

Types

type DocumentStructure

type DocumentStructure struct {
	TotalPages   int            `json:"total_pages"`
	TotalTables  int            `json:"total_tables"`
	TotalLines   int            `json:"total_lines"`
	TableContent []TableSummary `json:"table_content"`
	TextBlocks   []TextBlock    `json:"text_blocks"`
}

DocumentStructure 文档结构分析结果

type Image

type Image struct {
	Index            int    `json:"index"`
	Position         []int  `json:"position"` // [x0, y0, x1, y1]
	Width            int    `json:"width"`
	Height           int    `json:"height"`
	Data             string `json:"data"`                        // base64编码的图片数据
	Format           string `json:"format"`                      // 图片格式
	SizeBytes        int    `json:"size_bytes,omitempty"`        // 图片大小(字节)
	ExtractionMethod string `json:"extraction_method,omitempty"` // 提取方法:pdfimages, pymupdf, page_render
}

Image 表示提取的图片

type LineGroup

type LineGroup struct {
	Y        float64       // 行的Y坐标
	Elements []TextElement // 该行的所有元素
}

LineGroup 表示一行文本

type PDFParser

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

PDFParser PDF解析器。构造时必须显式注入 Runner。

func NewParser

func NewParser(runner Runner, opts ...ParserOption) (*PDFParser, error)

NewParser 使用显式 Runner 构造 parser。

func (*PDFParser) ParsePDF

func (p *PDFParser) ParsePDF(pdfPath string, needCharacter bool) (*TableResponse, error)

ParsePDF 解析PDF文件

func (*PDFParser) ParsePDFContext

func (p *PDFParser) ParsePDFContext(ctx context.Context, pdfPath string, needCharacter bool) (*TableResponse, error)

ParsePDFContext 解析 PDF 并传播调用方 cancellation/deadline。

func (*PDFParser) ParsePDFSafe

func (p *PDFParser) ParsePDFSafe(pdfPath string, needCharacter bool) (*TableResponse, error)

ParsePDFSafe 安全的PDF解析,带预检查

func (*PDFParser) ParsePDFToLines

func (p *PDFParser) ParsePDFToLines(pdfPath string, needCharacter bool) ([]string, error)

ParsePDFToLines 解析PDF并返回行数组

func (*PDFParser) ParsePDFToPages

func (p *PDFParser) ParsePDFToPages(pdfPath string, needCharacter bool) ([]string, error)

ParsePDFToPages 解析PDF并返回按页面分组的文本数组

func (*PDFParser) ParsePDFToText

func (p *PDFParser) ParsePDFToText(pdfPath string, needCharacter bool) (string, error)

ParsePDFToText 解析PDF并返回按行规整的文本

func (*PDFParser) ParsePDFToTextOnly

func (p *PDFParser) ParsePDFToTextOnly(pdfPath string, needCharacter bool) (string, error)

ParsePDFToTextOnly 解析PDF并返回纯文本(不包含表格格式)

func (*PDFParser) ParsePDFWithHybrid

func (p *PDFParser) ParsePDFWithHybrid(pdfPath string, needCharacter bool) (*TableResponse, error)

ParsePDFWithHybrid 使用混合模式解析PDF的便捷方法

func (*PDFParser) ParsePDFWithOptions

func (p *PDFParser) ParsePDFWithOptions(pdfPath string, opts *PDFParserOptions) (*TableResponse, error)

ParsePDFWithOptions 使用自定义选项解析PDF

func (*PDFParser) ParsePDFWithOptionsContext

func (p *PDFParser) ParsePDFWithOptionsContext(ctx context.Context, pdfPath string, opts *PDFParserOptions) (*TableResponse, error)

ParsePDFWithOptionsContext 使用显式 options 并传播调用方 context。

func (*PDFParser) ParsePDFWithOptionsToText

func (p *PDFParser) ParsePDFWithOptionsToText(pdfPath string, opts *PDFParserOptions) (string, error)

ParsePDFWithOptionsToText 使用自定义选项解析PDF并返回文本

func (*PDFParser) ParsePDFWithPdfplumber

func (p *PDFParser) ParsePDFWithPdfplumber(pdfPath string, needCharacter bool) (*TableResponse, error)

ParsePDFWithPdfplumber 使用pdfplumber解析PDF的便捷方法

type PDFParserOptions

type PDFParserOptions struct {
	NeedCharacter    bool   // 是否需要字符级信息
	ExtractImages    bool   // 是否提取图片
	OutputFormat     string // 输出格式: json, text, html
	PageRange        string // 页面范围: "1-5", "1,3,5", "all"
	TableEngine      string // 表格引擎: "pdfplumber", "pymupdf", "hybrid"
	HighAccuracyMode bool   // 高精度模式(启用Camelot兜底,速度较慢但精度更高)
}

PDFParserOptions 解析器选项(清理版)

func DefaultOptions

func DefaultOptions() *PDFParserOptions

DefaultOptions 返回默认选项

func NewHybridOptions

func NewHybridOptions() *PDFParserOptions

NewHybridOptions 创建混合模式选项

func NewPdfplumberOptions

func NewPdfplumberOptions() *PDFParserOptions

NewPdfplumberOptions 创建使用pdfplumber的选项

func NewPyMuPDFOptions

func NewPyMuPDFOptions() *PDFParserOptions

NewPyMuPDFOptions 创建只使用PyMuPDF的选项

func (*PDFParserOptions) ValidateOptions

func (opts *PDFParserOptions) ValidateOptions() error

ValidateOptions 验证选项参数

type ParserOption

type ParserOption func(*PDFParser)

ParserOption 配置 parser 本身而非具体 backend。

func WithTimeout

func WithTimeout(timeout time.Duration) ParserOption

WithTimeout 设置单次调用的默认上限。零值表示使用五分钟兼容默认值。

type RunRequest

type RunRequest struct {
	PDFPath string
	Options PDFParserOptions
}

RunRequest 是发送给显式 Runner 的解析请求。

type RunResult

type RunResult struct {
	Stdout []byte
	Stderr []byte
}

RunResult 保留 adapter 的 stdout/stderr,供兼容错误投影与诊断使用。

type Runner

type Runner interface {
	Run(context.Context, RunRequest) (RunResult, error)
}

Runner 是 PDF parser 唯一允许的外部执行端口。实现可以调用 Python、远程服务或 deterministic fake;canonical parser 不发现解释器、不读取环境变量,也不自行启动进程。

type SearchResult

type SearchResult struct {
	PageNumber    int    `json:"page_number"`
	ElementNumber int    `json:"element_number"`        // 元素序号(在页面中的顺序)
	LineNumber    int    `json:"line_number,omitempty"` // 在plain元素中的行号
	TableNumber   int    `json:"table_number,omitempty"`
	CellRow       int    `json:"cell_row,omitempty"`
	CellCol       int    `json:"cell_col,omitempty"`
	Text          string `json:"text"`
	Position      []int  `json:"position"`
	InTable       bool   `json:"in_table"`
	ElementType   string `json:"element_type"` // plain, table_with_line, table_without_line
}

SearchResult 搜索结果

type TLine

type TLine struct {
	Angle       int     `json:"angle"`
	Text        string  `json:"text"`
	Direction   int     `json:"direction"`
	Handwritten int     `json:"handwritten"`
	Position    []int   `json:"position"` // [x0, y0, x1, y1]
	Score       float64 `json:"score"`
	Type        string  `json:"type"`
	// 以下字段在 needCharacter 为 true 时返回
	CharAttributes      []string    `json:"char_attributes,omitempty"`
	CharCandidates      [][]string  `json:"char_candidates,omitempty"`
	CharCandidatesScore [][]float64 `json:"char_candidates_score,omitempty"`
	CharCenters         [][]int     `json:"char_centers,omitempty"`
	CharPositions       [][]int     `json:"char_positions,omitempty"`
	CharScores          []float64   `json:"char_scores,omitempty"`
}

TLine 表示 OCR 识别的文本行,包含字符级信息

type TPage

type TPage struct {
	Angle  int     `json:"angle"`
	Height int     `json:"height"`
	Width  int     `json:"width"`
	Tables []Table `json:"tables"`
	Images []Image `json:"images,omitempty"` // 图片信息(可选)
}

TPage 表示每个页面的解析结果

type TResult

type TResult struct {
	Pages []TPage `json:"pages"`
}

TResult 包含 Pages

type Table

type Table struct {
	HeightOfRows []int       `json:"height_of_rows"`
	Type         string      `json:"type"`
	TableCells   []TableCell `json:"table_cells"`
	TableRows    int         `json:"table_rows"`
	WidthOfCols  []int       `json:"width_of_cols"`
	Position     []int       `json:"position"`
	Lines        []TLine     `json:"lines"`
	TableCols    int         `json:"table_cols"`
}

Table 表示表格结构

type TableCell

type TableCell struct {
	StartRow int    `json:"start_row"`
	StartCol int    `json:"start_col"`
	EndRow   int    `json:"end_row"`
	EndCol   int    `json:"end_col"`
	Text     string `json:"text"`
	Borders  struct {
		Right  int `json:"right"`
		Bottom int `json:"bottom"`
		Left   int `json:"left"`
		Top    int `json:"top"`
	} `json:"borders"`
	Position []int   `json:"position"`
	Lines    []TLine `json:"lines"`
}

TableCell 表示表格单元格

type TableExporter

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

TableExporter 表格导出器

func NewTableExporter

func NewTableExporter(response *TableResponse) *TableExporter

NewTableExporter 创建表格导出器

func (*TableExporter) ExportToCSV

func (e *TableExporter) ExportToCSV(outputDir string) error

ExportToCSV 导出表格到CSV文件

func (*TableExporter) ExportToHTML

func (e *TableExporter) ExportToHTML(outputFile string) error

ExportToHTML 导出到HTML格式,参考table.go中的buildPageHTML方法

type TableResponse

type TableResponse struct {
	Msg      string  `json:"message"` // 注意:Python返回的是"message",不是"msg"
	Code     int     `json:"code"`
	Version  string  `json:"version"`
	Duration int     `json:"duration"`
	Result   TResult `json:"result"`
}

TableResponse 表示完整的 PDF解析 API 响应

type TableSummary

type TableSummary struct {
	PageNumber    int    `json:"page_number"`
	ElementNumber int    `json:"element_number"`
	Rows          int    `json:"rows"`
	Cols          int    `json:"cols"`
	Position      []int  `json:"position"`
	CellCount     int    `json:"cell_count"`
	TableType     string `json:"table_type"`
}

TableSummary 表格摘要

type TextAnalyzer

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

TextAnalyzer 文本分析器

func NewTextAnalyzer

func NewTextAnalyzer(response *TableResponse) *TextAnalyzer

NewTextAnalyzer 创建文本分析器

func (*TextAnalyzer) AnalyzeStructure

func (a *TextAnalyzer) AnalyzeStructure() *DocumentStructure

AnalyzeStructure 分析文档结构

func (*TextAnalyzer) SearchText

func (a *TextAnalyzer) SearchText(keyword string) []SearchResult

SearchText 在文档中搜索文本

type TextBlock

type TextBlock struct {
	PageNumber    int     `json:"page_number"`
	ElementNumber int     `json:"element_number"`
	LineCount     int     `json:"line_count"`
	Lines         []TLine `json:"lines"`
	Position      []int   `json:"position"`
}

TextBlock 文本块

type TextElement

type TextElement struct {
	Text     string  // 文本内容
	X        float64 // X坐标
	Y        float64 // Y坐标
	Width    float64 // 宽度
	Height   float64 // 高度
	FontSize float64 // 字体大小
	IsTable  bool    // 是否为表格
}

TextElement 表示一个可排序的文本元素

type TextFormatter

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

TextFormatter 文本格式化器

func NewTextFormatter

func NewTextFormatter(response *TableResponse) *TextFormatter

NewTextFormatter 创建文本格式化器

func (*TextFormatter) ExtractTextOnly

func (f *TextFormatter) ExtractTextOnly() string

ExtractTextOnly 提取纯文本内容(不包含表格格式)

func (*TextFormatter) FormatPageToLines

func (f *TextFormatter) FormatPageToLines(pageNum int) []string

FormatPageToLines 格式化指定页面为行数组

func (*TextFormatter) FormatToLines

func (f *TextFormatter) FormatToLines() []string

FormatToLines 将结果格式化为行数组

func (*TextFormatter) FormatToPages

func (f *TextFormatter) FormatToPages() []string

FormatToPages 将TableResponse格式化为按页面分组的文本数组

func (*TextFormatter) FormatToPagesWithEmptyFilter

func (f *TextFormatter) FormatToPagesWithEmptyFilter(includeEmpty bool) []string

FormatToPagesWithEmptyFilter 将TableResponse格式化为按页面分组的文本数组,可选择是否包含空页面

func (*TextFormatter) FormatToText

func (f *TextFormatter) FormatToText() string

FormatToText 将TableResponse格式化为按行规整的文本

Directories

Path Synopsis
Package python adapts an explicitly selected Python runtime to pdf.Runner.
Package python adapts an explicitly selected Python runtime to pdf.Runner.

Jump to

Keyboard shortcuts

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