Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix(parser): preserve PaddleOCR PDF highlight positions (#21205)

jay77721f402c001be35
diff --git a/internal/deepdoc/parser/pdf/pdfium/page_sizes_test.go b/internal/deepdoc/parser/pdf/pdfium/page_sizes_test.go
new file mode 100644
index 0000000000..83d9243bfd
--- /dev/null
+++ b/internal/deepdoc/parser/pdf/pdfium/page_sizes_test.go
@@ -0,0 +1,35 @@
+//go:build cgo
+
+package pdfium
+
+import (
+ "context"
+ "errors"
+ "os"
+ "reflect"
+ "testing"
+)
+
+func TestPageSizesKeepsValidRequestedPages(t *testing.T) {
+ data, err := os.ReadFile("testdata/page_sizes.pdf")
+ if err != nil {
+ t.Fatal(err)
+ }
+ sizes, count, err := PageSizes(t.Context(), data, []int{2, 99, 0, 2})
+ if err == nil {
+ t.Fatal("expected invalid page error")
+ }
+ want := map[int][2]float64{0: {595.5, 842}, 2: {300, 400}}
+ if count != 3 || !reflect.DeepEqual(sizes, want) {
+ t.Fatalf("count=%d sizes=%v", count, sizes)
+ }
+}
+
+func TestPageSizesHonorsCancellation(t *testing.T) {
+ ctx, cancel := context.WithCancel(t.Context())
+ cancel()
+ _, _, err := PageSizes(ctx, []byte("%PDF-1.4"), []int{0})
+ if !errors.Is(err, context.Canceled) {
+ t.Fatalf("error=%v", err)
+ }
+}
diff --git a/internal/deepdoc/parser/pdf/pdfium/pdfium.go b/internal/deepdoc/parser/pdf/pdfium/pdfium.go
index 70948a4662..6ddf8141f7 100644
--- a/internal/deepdoc/parser/pdf/pdfium/pdfium.go
+++ b/internal/deepdoc/parser/pdf/pdfium/pdfium.go
@@ -45,6 +45,8 @@ extern int FPDFDest_GetDestPageIndex(FPDF_DOCUMENT document, FPDF_DES
*/
import "C"
import (
+ "context"
+ "errors"
"fmt"
"image"
"image/color"
@@ -68,20 +70,56 @@ var initOnce sync.Once
// Init initializes the PDFium library. Safe to call multiple times.
func Init() { initOnce.Do(func() { C.FPDF_InitLibrary() }) }
-// PageSize returns the page dimensions in PDF points (1/72 inch) as seen
-// after rotation. For a page with /Rotate 90 on A4, this returns ~842×595
-// (swapped from the MediaBox 595×842). The call is cheap — it opens the
-// document and page, reads dimensions, then closes.
-func PageSize(pdfData []byte, pageIdx int) (width, height float64, err error) {
+// PageSize returns the post-rotation page dimensions in PDF points.
+func PageSize(pdfData []byte, pageIdx int) (float64, float64, error) {
+ sizes, _, err := PageSizes(context.Background(), pdfData, []int{pageIdx})
+ size := sizes[pageIdx]
+ return size[0], size[1], err
+}
+
+// PageSizes reads requested zero-based pages from one open document. Failed
+// pages are omitted; valid sizes and the document page count remain available.
+func PageSizes(ctx context.Context, pdfData []byte, pageIndexes []int) (sizes map[int][2]float64, count int, err error) {
pdfsync.With(func() {
+ if err = ctx.Err(); err != nil {
+ return
+ }
Init()
- _, _, pw, ph, closeAll, e := openPage(pdfData, pageIdx)
- if e != nil {
- err = e
+ data := C.CBytes(pdfData)
+ defer C.free(data)
+ doc := C.FPDF_LoadMemDocument(data, C.int(len(pdfData)), nil)
+ if doc == nil {
+ err = fmt.Errorf("pdfium: FPDF_LoadMemDocument returned nil")
return
}
- closeAll()
- width, height = pw, ph
+ defer C.FPDF_CloseDocument(doc)
+ count = int(C.FPDF_GetPageCount(doc))
+ sizes = make(map[int][2]float64, len(pageIndexes))
+ for _, index := range pageIndexes {
+ if cancelErr := ctx.Err(); cancelErr != nil {
+ err = errors.Join(err, cancelErr)
+ return
+ }
+ if index < 0 || index >= count {
+ err = errors.Join(err, fmt.Errorf("pdfium: page index %d out of range [0,%d)", index, count))
+ continue
+ }
+ if _, ok := sizes[index]; ok {
+ continue
+ }
+ page := C.FPDF_LoadPage(doc, C.int(index))
+ if page == nil {
+ err = errors.Join(err, fmt.Errorf("pdfium: FPDF_LoadPage(%d) returned nil", index))
+ continue
+ }
+ width, height := float64(C.FPDF_GetPageWidth(page)), float64(C.FPDF_GetPageHeight(page))
+ C.FPDF_ClosePage(page)
+ if width <= 0 || height <= 0 {
+ err = errors.Join(err, fmt.Errorf("pdfium: page %d has invalid dimensions %.1fx%.1f", index, width, height))
+ continue
+ }
+ sizes[index] = [2]float64{width, height}
+ }
})
return
}
diff --git a/internal/deepdoc/parser/pdf/pdfium/testdata/page_sizes.pdf b/internal/deepdoc/parser/pdf/pdfium/testdata/page_sizes.pdf
new file mode 100644
index 0000000000..553eef427e
--- /dev/null
+++ b/internal/deepdoc/parser/pdf/pdfium/testdata/page_sizes.pdf
@@ -0,0 +1,29 @@
+%PDF-1.4
+1 0 obj
+<< /Type /Catalog /Pages 2 0 R >>
+endobj
+2 0 obj
+<< /Type /Pages /Kids [3 0 R 4 0 R 5 0 R] /Count 3 >>
+endobj
+3 0 obj
+<< /Type /Page /Parent 2 0 R /MediaBox [0 0 595.5 842] >>
+endobj
+4 0 obj
+<< /Type /Page /Parent 2 0 R /MediaBox [0 0 300 400] >>
+endobj
+5 0 obj
+<< /Type /Page /Parent 2 0 R /MediaBox [0 0 400 300] /Rotate 90 >>
+endobj
+xref
+0 6
+0000000000 65535 f
+0000000009 00000 n
+0000000058 00000 n
+0000000127 00000 n
+0000000200 00000 n
+0000000271 00000 n
+trailer
+<< /Size 6 /Root 1 0 R >>
+startxref
+353
+%%EOF
diff --git a/internal/entity/models/paddleocr.go b/internal/entity/models/paddleocr.go
index 97747421e2..c8ddf1cd0f 100644
--- a/internal/entity/models/paddleocr.go
+++ b/internal/entity/models/paddleocr.go
@@ -17,7 +17,6 @@
package models
import (
- "bufio"
"bytes"
"context"
"encoding/json"
@@ -96,32 +95,15 @@ type paddleSubmitResponse struct {
type paddlePollResponse struct {
Data struct {
- State string `json:"state"`
- ErrorMsg string `json:"errorMsg"`
- ResultUrl struct {
+ State string `json:"state"`
+ ResultJSONURL string `json:"resultJsonUrl"`
+ ErrorMsg string `json:"errorMsg"`
+ ResultUrl struct {
JsonUrl string `json:"jsonUrl"`
} `json:"resultUrl"`
} `json:"data"`
}
-type paddleJsonlLine struct {
- LogId string `json:"logId"`
- ErrorCode int `json:"errorCode"`
- ErrorMsg string `json:"errorMsg"`
- Result struct {
- LayoutParsingResults []struct {
- Markdown struct {
- Text string `json:"text"`
- } `json:"markdown"`
- } `json:"layoutParsingResults"`
- OcrResults []struct {
- PrunedResult struct {
- RecTexts []string `json:"rec_texts"`
- } `json:"prunedResult"`
- } `json:"ocrResults"`
- } `json:"result"`
-}
-
// maxErrorBodyBytes caps how much of a failed HTTP response body is read and
// logged so an oversized or erroneous payload cannot consume unbounded memory
// or produce unbounded log lines / error strings.
@@ -134,115 +116,6 @@ func readErrorBody(body io.Reader) string {
return string(data)
}
-// logBody truncates an already-buffered response payload to maxErrorBodyBytes
-// so it is safe for log fields and error strings.
-func logBody(body []byte) string {
- if len(body) > maxErrorBodyBytes {
- return string(body[:maxErrorBodyBytes]) + "…(truncated)"
- }
- return string(body)
-}
-
-// appendOCRText appends the markdown or OCR text fragments carried by one
-// result entry to fullMarkdown and reports whether any text was added.
-// Entries whose errorCode != 0 are treated as errored rather than silently
-// empty so a partial/errored page is not invisible to the caller.
-func (p *PaddleOCRModel) appendOCRText(fullMarkdown *strings.Builder, entry paddleJsonlLine) (added, errored bool) {
- if entry.ErrorCode != 0 {
- return false, true
- }
- before := fullMarkdown.Len()
- for _, layoutRes := range entry.Result.LayoutParsingResults {
- fullMarkdown.WriteString(layoutRes.Markdown.Text)
- fullMarkdown.WriteString("\n\n")
- }
-
- // Fallback to ocrResults for models like PP-OCRv6
- if len(entry.Result.LayoutParsingResults) == 0 {
- for _, ocrRes := range entry.Result.OcrResults {
- for _, text := range ocrRes.PrunedResult.RecTexts {
- text = strings.TrimSpace(text)
- if text != "" {
- fullMarkdown.WriteString(text)
- fullMarkdown.WriteString("\n")
- }
- }
- }
- }
- return fullMarkdown.Len() > before, false
-}
-
-// parseOCRResultBody extracts markdown/OCR text from the downloaded result
-// payload. The online service returns a JSON array of page objects (list of
-// dict), while older endpoints may return newline-delimited JSON objects;
-// both forms are accepted. It reports how the payload was interpreted
-// (arrayParsed) together with parse accounting counters so callers can log
-// why the result was empty: entries dropped by json.Unmarshal (skippedLines),
-// decoded entries carrying no text fragment (emptyResultLines), entries whose
-// errorCode != 0 (erroredLines), and entries that yielded text (contentLines).
-func (p *PaddleOCRModel) parseOCRResultBody(rawBody []byte, fullMarkdown *strings.Builder) (arrayParsed bool, scannedLines, skippedLines, emptyResultLines, contentLines, erroredLines int, err error) {
- var entries []paddleJsonlLine
- err = json.Unmarshal(rawBody, &entries)
- if err == nil {
- arrayParsed = true
- scannedLines = len(entries)
- for _, entry := range entries {
- added, errored := p.appendOCRText(fullMarkdown, entry)
- switch {
- case errored:
- erroredLines++
- p.logErroredEntry(entry)
- case added:
- contentLines++
- default:
- emptyResultLines++
- }
- }
- return
- }
-
- scanner := bufio.NewScanner(bytes.NewReader(rawBody))
- scanner.Buffer(make([]byte, 64*1024), 1024*1024)
- for scanner.Scan() {
- line := strings.TrimSpace(scanner.Text())
- if line == "" {
- continue
- }
- scannedLines++
-
- var lineData paddleJsonlLine
- if err := json.Unmarshal([]byte(line), &lineData); err != nil {
- skippedLines++
- continue
- }
- added, errored := p.appendOCRText(fullMarkdown, lineData)
- switch {
- case errored:
- erroredLines++
- p.logErroredEntry(lineData)
- case added:
- contentLines++
- default:
- emptyResultLines++
- }
- }
- if err := scanner.Err(); err != nil {
- return false, 0, 0, 0, 0, 0, fmt.Errorf("error reading jsonl: %w", err)
- }
- // The array-parse error stored in err is expected on this path; the jsonl
- // fallback succeeded, so clear it before the bare return.
- return false, scannedLines, skippedLines, emptyResultLines, contentLines, erroredLines, nil
-}
-
-// logErroredEntry makes an entry with a non-zero errorCode visible instead of
-// silently dropping the page as empty.
-func (p *PaddleOCRModel) logErroredEntry(entry paddleJsonlLine) {
- common.Warn("paddleocr result: entry errored",
- zap.String("log_id", entry.LogId),
- zap.Int("error_code", entry.ErrorCode),
- zap.String("error_msg", entry.ErrorMsg))
-}
-
func (p *PaddleOCRModel) OCRFile(ctx context.Context, modelName *string, content []byte, fileURL *string, apiConfig *APIConfig, ocrConfig *OCRConfig, modelUsage *common.ModelUsage) (*OCRFileResponse, error) {
if err := p.baseModel.APIConfigCheck(apiConfig); err != nil {
return nil, err
@@ -415,6 +288,9 @@ func (p *PaddleOCRModel) OCRFile(ctx context.Context, modelName *string, content
zap.Int("attempt", attempt),
zap.String("state", state))
jsonlUrl = pollData.Data.ResultUrl.JsonUrl
+ if jsonlUrl == "" {
+ jsonlUrl = pollData.Data.ResultJSONURL
+ }
break
} else if state == "failed" {
common.Error("paddleocr poll: failed",
@@ -488,36 +364,21 @@ func (p *PaddleOCRModel) OCRFile(ctx context.Context, modelName *string, content
return nil, fmt.Errorf("failed to read result body: %w", err)
}
- var fullMarkdown strings.Builder
- arrayParsed, scannedLines, skippedLines, emptyResultLines, contentLines, erroredLines, err := p.parseOCRResultBody(rawBody, &fullMarkdown)
+ response, err := parsePaddleOCRResultBody(rawBody)
if err != nil {
+ common.Error("paddleocr result: parse failed", err,
+ zap.String("driver", p.Name()), zap.String("job_id", jobId),
+ zap.Int("body_bytes", len(rawBody)))
return nil, err
}
-
- extractedText := strings.TrimSpace(fullMarkdown.String())
- if extractedText == "" {
- common.Warn("paddleocr result: parsed empty text",
- zap.String("job_id", jobId),
- zap.Bool("array_parsed", arrayParsed),
- zap.Int("body_bytes", len(rawBody)),
- zap.Int("scanned_lines", scannedLines),
- zap.Int("skipped_lines", skippedLines),
- zap.Int("errored_lines", erroredLines),
- zap.Int("empty_result_lines", emptyResultLines),
- zap.Int("content_lines", contentLines))
- return nil, fmt.Errorf("paddleocr result: parsed empty text (scanned_lines=%d, skipped_lines=%d, errored_lines=%d, empty_result_lines=%d, content_lines=%d)",
- scannedLines, skippedLines, erroredLines, emptyResultLines, contentLines)
+ if len(response.Warnings) > 0 {
+ common.Warn("paddleocr result: partial failure", zap.String("job_id", jobId), zap.Strings("warnings", response.Warnings))
}
+ extractedText := *response.Text
common.Info("paddleocr result: parsed",
- zap.String("job_id", jobId),
- zap.Bool("array_parsed", arrayParsed),
- zap.Int("body_bytes", len(rawBody)),
- zap.Int("text_len", len(extractedText)),
- zap.Int("scanned_lines", scannedLines),
- zap.Int("skipped_lines", skippedLines),
- zap.Int("errored_lines", erroredLines),
- zap.Int("empty_result_lines", emptyResultLines),
- zap.Int("content_lines", contentLines))
+ zap.String("job_id", jobId), zap.Int("body_bytes", len(rawBody)),
+ zap.Int("text_len", len(extractedText)), zap.Int("page_count", len(response.Pages)))
+
// OCR text can contain sensitive document content; keep any preview out of
// default (Info) logs and only surface it when debug logging is enabled.
preview := extractedText
@@ -528,7 +389,7 @@ func (p *PaddleOCRModel) OCRFile(ctx context.Context, modelName *string, content
zap.String("job_id", jobId),
zap.String("text_preview", preview))
- return &OCRFileResponse{Text: &extractedText}, nil
+ return response, nil
}
// paddleOCRUploadFilename preserves the content-derived format expected by the
diff --git a/internal/entity/models/paddleocr_local.go b/internal/entity/models/paddleocr_local.go
index 477c2cae18..e460cce8aa 100644
--- a/internal/entity/models/paddleocr_local.go
+++ b/internal/entity/models/paddleocr_local.go
@@ -85,22 +85,6 @@ func (p *PaddleOCRLocalModel) AudioSpeechWithSender(ctx context.Context, modelNa
return fmt.Errorf("%s no such method", p.Name())
}
-// For different model, paddleOCR have different url_suffix:
-// e.g.PaddleOCR-VL: /layout-parsing | PP-OCRv5: /ocr
-// We select `PaddleOCR-VL` here
-type paddleLocalOCRResponse struct {
- LogId string `json:"logId"`
- ErrorCode int `json:"errorCode"`
- ErrorMsg string `json:"errorMsg"`
- Result struct {
- LayoutParsingResults []struct {
- Markdown struct {
- Text string `json:"text"`
- } `json:"markdown"`
- } `json:"layoutParsingResults"`
- } `json:"result"`
-}
-
func (p *PaddleOCRLocalModel) OCRFile(ctx context.Context, modelName *string, content []byte, fileURL *string, apiConfig *APIConfig, ocrConfig *OCRConfig, modelUsage *common.ModelUsage) (*OCRFileResponse, error) {
if len(content) == 0 {
return nil, fmt.Errorf("local PaddleOCR requires file content, but content is empty")
@@ -126,9 +110,11 @@ func (p *PaddleOCRLocalModel) OCRFile(ctx context.Context, modelName *string, co
}
reqData := map[string]interface{}{
- "file": base64Str,
- "fileType": fileType,
- "algorithm": algorithm,
+ "file": base64Str,
+ "fileType": fileType,
+ "algorithm": algorithm,
+ "useDocOrientationClassify": false,
+ "useDocUnwarping": false,
}
jsonData, err := json.Marshal(reqData)
@@ -182,45 +168,18 @@ func (p *PaddleOCRLocalModel) OCRFile(ctx context.Context, modelName *string, co
return nil, fmt.Errorf("failed to read response body: %w", err)
}
- var ocrResp paddleLocalOCRResponse
- if err := json.Unmarshal(respBody, &ocrResp); err != nil {
- common.Error("paddleocr local submit: parse failed",
- err,
- zap.String("driver", p.Name()),
- zap.String("url", url),
- zap.String("body", logBody(respBody)))
- return nil, fmt.Errorf("failed to parse local PaddleOCR response: %w, raw: %s", err, logBody(respBody))
- }
-
- if ocrResp.ErrorCode != 0 {
- common.Error("paddleocr local submit: task failed",
- fmt.Errorf("errorCode %d", ocrResp.ErrorCode),
- zap.String("driver", p.Name()),
- zap.String("url", url),
- zap.Int("error_code", ocrResp.ErrorCode),
- zap.String("error_msg", ocrResp.ErrorMsg))
- return nil, fmt.Errorf("local PaddleOCR task failed: %s (errorCode: %d)", ocrResp.ErrorMsg, ocrResp.ErrorCode)
+ response, err := parsePaddleOCRResultBody(respBody)
+ if err != nil {
+ common.Error("paddleocr local result: parse failed", err,
+ zap.String("driver", p.Name()), zap.String("url", url),
+ zap.Int("body_bytes", len(respBody)))
+ return nil, fmt.Errorf("local PaddleOCR result: %w", err)
}
-
- common.Info("paddleocr local submit: ok",
- zap.String("driver", p.Name()),
- zap.String("url", url),
- zap.Int("status", resp.StatusCode),
- zap.String("log_id", ocrResp.LogId))
-
- var fullMarkdown strings.Builder
- for _, layoutRes := range ocrResp.Result.LayoutParsingResults {
- if layoutRes.Markdown.Text != "" {
- fullMarkdown.WriteString(layoutRes.Markdown.Text)
- fullMarkdown.WriteString("\n\n")
- }
+ if len(response.Warnings) > 0 {
+ common.Warn("paddleocr local result: partial failure", zap.String("driver", p.Name()), zap.Strings("warnings", response.Warnings))
}
-
- extractedText := strings.TrimSpace(fullMarkdown.String())
-
- return &OCRFileResponse{
- Text: &extractedText,
- }, nil
+ common.Info("paddleocr local submit: ok", zap.String("driver", p.Name()), zap.String("url", url), zap.Int("status", resp.StatusCode))
+ return response, nil
}
func (p *PaddleOCRLocalModel) ParseFile(ctx context.Context, modelName *string, content []byte, url *string, apiConfig *APIConfig, parseFileConfig *ParseFileConfig, modelUsage *common.ModelUsage) (*ParseFileResponse, error) {
diff --git a/internal/entity/models/paddleocr_response.go b/internal/entity/models/paddleocr_response.go
new file mode 100644
index 0000000000..fe681cdd2d
--- /dev/null
+++ b/internal/entity/models/paddleocr_response.go
@@ -0,0 +1,263 @@
+package models
+
+import (
+ "bytes"
+ "encoding/json"
+ "fmt"
+ "io"
+ "math"
+ "strings"
+
+ "ragflow/internal/utility"
+)
+
+type paddleOCREntry struct {
+ LogID string `json:"logId"`
+ ErrorCode int `json:"errorCode"`
+ ErrorMsg string `json:"errorMsg"`
+ Result paddleOCRResult `json:"result"`
+}
+
+type paddleOCRPrunedResult struct {
+ PageIndex *int `json:"page_index"`
+ Width float64 `json:"width"`
+ Height float64 `json:"height"`
+ Blocks []struct {
+ Text string `json:"block_content"`
+ Label string `json:"block_label"`
+ BBox json.RawMessage `json:"block_bbox"`
+ } `json:"parsing_res_list"`
+ RecTexts []string `json:"rec_texts"`
+ RecBoxes []json.RawMessage `json:"rec_boxes"`
+}
+
+type paddleOCRResult struct {
+ LayoutParsingResults []struct {
+ Markdown struct {
+ Text string `json:"text"`
+ } `json:"markdown"`
+ PrunedResult paddleOCRPrunedResult `json:"prunedResult"`
+ } `json:"layoutParsingResults"`
+ OcrResults []struct {
+ PrunedResult paddleOCRPrunedResult `json:"prunedResult"`
+ } `json:"ocrResults"`
+ DataInfo struct {
+ Pages []struct {
+ Width float64 `json:"width"`
+ Height float64 `json:"height"`
+ } `json:"pages"`
+ } `json:"dataInfo"`
+}
+
+// parsePaddleOCRResultBody accepts JSON arrays, objects and JSONL. Rejecting
+// malformed JSON prevents inferred page numbers from shifting silently. Failed
+// entries are omitted only while subsequent source page identity stays known.
+func parsePaddleOCRResultBody(body []byte) (*OCRFileResponse, error) {
+ decoder := json.NewDecoder(bytes.NewReader(body))
+ var entries []paddleOCREntry
+ for {
+ var raw json.RawMessage
+ if err := decoder.Decode(&raw); err == io.EOF {
+ break
+ } else if err != nil {
+ return nil, fmt.Errorf("paddleocr result: invalid JSON after byte %d: %w", decoder.InputOffset(), err)
+ }
+ if len(raw) > 0 && raw[0] == '[' {
+ var group []paddleOCREntry
+ if err := json.Unmarshal(raw, &group); err != nil {
+ return nil, fmt.Errorf("paddleocr result: invalid entries: %w", err)
+ }
+ entries = append(entries, group...)
+ } else {
+ var entry paddleOCREntry
+ if err := json.Unmarshal(raw, &entry); err != nil {
+ return nil, fmt.Errorf("paddleocr result: invalid entry: %w", err)
+ }
+ entries = append(entries, entry)
+ }
+ }
+ response := &OCRFileResponse{}
+ var text strings.Builder
+ nextPage := 0
+ pageIdentityKnown := true
+ for i, entry := range entries {
+ pages := paddleOCRPages(entry.Result, nextPage)
+ var firstIndex *int
+ if len(entry.Result.LayoutParsingResults) > 0 {
+ firstIndex = entry.Result.LayoutParsingResults[0].PrunedResult.PageIndex
+ } else if len(entry.Result.OcrResults) > 0 {
+ firstIndex = entry.Result.OcrResults[0].PrunedResult.PageIndex
+ }
+ if firstIndex != nil && *firstIndex >= 0 {
+ pageIdentityKnown = true
+ }
+ if pageIdentityKnown {
+ for _, page := range pages {
+ if page.Index < nextPage {
+ return nil, fmt.Errorf("paddleocr result: entry %d (log_id=%q): source page index %d repeats or precedes an earlier page (next index %d)", i+1, entry.LogID, page.Index, nextPage)
+ }
+ nextPage = page.Index + 1
+ }
+ }
+ if entry.ErrorCode != 0 || len(pages) == 0 {
+ warning := fmt.Sprintf("paddleocr result: entry %d (log_id=%q)", i+1, entry.LogID)
+ if len(pages) > 0 && pageIdentityKnown {
+ warning += fmt.Sprintf(" (source pages %d..%d)", pages[0].Index+1, nextPage)
+ }
+ if entry.ErrorCode != 0 {
+ warning += fmt.Sprintf(": errorCode %d: %s", entry.ErrorCode, entry.ErrorMsg)
+ } else {
+ warning += ": contains no pages"
+ }
+ response.Warnings = append(response.Warnings, warning)
+ if len(pages) == 0 {
+ pageIdentityKnown = false
+ }
+ continue
+ }
+ if !pageIdentityKnown {
+ return nil, fmt.Errorf("paddleocr result: entry %d (log_id=%q): source page identity unknown after failed entry", i+1, entry.LogID)
+ }
+ response.Pages = append(response.Pages, pages...)
+ text.WriteString(paddleOCRText(entry.Result))
+ text.WriteString("\n\n")
+ }
+ content := strings.TrimSpace(text.String())
+ if content == "" {
+ if len(response.Warnings) > 0 {
+ return nil, fmt.Errorf("paddleocr result: no usable text; %s", response.Warnings[0])
+ }
+ return nil, fmt.Errorf("paddleocr result: parsed empty text")
+ }
+ response.Text = &content
+ return response, nil
+}
+
+func paddleOCRPages(result paddleOCRResult, nextPage int) []OCRPage {
+ var pages []OCRPage
+ appendPage := func(pruned paddleOCRPrunedResult, markdown string, ordinal int) {
+ page := OCRPage{Index: nextPage, Width: pruned.Width, Height: pruned.Height}
+ if pruned.PageIndex != nil && *pruned.PageIndex >= 0 {
+ page.Index = *pruned.PageIndex
+ }
+ // dataInfo pages are ordinal within this result. An explicit source
+ // index differing from that ordinal makes this fallback ambiguous.
+ if ordinal < len(result.DataInfo.Pages) && (pruned.PageIndex == nil || *pruned.PageIndex == ordinal) {
+ size := result.DataInfo.Pages[ordinal]
+ if page.Width <= 0 {
+ page.Width = size.Width
+ }
+ if page.Height <= 0 {
+ page.Height = size.Height
+ }
+ }
+ page.Blocks = pruned.blocks()
+ hasText := false
+ for _, block := range page.Blocks {
+ if strings.TrimSpace(utility.StripMarkdownImages(block.Text)) != "" {
+ hasText = true
+ break
+ }
+ }
+ if !hasText && strings.TrimSpace(markdown) != "" {
+ page.Blocks = []OCRBlock{{Text: markdown}}
+ }
+ pages = append(pages, page)
+ nextPage = page.Index + 1
+ }
+ for i, layout := range result.LayoutParsingResults {
+ appendPage(layout.PrunedResult, layout.Markdown.Text, i)
+ }
+ if len(result.LayoutParsingResults) == 0 {
+ for i, ocr := range result.OcrResults {
+ appendPage(ocr.PrunedResult, "", i)
+ }
+ }
+ return pages
+}
+
+func (pruned paddleOCRPrunedResult) blocks() []OCRBlock {
+ var blocks []OCRBlock
+ for _, raw := range pruned.Blocks {
+ // Match PaddleX PaddleOCR-VL's default markdown_ignore_labels for
+ // structured output: paddlex/configs/pipelines/PaddleOCR-VL.yaml (release/3.7).
+ switch strings.ToLower(strings.TrimSpace(raw.Label)) {
+ case "header", "header_image", "footer", "footer_image", "number", "aside_text", "footnote":
+ continue
+ }
+ blocks = append(blocks, OCRBlock{Text: raw.Text, Label: raw.Label, BBox: paddleOCRBox(raw.BBox)})
+ }
+ if len(pruned.Blocks) == 0 {
+ for i, text := range pruned.RecTexts {
+ var box []float64
+ if i < len(pruned.RecBoxes) {
+ box = paddleOCRBox(pruned.RecBoxes[i])
+ }
+ blocks = append(blocks, OCRBlock{Text: text, Label: "text", BBox: box})
+ }
+ }
+ return blocks
+}
+
+func paddleOCRBox(raw json.RawMessage) []float64 {
+ var coordinates []*float64
+ if err := json.Unmarshal(raw, &coordinates); err != nil {
+ return nil
+ }
+ if len(coordinates) != 4 && len(coordinates) != 8 {
+ return nil
+ }
+ box := make([]float64, len(coordinates))
+ for i, value := range coordinates {
+ if value == nil || math.IsNaN(*value) || math.IsInf(*value, 0) {
+ return nil
+ }
+ box[i] = *value
+ }
+ if len(box) == 8 {
+ left, top, right, bottom := box[0], box[1], box[0], box[1]
+ for i := 2; i < len(box); i += 2 {
+ left = math.Min(left, box[i])
+ right = math.Max(right, box[i])
+ top = math.Min(top, box[i+1])
+ bottom = math.Max(bottom, box[i+1])
+ }
+ box = []float64{left, top, right, bottom}
+ }
+ if box[2] <= box[0] || box[3] <= box[1] {
+ return nil
+ }
+ return box
+}
+
+// paddleOCRText projects the service text for image OCR consumers. PDF parsing
+// uses the independently preserved blocks and page geometry.
+func paddleOCRText(result paddleOCRResult) string {
+ var text strings.Builder
+ appendTexts := func(pruned paddleOCRPrunedResult) {
+ for _, block := range pruned.Blocks {
+ text.WriteString(block.Text)
+ text.WriteString("\n")
+ }
+ if len(pruned.Blocks) == 0 {
+ for _, line := range pruned.RecTexts {
+ text.WriteString(line)
+ text.WriteString("\n")
+ }
+ }
+ }
+ for _, layout := range result.LayoutParsingResults {
+ if strings.TrimSpace(layout.Markdown.Text) != "" {
+ text.WriteString(layout.Markdown.Text)
+ } else {
+ appendTexts(layout.PrunedResult)
+ }
+ text.WriteString("\n\n")
+ }
+ if len(result.LayoutParsingResults) == 0 {
+ for _, ocr := range result.OcrResults {
+ appendTexts(ocr.PrunedResult)
+ }
+ }
+ return strings.TrimSpace(text.String())
+}
diff --git a/internal/entity/models/paddleocr_test.go b/internal/entity/models/paddleocr_test.go
index c81a8d7f99..5dba43c51e 100644
--- a/internal/entity/models/paddleocr_test.go
+++ b/internal/entity/models/paddleocr_test.go
@@ -6,6 +6,7 @@ import (
"fmt"
"net/http"
"net/http/httptest"
+ "os"
"strings"
"testing"
)
@@ -363,152 +364,311 @@ func TestPaddleOCRLocalModelOCRFileResolvesPayload(t *testing.T) {
}
}
-func TestParseOCRResultBodyArray(t *testing.T) {
- p := &PaddleOCRModel{}
- var md strings.Builder
- arrayParsed, scanned, skipped, emptyRes, content, errored, err := p.parseOCRResultBody([]byte(sampleArrayResult), &md)
- if err != nil {
- t.Fatalf("unexpected error: %v", err)
+func TestParsePaddleOCRResultBody(t *testing.T) {
+ for _, tc := range []struct {
+ name, body string
+ pages int
+ want string
+ }{
+ {"array", sampleArrayResult, 2, "天下皆知美之为美"},
+ {"JSONL", `{"result":{"layoutParsingResults":[{"markdown":{"text":"first"}}]}}
+{"result":{"layoutParsingResults":[{"markdown":{"text":"second"}}]}}`, 2, "first\n\nsecond"},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ response, err := parsePaddleOCRResultBody([]byte(tc.body))
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(response.Pages) != tc.pages || response.Text == nil || !strings.Contains(*response.Text, tc.want) {
+ t.Fatalf("response=%+v", response)
+ }
+ })
}
- if !arrayParsed {
- t.Fatalf("expected array parse, got jsonl fallback")
+}
+
+func TestParsePaddleOCRResultBodyRejectsErrors(t *testing.T) {
+ for _, body := range []string{`[]`, `not json`, `{"result":{}}`, `[{"errorCode":1001,"errorMsg":"page failed"}]`, `{"errorCode":1001,"errorMsg":"page failed"}`} {
+ if _, err := parsePaddleOCRResultBody([]byte(body)); err == nil {
+ t.Fatalf("accepted failed result: %s", body)
+ }
}
- if scanned != 2 {
- t.Errorf("scannedLines = %d, want 2", scanned)
+}
+
+// Missing structured pages must fail even when the OCR text is readable.
+func TestPaddleOCRDriversPreserveLayout(t *testing.T) {
+ withSSRFBypass(t)
+ body, err := os.ReadFile("testdata/paddleocr_layout.json")
+ if err != nil {
+ t.Fatal(err)
+ }
+ for _, local := range []bool{false, true} {
+ t.Run(fmt.Sprintf("local=%v", local), func(t *testing.T) {
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "application/json")
+ switch {
+ case r.URL.Path == "/layout-parsing":
+ var request map[string]any
+ if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
+ t.Error(err)
+ }
+ for _, key := range []string{"useDocOrientationClassify", "useDocUnwarping"} {
+ if request[key] != false {
+ t.Errorf("%s=%v; want preprocessing disabled", key, request[key])
+ }
+ }
+ w.Write(body)
+ case r.URL.Path == "/result":
+ var obj any
+ json.Unmarshal(body, &obj)
+ json.NewEncoder(w).Encode(obj)
+ case r.Method == http.MethodPost:
+ fmt.Fprint(w, `{"data":{"jobId":"layout"}}`)
+ default:
+ fmt.Fprintf(w, `{"data":{"state":"done","resultUrl":{"jsonUrl":%q}}}`, "http://"+r.Host+"/result")
+ }
+ }))
+ defer server.Close()
+ var driver ModelDriver = NewPaddleOCRModel(nil, URLSuffix{OCR: "api/v2/ocr/jobs"})
+ if local {
+ driver = NewPaddleOCRLocalModel(nil, URLSuffix{OCR: "layout-parsing"})
+ }
+ model, token := "PaddleOCR-VL-1.6", "test-token"
+ response, err := driver.OCRFile(t.Context(), &model, []byte("%PDF-1.4"), nil, &APIConfig{BaseURL: &server.URL, ApiKey: &token}, nil, nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(response.Pages) != 1 {
+ t.Fatalf("pages=%+v", response.Pages)
+ }
+ page := response.Pages[0]
+ if page.Width != 1191 || page.Height != 1684 || len(page.Blocks) != 7 {
+ t.Fatalf("page=%+v", page)
+ }
+ if page.Blocks[0].Text != "Paragraph 1." || fmt.Sprint(page.Blocks[0].BBox) != "[174 146 1015 240]" {
+ t.Fatalf("block=%+v", page.Blocks[0])
+ }
+ })
}
- if skipped != 0 {
- t.Errorf("skippedLines = %d, want 0", skipped)
+}
+
+func TestPaddleOCRResultPageIdentity(t *testing.T) {
+ cases := []struct {
+ name, body string
+ pages int
+ index int
+ text string
+ box string
+ }{
+ {"block only", `{"result":{"layoutParsingResults":[{"prunedResult":{"width":1200,"height":1600,"parsing_res_list":[{"block_content":"body","block_bbox":[20,40,100,120]}]}}]}}`, 1, 0, "body", "[20 40 100 120]"},
+ {"blank middle page", `{"result":{"ocrResults":[{"prunedResult":{"width":1200,"height":1600,"rec_texts":["first"],"rec_boxes":[[20,40,100,120]]}},{"prunedResult":{}},{"prunedResult":{"width":900,"height":1200,"rec_texts":["third","unlocated"],"rec_boxes":[[30,60,90,120]]}}]}}`, 3, 2, "third", "[30 60 90 120]"},
+ {"explicit source index", `{"result":{"layoutParsingResults":[{"prunedResult":{"page_index":4,"parsing_res_list":[{"block_content":"fifth","block_bbox":[20,40,100,120]}]}}]}}`, 1, 4, "fifth", "[20 40 100 120]"},
+ {"layout polygon becomes rectangle", `{"result":{"layoutParsingResults":[{"prunedResult":{"parsing_res_list":[{"block_content":"polygon","block_bbox":[300,160,30,140,50,40,280,60]}]}}]}}`, 1, 0, "polygon", "[30 40 300 160]"},
+ {"recognition polygon becomes rectangle", `{"result":{"ocrResults":[{"prunedResult":{"rec_texts":["polygon"],"rec_boxes":[[300,160,30,140,50,40,280,60]]}}]}}`, 1, 0, "polygon", "[30 40 300 160]"},
+ {"unsupported box keeps text", `{"result":{"ocrResults":[{"prunedResult":{"rec_texts":["retained"],"rec_boxes":[[1,2,3,4,5,6]]}}]}}`, 1, 0, "retained", "[]"},
+ {"degenerate box keeps text", `{"result":{"ocrResults":[{"prunedResult":{"rec_texts":["retained"],"rec_boxes":[[10,20,10,40]]}}]}}`, 1, 0, "retained", "[]"},
+ {"null rectangle keeps text", `{"result":{"ocrResults":[{"prunedResult":{"rec_texts":["retained"],"rec_boxes":[[10,null,30,40]]}}]}}`, 1, 0, "retained", "[]"},
+ {"null polygon keeps text", `{"result":{"ocrResults":[{"prunedResult":{"rec_texts":["retained"],"rec_boxes":[[30,40,null,160,100,160,100,40]]}}]}}`, 1, 0, "retained", "[]"},
+ {"zero coordinate is valid", `{"result":{"ocrResults":[{"prunedResult":{"rec_texts":["retained"],"rec_boxes":[[0,0,30,40]]}}]}}`, 1, 0, "retained", "[0 0 30 40]"},
+ {"invalid box keeps text", `{"result":{"layoutParsingResults":[{"prunedResult":{"parsing_res_list":[{"block_content":"retained","block_bbox":[10,"bad",30,40]}]}}]}}`, 1, 0, "retained", "[]"},
+ {"pretty object", "{\n\"result\": {\n\"ocrResults\": [{\"prunedResult\": {\"rec_texts\": [\"pretty\"]}}]\n}}", 1, 0, "pretty", "[]"},
}
- if emptyRes != 0 {
- t.Errorf("emptyResultLines = %d, want 0", emptyRes)
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ response, err := parsePaddleOCRResultBody([]byte(tc.body))
+ if err != nil {
+ t.Fatal(err)
+ }
+ pages := response.Pages
+ if len(pages) != tc.pages {
+ t.Fatalf("pages=%+v", pages)
+ }
+ page := pages[len(pages)-1]
+ if page.Index != tc.index || len(page.Blocks) == 0 || page.Blocks[0].Text != tc.text || fmt.Sprint(page.Blocks[0].BBox) != tc.box {
+ t.Fatalf("page=%+v", page)
+ }
+ })
}
- if content != 2 {
- t.Errorf("contentLines = %d, want 2", content)
+}
+
+func TestPaddleOCRPageMarkdownFallbackWhenBlocksHaveNoText(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ blocks string
+ wantText string
+ wantBox string
+ }{
+ {"no blocks", `[]`, "page fallback", "[]"},
+ {"only header", `[{"block_label":"header","block_content":"ignored header","block_bbox":[10,20,30,40]}]`, "page fallback", "[]"},
+ {"only page number", `[{"block_label":"number","block_content":"12"}]`, "page fallback", "[]"},
+ {"empty blocks", `[{"block_content":" "}]`, "page fallback", "[]"},
+ {"markdown image", `[{"block_content":"![figure](https://example.invalid/a.png)","block_bbox":[10,20,30,40]}]`, "page fallback", "[]"},
+ {"parenthesized image destination", `[{"block_content":"![figure](https://example.invalid/a(1).png)","block_bbox":[10,20,30,40]}]`, "page fallback", "[]"},
+ {"html image", `[{"block_content":"<div><img src=\"a.png\"></div>"}]`, "page fallback", "[]"},
+ {"text among images", `[{"block_content":"![figure](a.png)"},{"block_content":"positioned text","block_bbox":[10,20,30,40]}]`, "![figure](a.png)\npositioned text", "[10 20 30 40]"},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ body := `{"result":{"layoutParsingResults":[{"markdown":{"text":"page fallback"},"prunedResult":{"page_index":2,"parsing_res_list":` + tc.blocks + `}}]}}`
+ response, err := parsePaddleOCRResultBody([]byte(body))
+ if err != nil {
+ t.Fatal(err)
+ }
+ page := response.Pages[0]
+ var texts []string
+ var box []float64
+ for _, block := range page.Blocks {
+ if strings.TrimSpace(block.Text) != "" {
+ texts = append(texts, block.Text)
+ box = block.BBox
+ }
+ }
+ if page.Index != 2 || strings.Join(texts, "\n") != tc.wantText || fmt.Sprint(box) != tc.wantBox {
+ t.Fatalf("page=%+v", page)
+ }
+ if response.Text == nil || *response.Text != "page fallback" {
+ t.Fatalf("API text changed: %+v", response.Text)
+ }
+ })
}
- if errored != 0 {
- t.Errorf("erroredLines = %d, want 0", errored)
+}
+
+func TestPaddleOCRPagesExcludeMarkdownIgnoredLabels(t *testing.T) {
+ for _, label := range []string{"header", "header_image", "footer", "footer_image", "number", "aside_text", "footnote", " HEADER "} {
+ t.Run(label, func(t *testing.T) {
+ body := `{"result":{"layoutParsingResults":[{"markdown":{"text":"body"},"prunedResult":{"parsing_res_list":[{"block_label":"` + label + `","block_content":"ignored furniture"},{"block_label":"text","block_content":"body","block_bbox":[10,20,30,40]}]}}]}}`
+ response, err := parsePaddleOCRResultBody([]byte(body))
+ if err != nil {
+ t.Fatal(err)
+ }
+ blocks := response.Pages[0].Blocks
+ if len(blocks) != 1 || blocks[0].Text != "body" || fmt.Sprint(blocks[0].BBox) != "[10 20 30 40]" {
+ t.Fatalf("blocks=%+v", blocks)
+ }
+ })
}
- out := md.String()
- if !strings.Contains(out, "道可道,非常道") {
- t.Errorf("markdown missing first page text: %q", out)
+}
+
+func TestPaddleOCRRejectsMalformedJSONL(t *testing.T) {
+ body := `{"result":{"ocrResults":[{"prunedResult":{"rec_texts":["first"]}}]}}
+{"result": corrupted
+{"result":{"ocrResults":[{"prunedResult":{"rec_texts":["third"]}}]}}`
+ if _, err := parsePaddleOCRResultBody([]byte(body)); err == nil {
+ t.Fatal("accepted malformed JSONL with ambiguous page identity")
}
- if !strings.Contains(out, "天下皆知美之为美") {
- t.Errorf("markdown missing second page text: %q", out)
+}
+
+func TestPaddleOCRResultErrorIdentifiesEntry(t *testing.T) {
+ body := `{"logId":"failed-page-log","errorCode":1001,"errorMsg":"page failed"}`
+ _, err := parsePaddleOCRResultBody([]byte(body))
+ if err == nil {
+ t.Fatal("accepted failed page")
+ }
+ for _, part := range []string{"entry 1", "failed-page-log", "1001", "page failed"} {
+ if !strings.Contains(err.Error(), part) {
+ t.Fatalf("missing %q in error: %v", part, err)
+ }
}
}
-func TestParseOCRResultBodyJSONL(t *testing.T) {
- // Older payloads are one JSON object per line; the fallback path must
- // handle them too.
- jsonl := `{"logId":"a","result":{"layoutParsingResults":[{"markdown":{"text":"line one"}}]},"errorCode":0}
-{"logId":"b","result":{"layoutParsingResults":[{"markdown":{"text":"line two"}}]},"errorCode":0}`
- p := &PaddleOCRModel{}
- var md strings.Builder
- arrayParsed, scanned, skipped, emptyRes, content, errored, err := p.parseOCRResultBody([]byte(jsonl), &md)
+func TestOCRFileResponseDoesNotExposePageBlocks(t *testing.T) {
+ text := "recognized text"
+ response := OCRFileResponse{Text: &text, Warnings: []string{"internal warning"}, Pages: []OCRPage{{Index: 0, Blocks: []OCRBlock{{Text: text, BBox: []float64{1, 2, 3, 4}}}}}}
+ body, err := json.Marshal(response)
if err != nil {
- t.Fatalf("unexpected error: %v", err)
- }
- if arrayParsed {
- t.Fatalf("expected jsonl fallback, got array parse")
- }
- if scanned != 2 {
- t.Errorf("scannedLines = %d, want 2", scanned)
- }
- if skipped != 0 {
- t.Errorf("skippedLines = %d, want 0", skipped)
+ t.Fatal(err)
}
- if emptyRes != 0 {
- t.Errorf("emptyResultLines = %d, want 0", emptyRes)
- }
- if content != 2 {
- t.Errorf("contentLines = %d, want 2", content)
- }
- if errored != 0 {
- t.Errorf("erroredLines = %d, want 0", errored)
- }
- out := md.String()
- if !strings.Contains(out, "line one") || !strings.Contains(out, "line two") {
- t.Errorf("markdown missing jsonl text: %q", out)
+ if string(body) != `{"text":"recognized text"}` {
+ t.Fatalf("public OCR response exposes internal data: %s", body)
}
}
-func TestParseOCRResultBodyEmpty(t *testing.T) {
- tests := []struct {
- name string
- body string
- wantArr bool
- wantEmpty int
+func TestPaddleOCRPartialFailuresPreservePageIdentity(t *testing.T) {
+ first := `{"result":{"layoutParsingResults":[{"markdown":{"text":"first"}}]}}`
+ failed := `{"logId":"bad-page","errorCode":1001,"errorMsg":"page failed"}`
+ third := `{"result":{"layoutParsingResults":[{"prunedResult":{"page_index":2},"markdown":{"text":"third"}}]}}`
+ for _, tc := range []struct {
+ name, body string
+ indexes []int
+ wantErr bool
}{
- {"empty array", `[]`, true, 0},
- {"entries without text", `[{"logId":"a","result":{"layoutParsingResults":[]},"errorCode":0}]`, true, 1},
- {"garbage line", `not json`, false, 0},
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- p := &PaddleOCRModel{}
- var md strings.Builder
- arrayParsed, _, _, emptyRes, content, errored, err := p.parseOCRResultBody([]byte(tt.body), &md)
- if err != nil {
- t.Fatalf("unexpected error: %v", err)
- }
- if errored != 0 {
- t.Errorf("erroredLines = %d, want 0", errored)
+ {"explicit recovery", first + "\n" + failed + "\n" + third, []int{0, 2}, false},
+ {"missing result recovery", first + "\n" + `{"result":{}}` + "\n" + third, []int{0, 2}, false},
+ {"trailing failure", first + "\n" + failed, []int{0}, false},
+ {"unknown page identity", first + "\n" + failed + "\n" + first, nil, true},
+ {"known failed range", first + "\n" + `{"errorCode":1001,"result":{"layoutParsingResults":[{"prunedResult":{"page_index":1}},{"prunedResult":{"page_index":2}}]}}` + "\n" + first, []int{0, 3}, false},
+ {"all failed", failed, nil, true},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ result, err := parsePaddleOCRResultBody([]byte(tc.body))
+ if tc.wantErr {
+ if err == nil {
+ t.Fatal("accepted result without trustworthy page identity or content")
+ }
+ return
}
- if arrayParsed != tt.wantArr {
- t.Errorf("arrayParsed = %v, want %v", arrayParsed, tt.wantArr)
+ if err != nil {
+ t.Fatal(err)
}
- if content != 0 {
- t.Errorf("contentLines = %d, want 0", content)
+ if len(result.Warnings) != 1 {
+ t.Fatalf("partial failure warnings=%v", result.Warnings)
}
- // Structurally valid entries that decode but carry no text are
- // counted as empty; unparseable content must never crash.
- if emptyRes != tt.wantEmpty {
- t.Errorf("emptyResultLines = %d, want %d", emptyRes, tt.wantEmpty)
+ if len(result.Pages) != len(tc.indexes) {
+ t.Fatalf("pages=%v", result.Pages)
}
- if strings.TrimSpace(md.String()) != "" {
- t.Errorf("expected empty markdown, got %q", md.String())
+ for i, index := range tc.indexes {
+ if result.Pages[i].Index != index {
+ t.Fatalf("page %d index=%d; want %d", i, result.Pages[i].Index, index)
+ }
}
})
}
}
-// TestParseOCRResultBodyErrored verifies that entries with a non-zero
-// errorCode are counted as errored (not silently counted as empty), produce no
-// text, and are handled identically on the array and jsonl paths.
-func TestParseOCRResultBodyErrored(t *testing.T) {
- tests := []struct {
- name string
- body string
+func TestPaddleOCRPageSizeFallbackRequiresAlignedIdentity(t *testing.T) {
+ for _, tc := range []struct {
+ name, fields string
+ width, height float64
}{
- {"array path", `[{"logId":"l1","result":{"layoutParsingResults":[{"markdown":{"text":"should not surface"}}]},"errorCode":1001,"errorMsg":"page failed"}]`},
- {"jsonl path", `{"logId":"l1","result":{"layoutParsingResults":[{"markdown":{"text":"should not surface"}}]},"errorCode":1001,"errorMsg":"page failed"}`},
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- p := &PaddleOCRModel{}
- var md strings.Builder
- _, scanned, skipped, emptyRes, content, errored, err := p.parseOCRResultBody([]byte(tt.body), &md)
+ {"ordinal", ``, 1200, 1600},
+ {"aligned index", `"page_index":0,`, 1200, 1600},
+ {"ambiguous index", `"page_index":1,`, 0, 0},
+ {"source dimensions", `"page_index":1,"width":900,"height":1100,`, 900, 1100},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ body := fmt.Sprintf(`{"result":{"layoutParsingResults":[{"prunedResult":{%s"parsing_res_list":[{"block_content":"body","block_bbox":[10,20,100,200]}]}}],"dataInfo":{"pages":[{"width":1200,"height":1600},{"width":900,"height":1100}]}}}`, tc.fields)
+ result, err := parsePaddleOCRResultBody([]byte(body))
if err != nil {
- t.Fatalf("unexpected error: %v", err)
+ t.Fatal(err)
}
- if scanned != 1 {
- t.Errorf("scannedLines = %d, want 1", scanned)
+ page := result.Pages[0]
+ if page.Width != tc.width || page.Height != tc.height {
+ t.Fatalf("dimensions=(%v,%v); want (%v,%v)", page.Width, page.Height, tc.width, tc.height)
}
- if skipped != 0 {
- t.Errorf("skippedLines = %d, want 0", skipped)
+ if len(page.Blocks) != 1 || page.Blocks[0].Text != "body" {
+ t.Fatalf("text lost: %v", page)
}
- if emptyRes != 0 {
- t.Errorf("emptyResultLines = %d, want 0 (errored entry must not masquerade as empty)", emptyRes)
- }
- if content != 0 {
- t.Errorf("contentLines = %d, want 0", content)
- }
- if errored != 1 {
- t.Errorf("erroredLines = %d, want 1", errored)
- }
- if strings.TrimSpace(md.String()) != "" {
- t.Errorf("errored entry must not contribute text, got %q", md.String())
+ })
+ }
+}
+
+func TestPaddleOCRRejectsRepeatedOrDecreasingSourcePages(t *testing.T) {
+ for _, tc := range []struct{ name, body string }{
+ {"repeated across entries", `{"logId":"first","result":{"layoutParsingResults":[{"prunedResult":{"page_index":0},"markdown":{"text":"first"}}]}}
+{"logId":"second","result":{"layoutParsingResults":[{"prunedResult":{"page_index":0},"markdown":{"text":"second"}}]}}`},
+ {"decreasing across entries", `{"result":{"layoutParsingResults":[{"prunedResult":{"page_index":2},"markdown":{"text":"first"}}]}}
+{"result":{"layoutParsingResults":[{"prunedResult":{"page_index":1},"markdown":{"text":"second"}}]}}`},
+ {"repeated within entry", `{"result":{"layoutParsingResults":[{"prunedResult":{"page_index":0},"markdown":{"text":"first"}},{"prunedResult":{"page_index":0},"markdown":{"text":"second"}}]}}`},
+ {"decreasing after inferred page", `{"result":{"layoutParsingResults":[{"prunedResult":{"page_index":0},"markdown":{"text":"first"}},{"markdown":{"text":"second"}},{"prunedResult":{"page_index":1},"markdown":{"text":"third"}}]}}`},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ _, err := parsePaddleOCRResultBody([]byte(tc.body))
+ if err == nil || !strings.Contains(err.Error(), "source page index") || !strings.Contains(err.Error(), "entry") {
+ t.Fatalf("expected identifiable source-page error, got %v", err)
}
})
}
+ body := `{"result":{"layoutParsingResults":[{"prunedResult":{"page_index":2},"markdown":{"text":"third"}},{"prunedResult":{"page_index":4},"markdown":{"text":"fifth"}}]}}`
+ response, err := parsePaddleOCRResultBody([]byte(body))
+ if err != nil || len(response.Pages) != 2 || response.Pages[0].Index != 2 || response.Pages[1].Index != 4 {
+ t.Fatalf("valid source-page gaps rejected: response=%+v err=%v", response, err)
+ }
}
diff --git a/internal/entity/models/testdata/paddleocr_layout.json b/internal/entity/models/testdata/paddleocr_layout.json
new file mode 100644
index 0000000000..4cffed37de
--- /dev/null
+++ b/internal/entity/models/testdata/paddleocr_layout.json
@@ -0,0 +1,125 @@
+{
+ "errorCode": 0,
+ "result": {
+ "layoutParsingResults": [
+ {
+ "prunedResult": {
+ "width": 1191,
+ "height": 1684,
+ "parsing_res_list": [
+ {
+ "block_label": "header",
+ "block_content": "IgnoredFurnitureHeader",
+ "block_bbox": [
+ 10,
+ 10,
+ 100,
+ 30
+ ]
+ },
+ {
+ "block_label": "text",
+ "block_content": "Paragraph 1.",
+ "block_bbox": [
+ 174,
+ 146,
+ 1015,
+ 240
+ ],
+ "block_order": 1
+ },
+ {
+ "block_label": "figure_title",
+ "block_content": "Paragraph 2.",
+ "block_bbox": [
+ 175,
+ 270,
+ 592,
+ 298
+ ],
+ "block_order": null
+ },
+ {
+ "block_label": "table",
+ "block_content": "<table><tr><th>Item</th><th>Value</th></tr><tr><td>A</td><td>42</td></tr></table>",
+ "block_bbox": [
+ 167,
+ 298,
+ 1022,
+ 527
+ ],
+ "block_order": null
+ },
+ {
+ "block_label": "text",
+ "block_content": "Paragraph 4.",
+ "block_bbox": [
+ 172,
+ 559,
+ 1017,
+ 652
+ ],
+ "block_order": 2
+ },
+ {
+ "block_label": "text",
+ "block_content": "Paragraph 5.",
+ "block_bbox": [
+ 175,
+ 684,
+ 1013,
+ 743
+ ],
+ "block_order": 3
+ },
+ {
+ "block_label": "table",
+ "block_content": "<table><tr><th>Item</th><th>Value</th></tr><tr><td>A</td><td>43</td></tr></table>",
+ "block_bbox": [
+ 167,
+ 743,
+ 1021,
+ 1062
+ ],
+ "block_order": null
+ },
+ {
+ "block_label": "text",
+ "block_content": "Paragraph 7.",
+ "block_bbox": [
+ 174,
+ 1094,
+ 1016,
+ 1219
+ ],
+ "block_order": 4
+ },
+ {
+ "block_label": "number",
+ "block_content": "IgnoredFurniturePageNumber",
+ "block_bbox": [
+ 10,
+ 1600,
+ 100,
+ 1640
+ ]
+ }
+ ]
+ },
+ "markdown": {
+ "text": "Paragraph 1.\n\nParagraph 2.\n\n<table><tr><th>Item</th><th>Value</th></tr><tr><td>A</td><td>42</td></tr></table>\n\nParagraph 4.\n\nParagraph 5.\n\n<table><tr><th>Item</th><th>Value</th></tr><tr><td>A</td><td>43</td></tr></table>\n\nParagraph 7."
+ }
+ }
+ ],
+ "dataInfo": {
+ "numPages": 1,
+ "pages": [
+ {
+ "width": 1191,
+ "height": 1684
+ }
+ ],
+ "type": "pdf"
+ }
+ }
+}
diff --git a/internal/entity/models/types.go b/internal/entity/models/types.go
index f6a1bc2e5d..4fac15aef0 100644
--- a/internal/entity/models/types.go
+++ b/internal/entity/models/types.go
@@ -124,8 +124,30 @@ type TTSResponse struct {
MediaType string `json:"media_type,omitempty"`
}
+// OCRPage retains the source image dimensions and reading-order blocks.
+// Index is the zero-based source PDF page index, including blank pages.
+type OCRPage struct {
+ Index int `json:"index"`
+ Width float64 `json:"width"`
+ Height float64 `json:"height"`
+ Blocks []OCRBlock `json:"blocks"`
+}
+
+// OCRBlock carries a layout region in source-image pixels. BBox is
+// [left, top, right, bottom]; an absent box is unlocated. Drivers normalize
+// provider coordinates to this rectangle before returning blocks.
+type OCRBlock struct {
+ Text string `json:"text"`
+ Label string `json:"label,omitempty"`
+ BBox []float64 `json:"bbox,omitempty"`
+}
+
type OCRFileResponse struct {
Text *string `json:"text"`
+ // Pages is optional structured output for internal parsing; nil means unavailable.
+ Pages []OCRPage `json:"-"`
+ // Warnings reports partial failures internally; the public response stays text-only.
+ Warnings []string `json:"-"`
}
type ListModelResponse struct {
diff --git a/internal/ingestion/component/chunker/general.go b/internal/ingestion/component/chunker/general.go
index c48172d20c..57c03cc6d7 100644
--- a/internal/ingestion/component/chunker/general.go
+++ b/internal/ingestion/component/chunker/general.go
@@ -292,9 +292,8 @@ func (c *GeneralChunkerComponent) chunkPDF(ctx context.Context, db *gorm.DB, ups
}
// sortPDFUnits orders each contiguous positioned run by physical reading
-// order. Coordinate-free units are barriers: they stay at their parser
-// positions and preserve their relative order instead of being moved to the
-// end of the document.
+// order. Units with explicit parser order or without coordinates are barriers:
+// they stay at their parser positions and preserve their relative order.
func sortPDFUnits(units []schema.ChunkDoc) []schema.ChunkDoc {
type positionedUnit struct {
unit schema.ChunkDoc
@@ -304,7 +303,7 @@ func sortPDFUnits(units []schema.ChunkDoc) []schema.ChunkDoc {
ordered := make([]positionedUnit, 0, len(units))
for _, unit := range units {
row, ok := firstPositionRow(lineRecord{pdfPositions: unit.PDFPositions, positions: unit.Positions})
- ordered = append(ordered, positionedUnit{unit: cloneChunkDoc(unit), row: row, positioned: ok})
+ ordered = append(ordered, positionedUnit{unit: cloneChunkDoc(unit), row: row, positioned: ok && !unit.PreserveOrder})
}
runStart := -1
flush := func(end int) {
diff --git a/internal/ingestion/component/chunker/group.go b/internal/ingestion/component/chunker/group.go
index 2a48eca1ef..e217ab227e 100644
--- a/internal/ingestion/component/chunker/group.go
+++ b/internal/ingestion/component/chunker/group.go
@@ -325,6 +325,9 @@ func buildChunksFromRecordGroups(groups [][]lineRecord, prefixes []string, p *ti
chunkText = removeTag(g[0].text)
}
chunk := map[string]any{"text": chunkText}
+ if g[0].preserveOrder {
+ chunk["_preserve_order"] = true
+ }
if !plain {
first := g[0]
if first.docType != "" {
@@ -540,17 +543,18 @@ func recordsFromStructured(items []schema.ChunkDoc) []lineRecord {
meta[k] = json.RawMessage(v)
}
out = append(out, lineRecord{
- text: recordText,
- docType: dt,
- imgID: imgID,
- image: image,
- layout: it.Layout,
- ckType: it.CKType,
- pdfPositions: it.PDFPositions,
- positions: it.Positions,
- sheet: it.Sheet,
- sheetIndex: it.SheetIndex,
- parentMeta: meta,
+ text: recordText,
+ docType: dt,
+ imgID: imgID,
+ image: image,
+ layout: it.Layout,
+ ckType: it.CKType,
+ pdfPositions: it.PDFPositions,
+ positions: it.Positions,
+ preserveOrder: it.PreserveOrder,
+ sheet: it.Sheet,
+ sheetIndex: it.SheetIndex,
+ parentMeta: meta,
})
}
// TextParser intentionally emits one coordinate-free text item for the
diff --git a/internal/ingestion/component/chunker/manual.go b/internal/ingestion/component/chunker/manual.go
index 3225493489..912fccc09a 100644
--- a/internal/ingestion/component/chunker/manual.go
+++ b/internal/ingestion/component/chunker/manual.go
@@ -12,27 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//
-// ManualChunker is the Go port of Python's `manual` doc-type chunk method
-// (rag/app/manual.py). Like GroupTitleChunker it merges adjacent text records
-// into heading-bounded groups, but it first re-sorts the records into physical
-// reading order before grouping.
-//
-// Why the resort matters: for multi-column / manually-laid-out PDFs the parser
-// emits records in READING order (column-by-column), so a naive grouping
-// interleaves the left column's later content with the right column's earlier
-// content. ManualChunker re-orders records by (page, top, left) — mirroring
-// Python's
-//
-// sorted(sections, key=lambda x: (x[-1][0][0], x[-1][0][3], x[-1][0][1]))
-//
-// so grouping then follows the true top-down, left-to-right layout.
-//
-// Crucially, docx (and any coordinate-free payload) emits records in
-// document-logical order with NO positions — exactly as Python's manual docx
-// branch, which performs no resort. When no record carries coordinates,
-// ManualChunker skips the sort entirely and reuses the identical grouping
-// pipeline as GroupTitleChunker, so its output is bit-for-bit equal (locked by
-// TestManualChunker_NoPositionsEqualsGroupChunker). No parser change is needed.
+// ManualChunker groups adjacent records by heading after sorting PDF records
+// by page, top and left. Records with parser-established order or without
+// coordinates retain their input positions. Geometry is the ordering fallback
+// when the producer has not declared an authoritative reading sequence.
package chunker
import (
@@ -139,9 +122,9 @@ func hasPdfPositions(records []lineRecord) bool {
// sortRecordsByPosition re-orders records into physical reading order
// (page, then top, then left) using a stable sort, so records that share a
-// key keep their original relative order. Records without coordinates act as
-// immovable barriers: they keep their original relative order and are never
-// compared against positioned records.
+// key keep their original relative order. Records with explicit parser order
+// or without coordinates act as immovable barriers: they retain their input
+// positions and are never compared against positioned records.
//
// The slice is split into maximal contiguous runs of positioned records and
// each run is sorted independently. This is required because a single
@@ -166,7 +149,7 @@ func sortRecordsByPosition(records []lineRecord) {
runStart = -1
}
for i := 0; i <= len(records); i++ {
- positioned := i < len(records)
+ positioned := i < len(records) && !records[i].preserveOrder
if positioned {
_, positioned = firstPositionRow(records[i])
}
diff --git a/internal/ingestion/component/chunker/paddleocr_test.go b/internal/ingestion/component/chunker/paddleocr_test.go
new file mode 100644
index 0000000000..d4ea0371af
--- /dev/null
+++ b/internal/ingestion/component/chunker/paddleocr_test.go
@@ -0,0 +1,213 @@
+package chunker
+
+import (
+ "fmt"
+ "net/http"
+ "net/http/httptest"
+ "os"
+ "reflect"
+ "strings"
+ "testing"
+ "time"
+
+ "ragflow/internal/common"
+ "ragflow/internal/entity/models"
+ "ragflow/internal/ingestion/component"
+ "ragflow/internal/ingestion/component/schema"
+ "ragflow/internal/ingestion/task/indexdoc"
+ "ragflow/internal/parser/parser"
+ "ragflow/internal/tokenizer"
+)
+
+func TestPaddleOCRHighlightsThroughPipeline(t *testing.T) {
+ previous := common.AllowAnyHostForTest
+ common.AllowAnyHostForTest = true
+ t.Cleanup(func() { common.AllowAnyHostForTest = previous })
+ tokenizer.SetEngineType("infinity")
+ t.Cleanup(func() { tokenizer.SetEngineType("") })
+ body, err := os.ReadFile("../../../entity/models/testdata/paddleocr_layout.json")
+ if err != nil {
+ t.Fatal(err)
+ }
+ data, err := os.ReadFile("../../../parser/parser/testdata/paddleocr_pages.pdf")
+ if err != nil {
+ t.Fatal(err)
+ }
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "application/json")
+ w.Write(body)
+ }))
+ defer server.Close()
+ driver := models.NewPaddleOCRLocalModel(nil, models.URLSuffix{OCR: "layout-parsing"})
+ model := "PaddleOCR-VL-1.6"
+ response, err := driver.OCRFile(t.Context(), &model, data, nil, &models.APIConfig{BaseURL: &server.URL}, nil, nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ result := parser.PaddleOCRPDFResult(t.Context(), "sample.pdf", data, response)
+ if result.Err != nil {
+ t.Fatal(result.Err)
+ }
+ for _, item := range result.JSON {
+ if strings.Contains(fmt.Sprint(item.ToMap()), "IgnoredFurniture") {
+ t.Fatalf("page furniture emitted as a parser item: %v", item)
+ }
+ }
+ parsed := map[string]any{"name": "sample.pdf", "file_type": "pdf", "output_format": "json", "json": result.JSON, "file": result.File}
+ general, err := NewGeneralChunker(map[string]any{"chunk_token_size": 512, "table_context_size": 32})
+ if err != nil {
+ t.Fatal(err)
+ }
+ chunked, err := general.Invoke(t.Context(), nil, parsed)
+ if err != nil {
+ t.Fatal(err)
+ }
+ tok, err := component.NewTokenizerComponent(map[string]any{"search_method": []any{"full_text"}})
+ if err != nil {
+ t.Fatal(err)
+ }
+ chunked["name"] = "sample.pdf"
+ output, err := tok.Invoke(t.Context(), nil, chunked)
+ if err != nil {
+ t.Fatal(err)
+ }
+ chunks := output["chunks"].([]map[string]any)
+ if len(chunks) == 0 {
+ t.Fatal("no chunks")
+ }
+ if _, err := indexdoc.ProcessChunksForPipeline(chunks, "doc-id", "sample.pdf", time.Unix(1, 0)); err != nil {
+ t.Fatal(err)
+ }
+ tables := 0
+ for _, ck := range chunks {
+ if strings.Contains(fmt.Sprint(ck), "IgnoredFurniture") {
+ t.Fatalf("page furniture leaked into indexed chunk: %v", ck)
+ }
+ if _, exists := ck["_preserve_order"]; exists {
+ t.Fatalf("parser reading-order flag leaked into indexed chunk: %v", ck)
+ }
+ positions, ok := ck["position_int"].([][]int)
+ if !ok || len(positions) == 0 {
+ t.Fatalf("chunk missing indexed positions: %v", ck)
+ }
+ if !reflect.DeepEqual(ck["page_num_int"], []int{1}) && ck["doc_type_kwd"] == "table" {
+ t.Fatalf("table pages=%v", ck["page_num_int"])
+ }
+ for _, box := range positions {
+ if box[0] != 1 || box[2] <= box[1] || box[4] <= box[3] {
+ t.Fatalf("bad position=%v", box)
+ }
+ }
+ if ck["doc_type_kwd"] == "table" {
+ tables++
+ }
+ }
+ if tables != 2 {
+ t.Fatalf("table chunks=%d; want two tables", tables)
+ }
+}
+
+func TestPaddleOCRChunkersPreserveColumnReadingOrder(t *testing.T) {
+ data, err := os.ReadFile("../../../parser/parser/testdata/paddleocr_pages.pdf")
+ if err != nil {
+ t.Fatal(err)
+ }
+ response := &models.OCRFileResponse{Pages: []models.OCRPage{{Index: 0, Width: 1191, Height: 1684, Blocks: []models.OCRBlock{
+ {Text: "LeftFirst", BBox: []float64{10, 10, 400, 60}},
+ {Text: "LeftSecond", BBox: []float64{10, 200, 400, 260}},
+ {Text: "RightFirst", BBox: []float64{600, 10, 1000, 60}},
+ {Text: "RightSecond", BBox: []float64{600, 200, 1000, 260}},
+ }}}}
+ result := parser.PaddleOCRPDFResult(t.Context(), "columns.pdf", data, response)
+ if result.Err != nil {
+ t.Fatal(result.Err)
+ }
+ for _, name := range []string{"general", "manual"} {
+ t.Run(name, func(t *testing.T) {
+ var output map[string]any
+ var err error
+ input := map[string]any{"name": "columns.pdf", "file_type": "pdf", "output_format": "json", "json": result.JSON, "file": result.File}
+ if name == "general" {
+ c, e := NewGeneralChunker(map[string]any{"chunk_token_size": 512})
+ if e != nil {
+ t.Fatal(e)
+ }
+ output, err = c.Invoke(t.Context(), nil, input)
+ } else {
+ c, e := NewManualChunker(map[string]any{"levels": [][]string{{`^# `}}})
+ if e != nil {
+ t.Fatal(e)
+ }
+ output, err = c.Invoke(t.Context(), nil, input)
+ }
+ if err != nil {
+ t.Fatal(err)
+ }
+ var texts []string
+ for _, chunk := range outputChunks(t, output) {
+ texts = append(texts, chunk["text"].(string))
+ }
+ text := strings.Join(texts, "\n")
+ previous := -1
+ for _, word := range []string{"LeftFirst", "LeftSecond", "RightFirst", "RightSecond"} {
+ index := strings.Index(text, word)
+ if index <= previous {
+ t.Fatalf("reading order lost: %q", text)
+ }
+ previous = index
+ }
+ })
+ }
+}
+
+func TestPDFSortingPreservesMarkedBarriersWithinMixedInput(t *testing.T) {
+ units := []schema.ChunkDoc{
+ {Text: "late before", PDFPositions: mustPosJSON(t, 1, 0, 10, 30, 35)},
+ {Text: "early before", PDFPositions: mustPosJSON(t, 1, 0, 10, 10, 15)},
+ {Text: "first marked", PreserveOrder: true, PDFPositions: mustPosJSON(t, 1, 0, 10, 90, 95)},
+ {Text: "second marked", PreserveOrder: true, PDFPositions: mustPosJSON(t, 1, 0, 10, 5, 10)},
+ {Text: "late after", PDFPositions: mustPosJSON(t, 1, 0, 10, 40, 45)},
+ {Text: "early after", PDFPositions: mustPosJSON(t, 1, 0, 10, 20, 25)},
+ }
+ want := []string{"early before", "late before", "first marked", "second marked", "early after", "late after"}
+ t.Run("general", func(t *testing.T) {
+ got := generalChunkTexts(sortPDFUnits(units))
+ if !reflect.DeepEqual(got, want) {
+ t.Fatalf("mixed reading order=%v; want %v", got, want)
+ }
+ })
+ t.Run("manual", func(t *testing.T) {
+ records := recordsFromStructured(units)
+ sortRecordsByPosition(records)
+ var got []string
+ for _, record := range records {
+ got = append(got, record.text)
+ }
+ if !reflect.DeepEqual(got, want) {
+ t.Fatalf("mixed reading order=%v; want %v", got, want)
+ }
+ })
+}
+
+func TestPaddleOCRCaptionsRemainBodyInTitleChunking(t *testing.T) {
+ response := &models.OCRFileResponse{Pages: []models.OCRPage{{Index: 0, Blocks: []models.OCRBlock{
+ {Text: "Document title", Label: "doc_title"},
+ {Text: "Figure caption", Label: "figure_title"},
+ {Text: "Table caption", Label: "table_title"},
+ {Text: "Ordinary body.", Label: "text"},
+ }}}}
+ result := parser.PaddleOCRPDFResult(t.Context(), "captions.pdf", nil, response)
+ if result.Err != nil {
+ t.Fatal(result.Err)
+ }
+ units, _, err := schema.ChunkDocsFromDocItem(result.JSON)
+ if err != nil {
+ t.Fatal(err)
+ }
+ records := recordsFromStructured(units)
+ params := defaultsTitle()
+ levels := resolveTitleLevels(records, &params)
+ if len(levels) != 4 || levels[0] >= bodyLevel || levels[1] != bodyLevel || levels[2] != bodyLevel || levels[3] != bodyLevel {
+ t.Fatalf("caption became a section heading: levels=%v records=%+v", levels, records)
+ }
+}
diff --git a/internal/ingestion/component/chunker/title.go b/internal/ingestion/component/chunker/title.go
index 4bf5400223..4ff195dbc4 100644
--- a/internal/ingestion/component/chunker/title.go
+++ b/internal/ingestion/component/chunker/title.go
@@ -635,17 +635,18 @@ func lineRecordsFromText(text string) []lineRecord {
// common.py:extract_line_records yields. Used by Group/Hierarchy
// chunk-builders.
type lineRecord struct {
- text string
- docType string
- imgID *string
- image string
- layout string
- ckType string
- pdfPositions json.RawMessage
- positions json.RawMessage
- sheet string
- sheetIndex *int
- parentMeta map[string]any
+ text string
+ docType string
+ imgID *string
+ image string
+ layout string
+ ckType string
+ pdfPositions json.RawMessage
+ positions json.RawMessage
+ preserveOrder bool
+ sheet string
+ sheetIndex *int
+ parentMeta map[string]any
}
func (r lineRecord) textOrEmpty() string { return r.text }
diff --git a/internal/ingestion/component/parser.go b/internal/ingestion/component/parser.go
index 9d0c421d38..a66da12a33 100644
--- a/internal/ingestion/component/parser.go
+++ b/internal/ingestion/component/parser.go
@@ -624,7 +624,12 @@ func resolveParserNormalizationSource(dispatched parser.ParseResult) string {
}
func reportParserWarnings(ctx context.Context, warnings []string) {
+ seen := make(map[string]struct{}, len(warnings))
for _, warning := range warnings {
+ if _, exists := seen[warning]; exists {
+ continue
+ }
+ seen[warning] = struct{}{}
runtime.ReportProgressMessage(ctx, "Parser", "WARNING: "+warning)
}
}
diff --git a/internal/ingestion/component/parser_test.go b/internal/ingestion/component/parser_test.go
index fb7065d7ac..58b216e5b6 100644
--- a/internal/ingestion/component/parser_test.go
+++ b/internal/ingestion/component/parser_test.go
@@ -575,3 +575,12 @@ func TestParserComponent_Invoke_AcceptsBytes(t *testing.T) {
t.Errorf("json = %+v, want [alpha beta]", jsonItems)
}
}
+
+func TestReportParserWarningsDeduplicatesMessages(t *testing.T) {
+ var got []string
+ ctx := runtime.WithProgressMessageCallback(t.Context(), func(component, message string) { got = append(got, component+": "+message) })
+ reportParserWarnings(ctx, []string{"repeated", "repeated", "distinct", "repeated"})
+ if len(got) != 2 || got[0] != "Parser: WARNING: repeated" || got[1] != "Parser: WARNING: distinct" {
+ t.Fatalf("warnings=%v", got)
+ }
+}
diff --git a/internal/ingestion/component/pdf_vision_dispatch.go b/internal/ingestion/component/pdf_vision_dispatch.go
index 1f146dd179..a607da0a2a 100644
--- a/internal/ingestion/component/pdf_vision_dispatch.go
+++ b/internal/ingestion/component/pdf_vision_dispatch.go
@@ -668,11 +668,8 @@ func defaultResolvePaddleOCRModelForDispatch(ctx context.Context, db *gorm.DB, t
return driver, modelName, apiConfig, err
}
-// dispatchPaddleOCRPdf submits a PDF to the tenant's PaddleOCR OCR model and
-// returns parsed sections. The resolved driver runs the protocol it knows:
-// the cloud "PaddleOCR" driver submits a job and polls the v2/ocr/jobs
-// endpoint, the local "PaddleOCR.local" driver POSTs synchronously to
-// layout-parsing — both mirror the Python paddleocr_parser paths.
+// dispatchPaddleOCRPdf resolves the tenant's cloud or local OCR model and
+// converts its page blocks to the structured PDF parser output.
func dispatchPaddleOCRPdf(
ctx context.Context,
db *gorm.DB,
@@ -697,14 +694,9 @@ func dispatchPaddleOCRPdf(
if err != nil {
return parser.ParseResult{}, fmt.Errorf("parser: PaddleOCR OCRFile: %w", err)
}
- if resp == nil || resp.Text == nil {
- return parser.ParseResult{}, fmt.Errorf("parser: PaddleOCR returned empty text")
- }
- if strings.TrimSpace(*resp.Text) == "" {
- return parser.ParseResult{}, fmt.Errorf("parser: PaddleOCR returned empty text")
- }
- return buildMarkdownOCRDispatchResult(*resp.Text), nil
+ result := parser.PaddleOCRPDFResult(ctx, filename, binary, resp)
+ return result, result.Err
}
// resolveMinerUBaseURL extracts the resolved base URL from a model driver.
diff --git a/internal/ingestion/component/pdf_vision_dispatch_test.go b/internal/ingestion/component/pdf_vision_dispatch_test.go
index 312e030e56..faf45f7ad2 100644
--- a/internal/ingestion/component/pdf_vision_dispatch_test.go
+++ b/internal/ingestion/component/pdf_vision_dispatch_test.go
@@ -22,6 +22,7 @@ import (
"encoding/json"
"net/http"
"net/http/httptest"
+ "os"
"strings"
"testing"
"time"
@@ -172,91 +173,44 @@ func TestDispatchMonkeyOCRv2PDFUsesSelectedCompositeModel(t *testing.T) {
// the OCRFile method needed by dispatchPaddleOCRPdf.
type paddleOCRFakeDriver struct {
modelModule.ModelDriver
- text string
+ pages []modelModule.OCRPage
}
func (d *paddleOCRFakeDriver) Name() string { return "PaddleOCR" }
func (d *paddleOCRFakeDriver) OCRFile(_ context.Context, _ *string, _ []byte, _ *string, _ *modelModule.APIConfig, _ *modelModule.OCRConfig, _ *common.ModelUsage) (*modelModule.OCRFileResponse, error) {
- return &modelModule.OCRFileResponse{Text: &d.text}, nil
+ return &modelModule.OCRFileResponse{Pages: d.pages}, nil
}
-// TestDispatchPaddleOCRPdfPreservesMarkdownForCentralNormalization verifies
-// that PaddleOCR dispatch returns backend Markdown for the shared Parser
-// output normalizer, regardless of the normalized component setup format.
-func TestDispatchPaddleOCRPdfPreservesMarkdownForCentralNormalization(t *testing.T) {
- orig := resolvePaddleOCRModelForDispatch
- t.Cleanup(func() { resolvePaddleOCRModelForDispatch = orig })
-
- md := "## 《道德经》全文及翻译\n\n道可道,非常道。"
+func TestDispatchPaddleOCRPDFUsesStructuredOutput(t *testing.T) {
+ original := resolvePaddleOCRModelForDispatch
+ t.Cleanup(func() { resolvePaddleOCRModelForDispatch = original })
resolvePaddleOCRModelForDispatch = func(context.Context, *gorm.DB, string, string) (modelModule.ModelDriver, string, *modelModule.APIConfig, error) {
- baseURL := "http://localhost:9380"
- return &paddleOCRFakeDriver{text: md}, "ocr-model", &modelModule.APIConfig{BaseURL: &baseURL}, nil
- }
-
- // 1. output_format="json": preserve Markdown for central normalization.
- res, err := dispatchPaddleOCRPdf(t.Context(), dao.DB, "test.pdf", []byte("%PDF-1.4"), "tenant", schema.ParserSetup{"output_format": "json"}, "some-uuid")
- if err != nil {
- t.Fatalf("dispatchPaddleOCRPdf: %v", err)
- }
- if res.OutputFormat != "markdown" {
- t.Errorf("OutputFormat = %q, want markdown", res.OutputFormat)
- }
- if res.JSON != nil {
- t.Fatalf("res.JSON = %#v, want nil before central normalization", res.JSON)
- }
- if res.Markdown != md {
- t.Errorf("Markdown = %q, want %q", res.Markdown, md)
- }
-
- // 2. output_format="markdown": the backend payload is unchanged.
- resMD, err := dispatchPaddleOCRPdf(t.Context(), dao.DB, "test.pdf", []byte("%PDF-1.4"), "tenant", schema.ParserSetup{"output_format": "markdown"}, "some-uuid")
- if err != nil {
- t.Fatalf("dispatchPaddleOCRPdf (markdown): %v", err)
+ return &paddleOCRFakeDriver{pages: []modelModule.OCRPage{{Index: 0, Blocks: []modelModule.OCRBlock{{Text: "# Heading\n\nBody"}}}}}, "ocr-model", &modelModule.APIConfig{}, nil
}
- if resMD.OutputFormat != "markdown" {
- t.Errorf("OutputFormat = %q, want markdown", resMD.OutputFormat)
- }
- if resMD.Markdown != md {
- t.Errorf("Markdown = %q, want %q", resMD.Markdown, md)
- }
- if resMD.JSON != nil {
- t.Errorf("resMD.JSON = %#v, want nil before central normalization", resMD.JSON)
- }
-
- // 3. Default setup (no output_format key) follows the same central path.
- resDef, err := dispatchPaddleOCRPdf(t.Context(), dao.DB, "test.pdf", []byte("%PDF-1.4"), "tenant", nil, "some-uuid")
- if err != nil {
- t.Fatalf("dispatchPaddleOCRPdf (default setup): %v", err)
- }
- if resDef.OutputFormat != "markdown" {
- t.Errorf("OutputFormat = %q, want markdown", resDef.OutputFormat)
- }
- if resDef.JSON != nil {
- t.Fatalf("resDef.JSON = %#v, want nil before central normalization", resDef.JSON)
- }
- if resDef.Markdown != md {
- t.Errorf("Markdown = %q, want %q", resDef.Markdown, md)
+ for _, format := range []string{"json", "markdown", ""} {
+ result, err := dispatchPaddleOCRPdf(t.Context(), nil, "sample.pdf", nil, "tenant", schema.ParserSetup{"output_format": format}, "model")
+ if err != nil {
+ t.Fatal(err)
+ }
+ if result.OutputFormat != "json" || len(result.JSON) == 0 {
+ t.Fatalf("result=%+v", result)
+ }
}
}
-// TestDispatchPaddleOCRPdfEmptyTextFails guards against the silent-empty
-// result: OCRFileResponse.Text is a *string that stays non-nil even when the
-// backend produced zero text, so the old nil-only guard let an empty payload
-// through and the pipeline emitted a "completed with 0 chunks" document.
-// Empty text must surface as an explicit error instead.
-func TestDispatchPaddleOCRPdfEmptyTextFails(t *testing.T) {
+func TestDispatchPaddleOCRPDFRejectsMissingBlocks(t *testing.T) {
orig := resolvePaddleOCRModelForDispatch
t.Cleanup(func() { resolvePaddleOCRModelForDispatch = orig })
resolvePaddleOCRModelForDispatch = func(context.Context, *gorm.DB, string, string) (modelModule.ModelDriver, string, *modelModule.APIConfig, error) {
baseURL := "http://localhost:9380"
- return &paddleOCRFakeDriver{text: ""}, "ocr-model", &modelModule.APIConfig{BaseURL: &baseURL}, nil
+ return &paddleOCRFakeDriver{}, "ocr-model", &modelModule.APIConfig{BaseURL: &baseURL}, nil
}
res, err := dispatchPaddleOCRPdf(t.Context(), dao.DB, "test.pdf", []byte("%PDF-1.4"), "tenant", nil, "some-uuid")
if err == nil {
- t.Fatalf("dispatchPaddleOCRPdf with empty text: expected error, got result %+v", res)
+ t.Fatalf("dispatchPaddleOCRPdf without page blocks: expected error, got result %+v", res)
}
if res.OutputFormat != "" || res.Markdown != "" {
t.Errorf("expected zero-value result on error, got %+v", res)
@@ -343,3 +297,50 @@ func TestDispatchMinerUPDFRequiresBaseURL(t *testing.T) {
t.Fatalf("dispatchMinerUPDF empty URL error = %v", err)
}
}
+
+func TestDispatchPaddleOCRPDFPreservesLayout(t *testing.T) {
+ withSSRFBypass(t)
+ body, err := os.ReadFile("../../entity/models/testdata/paddleocr_layout.json")
+ if err != nil {
+ t.Fatal(err)
+ }
+ data, err := os.ReadFile("../../parser/parser/testdata/paddleocr_pages.pdf")
+ if err != nil {
+ t.Fatal(err)
+ }
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "application/json")
+ w.Write(body)
+ }))
+ defer server.Close()
+ original := resolvePaddleOCRModelForDispatch
+ t.Cleanup(func() { resolvePaddleOCRModelForDispatch = original })
+ resolvePaddleOCRModelForDispatch = func(context.Context, *gorm.DB, string, string) (modelModule.ModelDriver, string, *modelModule.APIConfig, error) {
+ return modelModule.NewPaddleOCRLocalModel(nil, modelModule.URLSuffix{OCR: "layout-parsing"}), "PaddleOCR-VL-1.6", &modelModule.APIConfig{BaseURL: &server.URL}, nil
+ }
+ result, err := dispatchPaddleOCRPdf(t.Context(), nil, "sample.pdf", data, "tenant", nil, "model-id")
+ if err != nil {
+ t.Fatal(err)
+ }
+ if result.OutputFormat != "json" || len(result.JSON) != 7 {
+ t.Fatalf("result=%+v", result)
+ }
+ if result.JSON[0].ToMap()["positions"] == nil || result.JSON[2].ToMap()["table"] == nil {
+ t.Fatalf("items=%v", result.JSON)
+ }
+}
+
+func TestDispatchPaddleOCRPDFAcceptsLocatedBlocksWithoutText(t *testing.T) {
+ original := resolvePaddleOCRModelForDispatch
+ t.Cleanup(func() { resolvePaddleOCRModelForDispatch = original })
+ resolvePaddleOCRModelForDispatch = func(context.Context, *gorm.DB, string, string) (modelModule.ModelDriver, string, *modelModule.APIConfig, error) {
+ return &paddleOCRFakeDriver{pages: []modelModule.OCRPage{{Index: 0, Blocks: []modelModule.OCRBlock{{Text: "body", Label: "text"}}}}}, "ocr-model", &modelModule.APIConfig{}, nil
+ }
+ result, err := dispatchPaddleOCRPdf(t.Context(), nil, "sample.pdf", nil, "tenant", nil, "model")
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(result.JSON) != 1 {
+ t.Fatalf("items=%v", result.JSON)
+ }
+}
diff --git a/internal/ingestion/component/schema/chunk_types.go b/internal/ingestion/component/schema/chunk_types.go
index b5a66e122c..dce014701a 100644
--- a/internal/ingestion/component/schema/chunk_types.go
+++ b/internal/ingestion/component/schema/chunk_types.go
@@ -131,6 +131,9 @@ type ChunkDoc struct {
Positions json.RawMessage `json:"positions,omitempty"`
Sheet string `json:"sheet,omitempty"`
SheetIndex *int `json:"sheet_index,omitempty"`
+ // PreserveOrder declares the input sequence authoritative; geometry sorting
+ // must retain it. PaddleOCR sets this for its ordered layout blocks.
+ PreserveOrder bool `json:"_preserve_order,omitempty"`
// TableData is the structured table contract. When a chunk carries it
// the table content is consumed directly from here instead of being
// re-parsed from Text markup. It must stay out of Extra (see the
@@ -159,7 +162,7 @@ func (d *ChunkDoc) UnmarshalJSON(data []byte) error {
"context_above", "context_below", "questions", "keywords", "summary",
"chunk_order_int", "title_tks", "title_sm_tks", "content_ltks",
"content_sm_ltks", "tag_kwd", "page_number", "top_int", "_pdf_positions", "positions",
- "sheet", "sheet_index", "table",
+ "sheet", "sheet_index", "table", "_preserve_order",
} {
delete(raw, key)
}
diff --git a/internal/ingestion/task/indexdoc/process.go b/internal/ingestion/task/indexdoc/process.go
index f3b06ddbbd..78684dedbb 100644
--- a/internal/ingestion/task/indexdoc/process.go
+++ b/internal/ingestion/task/indexdoc/process.go
@@ -156,7 +156,7 @@ func cleanupConsumedChunkFields(ck map[string]any) {
// and Infinity would drop each key at insert with a warning.
var pipelineOnlyFields = []string{
"ck_type", "tk_nums", "layout", "layout_type", "layoutno", "image",
- "context_above", "context_below", "page_number",
+ "context_above", "context_below", "page_number", "_preserve_order",
"table_id", "sheet", "sheet_index", "headers", "cells",
"row_start", "row_end", "col_start", "col_end",
}
diff --git a/internal/parser/parser/paddleocr.go b/internal/parser/parser/paddleocr.go
deleted file mode 100644
index 4b4e64d7fe..0000000000
--- a/internal/parser/parser/paddleocr.go
+++ /dev/null
@@ -1,462 +0,0 @@
-//
-// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-
-// PaddleOCR client for the async Job API.
-// Mirrors Python's deepdoc/parser/paddleocr_parser.py:
-// - Submit job via POST /api/v2/ocr/jobs
-// - Poll with exponential backoff until "done" or "failed"
-// - Fetch result JSONL and extract text
-//
-// Both image (parse_image) and PDF (parse_pdf) parsing go through the same
-// Job API; the file extension selects the service-side file type.
-
-package parser
-
-import (
- "bytes"
- "context"
- "encoding/json"
- "fmt"
- "io"
- "mime/multipart"
- "net/http"
- "regexp"
- "strings"
- "time"
-
- "go.uber.org/zap"
-
- "ragflow/internal/common"
-)
-
-// imgTagPattern matches HTML <img> tags and <div> wrappers, mirroring
-// Python's _MARKDOWN_IMAGE_PATTERN in deepdoc/parser/paddleocr_parser.py.
-var imgTagPattern = regexp.MustCompile(
- `(?is)<div[^>]*>\s*<img[^>]*/>\s*</div>|<img[^>]*/>`,
-)
-
-const (
- defaultPaddleOCRBaseURL = "https://paddleocr.aistudio-app.com"
- defaultPaddleOCRTimeout = 600 * time.Second
- defaultPaddleOCRAlgo = "PaddleOCR-VL"
- paddleOCRSubmitPath = "/api/v2/ocr/jobs"
- paddleOCRPollInterval = 3 * time.Second
- paddleOCRPollMultiplier = 1.5
- paddleOCRPollMaxInterval = 15 * time.Second
-)
-
-// PaddleOCRClient talks to the PaddleOCR async Job API.
-type PaddleOCRClient struct {
- BaseURL string
- AccessToken string
- Algorithm string
- Timeout time.Duration
- httpClient *http.Client
-}
-
-// NewPaddleOCRClientFromEnv creates a client from environment variables:
-//
-// PADDLEOCR_BASE_URL – base URL (default https://paddleocr.aistudio-app.com)
-// PADDLEOCR_ACCESS_TOKEN – bearer token
-// PADDLEOCR_ALGORITHM – algorithm name (default PaddleOCR-VL)
-func NewPaddleOCRClientFromEnv() *PaddleOCRClient {
- return newPaddleOCRClient(
- common.GetEnv(common.EnvPaddleOCRBaseUrl),
- common.GetEnv(common.EnvPaddleOCRAccessToken),
- common.GetEnv(common.EnvPaddleOCRAlgorithm),
- )
-}
-
-// newPaddleOCRClient builds a client, falling back to the default base URL and
-// algorithm when the given values are empty.
-func newPaddleOCRClient(baseURL, accessToken, algorithm string) *PaddleOCRClient {
- if strings.TrimSpace(baseURL) == "" {
- baseURL = defaultPaddleOCRBaseURL
- }
- return &PaddleOCRClient{
- BaseURL: strings.TrimRight(strings.TrimSpace(baseURL), "/"),
- AccessToken: strings.TrimSpace(accessToken),
- Algorithm: firstNonEmpty(strings.TrimSpace(algorithm), defaultPaddleOCRAlgo),
- Timeout: defaultPaddleOCRTimeout,
- httpClient: &http.Client{
- Timeout: 30 * time.Second, // per-request timeout; polling uses deadline
- },
- }
-}
-
-// Enabled reports whether the client has a usable access token.
-func (c *PaddleOCRClient) Enabled() bool {
- return c != nil && c.AccessToken != ""
-}
-
-// ParseImage submits the image to PaddleOCR and returns extracted text.
-// Mirrors Python's PaddleOCRParser.parse_image().
-func (c *PaddleOCRClient) ParseImage(binary []byte, filename string) (string, error) {
- raw, err := c.submitAndFetch(binary, filename)
- if err != nil {
- return "", err
- }
- return c.extractImageText(raw), nil
-}
-
-// ParsePDF submits the PDF to PaddleOCR and returns extracted markdown text.
-// Mirrors Python's PaddleOCRParser.parse_pdf(): the file is uploaded through
-// the same async Job API under a ".pdf" name so the service applies PDF
-// parsing, then the JSONL results are flattened exactly like parse_image
-// (layout blocks with a fallback to rec_texts).
-func (c *PaddleOCRClient) ParsePDF(binary []byte, filename string) (string, error) {
- pdfName := strings.TrimSpace(filename)
- if !strings.HasSuffix(strings.ToLower(pdfName), ".pdf") {
- pdfName = "document.pdf" // Python always submits the PDF as document.pdf
- }
- raw, err := c.submitAndFetch(binary, pdfName)
- if err != nil {
- return "", err
- }
- return c.extractImageText(raw), nil
-}
-
-// submitAndFetch runs the async Job API round trip: submit, poll, fetch.
-func (c *PaddleOCRClient) submitAndFetch(binary []byte, filename string) ([]map[string]any, error) {
- deadline := time.Now().Add(c.Timeout)
-
- jobID, err := c.submitJob(binary, filename, deadline)
- if err != nil {
- return nil, fmt.Errorf("paddleocr submit: %w", err)
- }
- resultData, err := c.pollJob(jobID, deadline)
- if err != nil {
- return nil, fmt.Errorf("paddleocr poll: %w", err)
- }
- raw, err := c.fetchResult(resultData, deadline)
- if err != nil {
- return nil, fmt.Errorf("paddleocr fetch: %w", err)
- }
- return raw, nil
-}
-
-// submitJob POSTs the file to /api/v2/ocr/jobs and returns the job ID.
-func (c *PaddleOCRClient) submitJob(binary []byte, filename string, deadline time.Time) (string, error) {
- var buf bytes.Buffer
- w := multipart.NewWriter(&buf)
-
- // Write the image file field
- fw, err := w.CreateFormFile("file", filename)
- if err != nil {
- return "", err
- }
- if _, err := fw.Write(binary); err != nil {
- return "", err
- }
-
- // Write form fields
- if err := w.WriteField("model", c.Algorithm); err != nil {
- return "", err
- }
- optionalPayload := map[string]any{"formatBlockContent": true}
- payloadBytes, _ := json.Marshal(optionalPayload)
- if err := w.WriteField("optionalPayload", string(payloadBytes)); err != nil {
- return "", err
- }
- w.Close()
-
- url := c.BaseURL + paddleOCRSubmitPath
- req, err := http.NewRequest(http.MethodPost, url, &buf)
- if err != nil {
- return "", err
- }
- req.Header.Set("Content-Type", w.FormDataContentType())
- req.Header.Set("Client-Platform", "ragflow")
- if c.AccessToken != "" {
- req.Header.Set("Authorization", "Bearer "+c.AccessToken)
- }
- req = req.WithContext(withDeadline(req.Context(), deadline))
-
- resp, err := c.httpClient.Do(req)
- if err != nil {
- return "", fmt.Errorf("POST %s: %w", url, err)
- }
- defer resp.Body.Close()
-
- if resp.StatusCode != 200 {
- body, _ := io.ReadAll(io.LimitReader(resp.Body, 4096))
- return "", fmt.Errorf("HTTP %d: %s", resp.StatusCode, string(body))
- }
-
- var submitResp struct {
- Data struct {
- JobID string `json:"jobId"`
- } `json:"data"`
- JobID string `json:"jobId"`
- }
- if err := json.NewDecoder(resp.Body).Decode(&submitResp); err != nil {
- return "", fmt.Errorf("decode submit response: %w", err)
- }
- jobID := submitResp.Data.JobID
- if jobID == "" {
- jobID = submitResp.JobID
- }
- if jobID == "" {
- return "", fmt.Errorf("no jobId in submit response")
- }
- common.Info("paddleocr: job submitted", zap.String("jobId", jobID))
- return jobID, nil
-}
-
-// pollJob polls the job status until it reaches "done" or "failed".
-// Uses exponential backoff: 3s initial, 1.5x multiplier, 15s max.
-// Returns the final poll response data.
-func (c *PaddleOCRClient) pollJob(jobID string, deadline time.Time) (map[string]any, error) {
- pollURL := fmt.Sprintf("%s/%s", c.BaseURL+paddleOCRSubmitPath, jobID)
- interval := paddleOCRPollInterval
-
- for {
- if time.Now().After(deadline) {
- return nil, fmt.Errorf("job %s timed out after %v", jobID, c.Timeout)
- }
- remaining := time.Until(deadline)
- if remaining <= 0 {
- return nil, fmt.Errorf("job %s timed out", jobID)
- }
-
- req, err := http.NewRequest(http.MethodGet, pollURL, nil)
- if err != nil {
- return nil, err
- }
- if c.AccessToken != "" {
- req.Header.Set("Authorization", "Bearer "+c.AccessToken)
- }
- req.Header.Set("Client-Platform", "ragflow")
- req = req.WithContext(withDeadline(req.Context(), deadline))
-
- resp, err := c.httpClient.Do(req)
- if err != nil {
- return nil, fmt.Errorf("poll %s: %w", jobID, err)
- }
- defer resp.Body.Close()
-
- if resp.StatusCode != 200 {
- body, _ := io.ReadAll(io.LimitReader(resp.Body, 4096))
- return nil, fmt.Errorf("poll HTTP %d: %s", resp.StatusCode, string(body))
- }
-
- var pollResp struct {
- Data struct {
- State string `json:"state"`
- ErrorMsg string `json:"errorMsg"`
- } `json:"data"`
- State string `json:"state"`
- ErrorMsg string `json:"errorMsg"`
- }
- bodyBytes, _ := io.ReadAll(resp.Body)
- if err := json.Unmarshal(bodyBytes, &pollResp); err != nil {
- return nil, fmt.Errorf("decode poll response: %w", err)
- }
- state := pollResp.Data.State
- if state == "" {
- state = pollResp.State
- }
-
- switch state {
- case "done":
- common.Info("paddleocr: job done", zap.String("jobId", jobID))
- // Return full data so caller can extract resultJsonUrl
- var fullData map[string]any
- json.Unmarshal(bodyBytes, &fullData)
- return fullData, nil
- case "failed":
- errMsg := pollResp.Data.ErrorMsg
- if errMsg == "" {
- errMsg = pollResp.ErrorMsg
- }
- return nil, fmt.Errorf("job %s failed: %s", jobID, errMsg)
- }
-
- // Exponential backoff
- sleepTime := interval
- if remaining < sleepTime {
- sleepTime = remaining
- }
- time.Sleep(sleepTime)
- interval = time.Duration(float64(interval) * paddleOCRPollMultiplier)
- if interval > paddleOCRPollMaxInterval {
- interval = paddleOCRPollMaxInterval
- }
- }
-}
-
-// fetchResult downloads and parses the JSONL result from resultJsonUrl.
-func (c *PaddleOCRClient) fetchResult(pollData map[string]any, deadline time.Time) ([]map[string]any, error) {
- data, _ := pollData["data"].(map[string]any)
- if data == nil {
- data = pollData
- }
- resultJSONURL, _ := data["resultJsonUrl"].(string)
- if resultJSONURL == "" {
- if resultURL, ok := data["resultUrl"].(map[string]any); ok {
- resultJSONURL, _ = resultURL["jsonUrl"].(string)
- }
- }
- if resultJSONURL == "" {
- return nil, fmt.Errorf("no resultJsonUrl in completion response")
- }
-
- req, err := http.NewRequest(http.MethodGet, resultJSONURL, nil)
- if err != nil {
- return nil, err
- }
- if c.AccessToken != "" {
- req.Header.Set("Authorization", "Bearer "+c.AccessToken)
- }
- req = req.WithContext(withDeadline(req.Context(), deadline))
-
- resp, err := c.httpClient.Do(req)
- if err != nil {
- return nil, fmt.Errorf("fetch result: %w", err)
- }
- defer resp.Body.Close()
-
- if resp.StatusCode != 200 {
- body, _ := io.ReadAll(io.LimitReader(resp.Body, 4096))
- return nil, fmt.Errorf("fetch result HTTP %d: %s", resp.StatusCode, string(body))
- }
-
- content, err := io.ReadAll(resp.Body)
- if err != nil {
- return nil, fmt.Errorf("read result: %w", err)
- }
-
- // Parse JSONL: one JSON object per line
- var lines []map[string]any
- for _, line := range strings.Split(strings.TrimSpace(string(content)), "\n") {
- line = strings.TrimSpace(line)
- if line == "" {
- continue
- }
- var obj map[string]any
- if err := json.Unmarshal([]byte(line), &obj); err != nil {
- return nil, fmt.Errorf("parse JSONL line: %w", err)
- }
- lines = append(lines, obj)
- }
- return lines, nil
-}
-
-// extractImageText extracts text from PaddleOCR JSONL results.
-// Mirrors Python's parse_image() text extraction:
-//
-// layoutParsingResults[].prunedResult.parsing_res_list[].block_content
-// Fallback: ocrResults[].prunedResult.rec_texts[]
-func (c *PaddleOCRClient) extractImageText(resultLines []map[string]any) string {
- var texts []string
-
- for _, line := range resultLines {
- result, _ := line["result"].(map[string]any)
- if result == nil {
- continue
- }
- // Primary: layoutParsingResults
- if lpr, ok := result["layoutParsingResults"].([]any); ok {
- for _, lr := range lpr {
- lrMap, _ := lr.(map[string]any)
- if lrMap == nil {
- continue
- }
- pruned, _ := lrMap["prunedResult"].(map[string]any)
- if pruned == nil {
- continue
- }
- if prl, ok := pruned["parsing_res_list"].([]any); ok {
- for _, block := range prl {
- blockMap, _ := block.(map[string]any)
- if blockMap == nil {
- continue
- }
- content, _ := blockMap["block_content"].(string)
- content = strings.TrimSpace(content)
- // Remove Markdown image blocks
- content = removeMarkdownImages(content)
- if content != "" {
- texts = append(texts, content)
- }
- }
- }
- }
- }
- }
-
- // Fallback: ocrResults for text-only models (e.g. PP-OCRv6)
- if len(texts) == 0 {
- for _, line := range resultLines {
- result, _ := line["result"].(map[string]any)
- if result == nil {
- continue
- }
- if ocr, ok := result["ocrResults"].([]any); ok {
- for _, o := range ocr {
- ocrMap, _ := o.(map[string]any)
- if ocrMap == nil {
- continue
- }
- pruned, _ := ocrMap["prunedResult"].(map[string]any)
- if pruned == nil {
- continue
- }
- if recTexts, ok := pruned["rec_texts"].([]any); ok {
- for _, t := range recTexts {
- if s, ok := t.(string); ok {
- s = strings.TrimSpace(s)
- if s != "" {
- texts = append(texts, s)
- }
- }
- }
- }
- }
- }
- }
- }
-
- if len(texts) == 0 {
- return ""
- }
- return strings.Join(texts, "\n")
-}
-
-// removeMarkdownImages strips markdown image syntax (![](...)) and HTML
-// img/div blocks, mirroring Python's _remove_images_from_markdown.
-func removeMarkdownImages(md string) string {
- // Strip HTML <img> and <div><img/></div> blocks
- md = imgTagPattern.ReplaceAllString(md, "")
- return strings.TrimSpace(md)
-}
-
-// withDeadline applies a deadline to a context, respecting the existing
-// deadline if it is sooner.
-func withDeadline(ctx context.Context, deadline time.Time) context.Context {
- if d, ok := ctx.Deadline(); ok && d.Before(deadline) {
- return ctx
- }
- newCtx, _ := context.WithDeadline(ctx, deadline)
- return newCtx
-}
-
-func firstNonEmpty(a, b string) string {
- if a != "" {
- return a
- }
- return b
-}
diff --git a/internal/parser/parser/pdf_analyzer_dispatch_entry_test.go b/internal/parser/parser/pdf_analyzer_dispatch_entry_test.go
index a213f4dfdb..607d12ce41 100644
--- a/internal/parser/parser/pdf_analyzer_dispatch_entry_test.go
+++ b/internal/parser/parser/pdf_analyzer_dispatch_entry_test.go
@@ -64,7 +64,7 @@ func TestValidateParseMethod_UnregisteredUnknownMethodStillRejected(t *testing.T
// Sanity guard: built-in methods continue to validate as before.
func TestValidateParseMethod_KnownBuiltinMethodAccepted(t *testing.T) {
- for _, m := range []string{"", "deepdoc", "plain_text", "mineru", "paddleocr", "docling", "opendataloader", "somark", "tcadp"} {
+ for _, m := range []string{"", "deepdoc", "plain_text", "mineru", "docling", "opendataloader", "somark", "tcadp"} {
if err := (&PDFParser{ParseMethod: m}).validateParseMethod(); err != nil {
t.Fatalf("built-in method %q should pass validation, got err: %v", m, err)
}
diff --git a/internal/parser/parser/pdf_analyzer_dispatch_register_test.go b/internal/parser/parser/pdf_analyzer_dispatch_register_test.go
index 8989e14ec3..bf52bfb63b 100644
--- a/internal/parser/parser/pdf_analyzer_dispatch_register_test.go
+++ b/internal/parser/parser/pdf_analyzer_dispatch_register_test.go
@@ -19,12 +19,9 @@ func mustPanic(t *testing.T, name string, f func()) {
f()
}
-// Built-in methods that are dispatched by an internal engine (every switch case
-// in ParseWithResult except the fall-through to DeepDoc) cannot be overridden by
-// an external factory — the switch intercepts them before resolution runs, so a
-// factory registered for them would be silently ignored. RegisterAnalyzerFactory
-// must reject such registrations instead of letting them fail silently.
-func TestRegisterAnalyzerFactory_RejectsBuiltinEngineMethods(t *testing.T) {
+// Built-in engine methods and tenant-dispatched PaddleOCR cannot be overridden
+// by an external analyzer factory.
+func TestRegisterAnalyzerFactory_RejectsBuiltinAndTenantDispatchedMethods(t *testing.T) {
for _, m := range []string{"plain_text", "mineru", "paddleocr", "docling", "opendataloader", "somark", "tcadp"} {
mm := m
mustPanic(t, mm, func() {
diff --git a/internal/parser/parser/pdf_parser_common.go b/internal/parser/parser/pdf_parser_common.go
index be6bcc81f0..2d08a4713a 100644
--- a/internal/parser/parser/pdf_parser_common.go
+++ b/internal/parser/parser/pdf_parser_common.go
@@ -45,14 +45,12 @@ var ErrPDFEngineUnavailable = errors.New("parser: PDF backend unavailable in thi
// supportedPDFParseMethods is the set of canonical tokens PDFParser can
// execute; see pdfParseMethodSpellings for the accepted spellings and
-// TestPDFParseMethodTablesAgree for the two intentional divergences
-// ("" sentinel, dispatcher-handled monkeyocrv2).
+// TestPDFParseMethodTablesAgree for dispatcher-handled methods.
var supportedPDFParseMethods = map[string]struct{}{
"": {},
"deepdoc": {},
"plain_text": {},
"mineru": {},
- "paddleocr": {},
"docling": {},
"opendataloader": {},
"somark": {},
@@ -60,7 +58,7 @@ var supportedPDFParseMethods = map[string]struct{}{
}
type PDFParser struct {
- ParserType string // DeepDoc, PaddleOCR, MinerU
+ ParserType string // DeepDoc, MinerU
Model string // DeepDoc@buildin@ragflow
LibType string // pdf_oxide, used by DeepDoc
@@ -89,9 +87,6 @@ type PDFParser struct {
MinerUBackend string
MinerUServerURL string
MinerUPollTimeout time.Duration
- PaddleOCRBaseURL string
- PaddleOCRAPIKey string
- PaddleOCRAlgorithm string
DoclingServerURL string
DoclingAPIKey string
DoclingDoOCR *bool
@@ -130,7 +125,6 @@ func NewPDFParser() *PDFParser {
OutputFormat: "json",
MinerUBackend: "",
MinerUPollTimeout: minerUPollTimeout,
- PaddleOCRAlgorithm: "PaddleOCR-VL",
OpenDataLoaderTimeout: 600,
SoMarkBaseURL: "https://somark.cn/api/v1",
SoMarkImageFormat: "url",
@@ -189,15 +183,6 @@ func (p *PDFParser) ConfigureFromSetup(setup map[string]any) {
if v, ok := setup["output_format"].(string); ok && v != "" {
p.OutputFormat = v
}
- if v, ok := setup["paddleocr_base_url"].(string); ok && v != "" {
- p.PaddleOCRBaseURL = v
- }
- if v, ok := setup["paddleocr_api_key"].(string); ok {
- p.PaddleOCRAPIKey = v
- }
- if v, ok := setup["paddleocr_algorithm"].(string); ok && v != "" {
- p.PaddleOCRAlgorithm = v
- }
if v, ok := setup["docling_server_url"].(string); ok && v != "" {
p.DoclingServerURL = v
}
@@ -358,7 +343,7 @@ func (p *PDFParser) validateParseMethod() error {
if _, ok := supportedPDFParseMethods[method]; ok {
return nil
}
- return fmt.Errorf("parser: unsupported PDF parse_method %q (Go currently supports: deepdoc, plain_text, mineru, paddleocr, docling, opendataloader, somark, tcadp; tenant-resolved custom IMAGE2TEXT/VLM model names are not supported in the Go parser layer — methods backed by a registered external analyzer factory are supported)", p.ParseMethod)
+ return fmt.Errorf("parser: unsupported PDF parse_method %q (standalone PDFParser supports: deepdoc, plain_text, mineru, docling, opendataloader, somark, tcadp; methods backed by a registered external analyzer factory are supported; tenant-resolved custom IMAGE2TEXT/VLM model names are not supported in the parser layer)", p.ParseMethod)
}
// ParseWithResult parses a PDF into a ParseResult, dispatching on the
@@ -384,8 +369,6 @@ func (p *PDFParser) ParseWithResult(ctx context.Context, filename string, data [
return pdfItemsToResult(filename, items, p.OutputFormat, pageCount)
case "mineru":
return parsePDFWithMinerU(ctx, filename, data, p)
- case "paddleocr":
- return parsePDFWithPaddleOCR(ctx, filename, data, p)
case "docling":
return parsePDFWithDocling(ctx, filename, data, p)
case "opendataloader":
@@ -481,10 +464,11 @@ var analyzerFactories = map[string]func(baseURL string) (deepdoctype.DocAnalyzer
// deepdoctype.SetNativeDocAnalyzerFactory, but for out-of-process backends
// selected per parse method.
//
-// Two registrations are rejected (panic) because they would fail silently:
+// Reserved registrations are rejected (panic):
// - a built-in method that ParseWithResult dispatches to an internal engine
// (every switch case other than the fall-through to DeepDoc) — the switch
// intercepts it before resolution runs, so the factory would never be used;
+// - "paddleocr" — this method requires tenant model dispatch;
// - the empty (default) method — routing it externally would hijack every
// caller that omits parse_method and divert the default in-process path.
//
@@ -498,8 +482,8 @@ func RegisterAnalyzerFactory(method string, factory func(baseURL string) (deepdo
panic("parser: RegisterAnalyzerFactory: cannot register an external analyzer for the empty (default) parse method; use an explicit method name")
}
if m != "deepdoc" {
- if _, builtin := supportedPDFParseMethods[m]; builtin {
- panic(fmt.Sprintf("parser: RegisterAnalyzerFactory: cannot register an external analyzer for built-in method %q; it is dispatched to an internal engine and the factory would be ignored", m))
+ if _, builtin := supportedPDFParseMethods[m]; builtin || m == "paddleocr" {
+ panic(fmt.Sprintf("parser: RegisterAnalyzerFactory: cannot register an external analyzer for reserved method %q; it requires a built-in parser or tenant model dispatch", m))
}
}
analyzerFactories[m] = factory
diff --git a/internal/parser/parser/pdf_parser_common_test.go b/internal/parser/parser/pdf_parser_common_test.go
index 64e43eb63c..07058df973 100644
--- a/internal/parser/parser/pdf_parser_common_test.go
+++ b/internal/parser/parser/pdf_parser_common_test.go
@@ -447,11 +447,6 @@ func TestPDFParser_ValidateParseMethod(t *testing.T) {
t.Fatalf("default validateParseMethod: %v", err)
}
- p.ConfigureFromSetup(map[string]any{"parse_method": "PaddleOCR"})
- if err := p.validateParseMethod(); err != nil {
- t.Fatalf("validateParseMethod(PaddleOCR): %v", err)
- }
-
p.ConfigureFromSetup(map[string]any{"parse_method": "tenant@provider@SoMark"})
if err := p.validateParseMethod(); err != nil {
t.Fatalf("validateParseMethod(tenant@provider@SoMark): %v", err)
@@ -550,28 +545,11 @@ func TestIsPDFParseMethodLayoutSuffixes(t *testing.T) {
}
}
-// TestPDFParseMethodTablesAgree pins the relationship between the two
-// vocabulary tables in pdf_parser_common.go: every canonical token a
-// spelling maps to must be executable by PDFParser, and every executable
-// token must be reachable from some spelling. The only intended divergences
-// are:
-//
-// - "" (the unset sentinel) is accepted by the parser as the default
-// method but has no spelling, so IsPDFParseMethod("") stays false;
-// - "monkeyocrv2" is dispatched by the ingestion vision dispatcher
-// before PDFParser runs, so it is a recognized method without being
-// executable by the parser switch itself.
-//
-// Any other difference means one table was updated without the other: a
-// spelling whose canonical token is not executable dies in
-// validateParseMethod, and an executable token without a spelling is
-// misclassified as a VLM model name by Check and the dispatcher (the
-// "Plain Text" bug class).
+// TestPDFParseMethodTablesAgree verifies executable parser methods and
+// dispatcher-handled OCR methods remain recognized by the selector vocabulary.
func TestPDFParseMethodTablesAgree(t *testing.T) {
- const (
- unsetSentinel = ""
- dispatcherHandled = "monkeyocrv2"
- )
+ const unsetSentinel = ""
+ dispatcherHandled := map[string]bool{"monkeyocrv2": true, "paddleocr": true}
reachable := make(map[string]bool, len(pdfParseMethodSpellings))
for spelling, canonical := range pdfParseMethodSpellings {
if canonical == unsetSentinel {
@@ -579,12 +557,12 @@ func TestPDFParseMethodTablesAgree(t *testing.T) {
continue
}
reachable[canonical] = true
- if canonical == dispatcherHandled {
+ if dispatcherHandled[canonical] {
continue
}
if _, ok := supportedPDFParseMethods[canonical]; !ok {
- t.Errorf("spelling %q maps to canonical %q, which supportedPDFParseMethods cannot execute; add %q to that set, or handle it in the ingestion dispatcher like %q",
- spelling, canonical, canonical, dispatcherHandled)
+ t.Errorf("spelling %q maps to canonical %q, which supportedPDFParseMethods cannot execute; add %q to that set, or handle it in the ingestion dispatcher like PaddleOCR",
+ spelling, canonical, canonical)
}
}
for supported := range supportedPDFParseMethods {
@@ -593,8 +571,10 @@ func TestPDFParseMethodTablesAgree(t *testing.T) {
}
t.Errorf("supportedPDFParseMethods contains %q that no spelling maps to; without a spelling, Check() and the vision dispatcher route it to the VLM path", supported)
}
- if !IsPDFParseMethod(dispatcherHandled) {
- t.Errorf("IsPDFParseMethod(%q) = false; the ingestion dispatcher relies on it being recognized (Check() must not demand lang for it)", dispatcherHandled)
+ for method := range dispatcherHandled {
+ if !IsPDFParseMethod(method) {
+ t.Errorf("dispatcher-handled method %q must remain recognized", method)
+ }
}
}
@@ -877,3 +857,12 @@ func TestPDFParseResultToJSON_EngineNilGraceful(t *testing.T) {
t.Fatalf("JSON len = %d, want 1", len(res.JSON))
}
}
+
+func TestPDFParserPaddleOCRRequiresTenantDispatch(t *testing.T) {
+ p := NewPDFParser()
+ p.ConfigureFromSetup(map[string]any{"parse_method": "paddleocr"})
+ result := p.ParseWithResult(t.Context(), "document.pdf", []byte("%PDF-1.4"))
+ if result.Err == nil || !strings.Contains(result.Err.Error(), "unsupported PDF parse_method") {
+ t.Fatalf("standalone parser must not submit OCR jobs: %v", result.Err)
+ }
+}
diff --git a/internal/parser/parser/pdf_parser_paddleocr.go b/internal/parser/parser/pdf_parser_paddleocr.go
index 4088a8aced..fd829ecd73 100644
--- a/internal/parser/parser/pdf_parser_paddleocr.go
+++ b/internal/parser/parser/pdf_parser_paddleocr.go
@@ -3,47 +3,142 @@ package parser
import (
"context"
"fmt"
- "ragflow/internal/common"
+ "math"
"strings"
+
+ "ragflow/internal/entity"
+ "ragflow/internal/entity/models"
+ "ragflow/internal/utility"
)
-func parsePDFWithPaddleOCR(ctx context.Context, filename string, data []byte, parser *PDFParser) ParseResult {
- if len(data) == 0 {
- return emptyPDFResult(filename)
+// PaddleOCRPDFResult projects OCR regions onto the PDF's page geometry and
+// emits the structured items consumed by the existing PDF chunk pipeline.
+func PaddleOCRPDFResult(ctx context.Context, filename string, data []byte, response *models.OCRFileResponse) ParseResult {
+ if err := ctx.Err(); err != nil {
+ return ParseResult{Err: err}
+ }
+ if response == nil || len(response.Pages) == 0 {
+ return ParseResult{Err: fmt.Errorf("parser: PaddleOCR returned no page blocks")}
}
- baseURL := strings.TrimSpace(parser.PaddleOCRBaseURL)
- if baseURL == "" {
- baseURL = strings.TrimSpace(common.GetEnv(common.EnvPaddleOCRBaseUrl))
+ sizes, pageCount, geometryErr := paddleOCRPageSizes(ctx, data, response.Pages)
+ if err := ctx.Err(); err != nil {
+ return ParseResult{Err: err}
}
- if baseURL == "" {
- baseURL = strings.TrimSpace(common.GetEnv(common.EnvPaddleOCRAPIURL))
+ result := ParseResult{OutputFormat: "json", Warnings: append([]string(nil), response.Warnings...)}
+ hasPageCount := pageCount > 0
+ if geometryErr != nil {
+ result.Warnings = append(result.Warnings, "PaddleOCR PDF geometry unavailable: "+geometryErr.Error())
}
- if baseURL == "" {
- return ParseResult{Err: fmt.Errorf("parser: PaddleOCR requires paddleocr_base_url or PADDLEOCR_BASE_URL")}
+ markdown, err := NewMarkdownParser(GoMarkdown)
+ if err != nil {
+ return ParseResult{Err: err}
}
- apiKey := strings.TrimSpace(parser.PaddleOCRAPIKey)
- if apiKey == "" {
- apiKey = strings.TrimSpace(common.GetEnv(common.EnvPaddleOCRAccessToken))
+ markdown.FetchRemoteImages = false
+ for _, page := range response.Pages {
+ if !hasPageCount && page.Index+1 > pageCount {
+ pageCount = page.Index + 1
+ }
+ size := sizes[page.Index]
+ if paddleOCRPositive(page.Width) && paddleOCRPositive(page.Height) && paddleOCRPositive(size[0]) && paddleOCRPositive(size[1]) && !paddleOCRAspectRatioMatches(page, size) {
+ result.Warnings = append(result.Warnings, fmt.Sprintf("PaddleOCR page %d: OCR dimensions %gx%g do not match PDF aspect ratio %gx%g; coordinates omitted", page.Index+1, page.Width, page.Height, size[0], size[1]))
+ size = [2]float64{}
+ }
+ for _, block := range page.Blocks {
+ if err := ctx.Err(); err != nil {
+ return ParseResult{Err: err}
+ }
+ content := strings.TrimSpace(utility.StripMarkdownImages(block.Text))
+ if content == "" {
+ continue
+ }
+ parsed := markdown.ParseWithResult(ctx, filename, []byte(content+"\n\n"))
+ if parsed.Err != nil {
+ return parsed
+ }
+ result.Warnings = append(result.Warnings, parsed.Warnings...)
+ position := paddleOCRBlockPosition(page, block.BBox, size)
+ label := strings.ToLower(strings.TrimSpace(block.Label))
+ isTitle := label == "doc_title" || label == "paragraph_title"
+ if label == "figure_title" || label == "table_title" {
+ label = "caption"
+ }
+ for _, item := range parsed.JSON {
+ if text, ok := item.(TextItem); ok {
+ if strings.TrimSpace(text.Text) == "" {
+ continue
+ }
+ if isTitle && text.CKType == "text" {
+ prefix := "## "
+ if label == "doc_title" {
+ prefix = "# "
+ }
+ text.Text = prefix + text.Text
+ text.CKType = "heading"
+ item = text
+ }
+ }
+ fields := item.ToMap()
+ // Page identity is retained even when the block has no usable geometry.
+ fields["page_number"] = page.Index + 1
+ fields["_preserve_order"] = true
+ if isTitle {
+ fields["layout"] = "title"
+ } else if label != "" {
+ fields["layout"] = label
+ }
+ if len(position) > 0 {
+ matrix := [][]any{position}
+ fields["positions"], fields["_pdf_positions"] = matrix, matrix
+ }
+ if table, ok := fields["table"].(*entity.TableData); ok && table != nil {
+ table.Page = page.Index + 1
+ }
+ result.JSON = append(result.JSON, DocItemFromMap(fields))
+ }
+ }
}
- algorithm := strings.TrimSpace(parser.PaddleOCRAlgorithm)
- if algorithm == "" {
- algorithm = strings.TrimSpace(common.GetEnv(common.EnvPaddleOCRAlgorithm))
+ if err := ctx.Err(); err != nil {
+ return ParseResult{Err: err}
}
- if algorithm == "" {
- algorithm = "PaddleOCR-VL"
+ if len(result.JSON) == 0 {
+ return ParseResult{Err: fmt.Errorf("parser: PaddleOCR returned empty text")}
}
+ result.File = pdfFileMeta(filename, pageCount)
+ return result
+}
- // The async Job API, exactly like Python's PaddleOCRParser.parse_pdf():
- // submit {base}/api/v2/ocr/jobs, poll until done, download the JSONL
- // results. No synchronous local driver is involved here.
- client := newPaddleOCRClient(baseURL, apiKey, algorithm)
- text, err := client.ParsePDF(data, filename)
- if err != nil {
- return ParseResult{Err: fmt.Errorf("parser: PaddleOCR ParsePDF: %w", err)}
+func paddleOCRBlockPosition(page models.OCRPage, bbox []float64, size [2]float64) []any {
+ if page.Index < 0 || !paddleOCRPositive(page.Width) || !paddleOCRPositive(page.Height) || !paddleOCRPositive(size[0]) || !paddleOCRPositive(size[1]) {
+ return nil
+ }
+ if !paddleOCRAspectRatioMatches(page, size) || len(bbox) != 4 {
+ return nil
}
- pageCount := 1
- if strings.TrimSpace(text) == "" {
- pageCount = 0
+ for _, v := range bbox {
+ if math.IsNaN(v) || math.IsInf(v, 0) {
+ return nil
+ }
}
- return parseMinerUMarkdownResult(ctx, filename, text, parser.OutputFormat, pageCount)
+ left, top, right, bottom := bbox[0], bbox[1], bbox[2], bbox[3]
+ if right <= left || bottom <= top {
+ return nil
+ }
+ left = math.Max(0, math.Min(size[0], left*size[0]/page.Width))
+ right = math.Max(0, math.Min(size[0], right*size[0]/page.Width))
+ top = math.Max(0, math.Min(size[1], top*size[1]/page.Height))
+ bottom = math.Max(0, math.Min(size[1], bottom*size[1]/page.Height))
+ if right <= left || bottom <= top {
+ return nil
+ }
+ return []any{page.Index + 1, left, right, top, bottom}
+}
+
+// Allow 2% variation for renderer pixel rounding, but reject anisotropic scaling.
+func paddleOCRAspectRatioMatches(page models.OCRPage, size [2]float64) bool {
+ xScale, yScale := size[0]/page.Width, size[1]/page.Height
+ return math.Max(xScale, yScale) <= 1.02*math.Min(xScale, yScale)
+}
+
+func paddleOCRPositive(value float64) bool {
+ return value > 0 && !math.IsInf(value, 0) && !math.IsNaN(value)
}
diff --git a/internal/parser/parser/pdf_parser_paddleocr_cgo.go b/internal/parser/parser/pdf_parser_paddleocr_cgo.go
new file mode 100644
index 0000000000..f4ccd0f8a1
--- /dev/null
+++ b/internal/parser/parser/pdf_parser_paddleocr_cgo.go
@@ -0,0 +1,20 @@
+//go:build cgo
+
+package parser
+
+import (
+ "context"
+
+ "ragflow/internal/deepdoc/parser/pdf/pdfium"
+ "ragflow/internal/entity/models"
+)
+
+func paddleOCRPageSizes(ctx context.Context, data []byte, pages []models.OCRPage) (map[int][2]float64, int, error) {
+ indexes := make([]int, 0, len(pages))
+ for _, page := range pages {
+ if len(page.Blocks) > 0 {
+ indexes = append(indexes, page.Index)
+ }
+ }
+ return pdfium.PageSizes(ctx, data, indexes)
+}
diff --git a/internal/parser/parser/pdf_parser_paddleocr_cgo_test.go b/internal/parser/parser/pdf_parser_paddleocr_cgo_test.go
new file mode 100644
index 0000000000..cfc8e627fc
--- /dev/null
+++ b/internal/parser/parser/pdf_parser_paddleocr_cgo_test.go
@@ -0,0 +1,132 @@
+//go:build cgo
+
+package parser
+
+import (
+ "os"
+ "reflect"
+ "strings"
+ "testing"
+
+ "ragflow/internal/entity"
+ "ragflow/internal/entity/models"
+)
+
+func TestPaddleOCRPDFResultPageGeometry(t *testing.T) {
+ data, err := os.ReadFile("testdata/paddleocr_pages.pdf")
+ if err != nil {
+ t.Fatal(err)
+ }
+ text := "body"
+ response := &models.OCRFileResponse{Text: &text, Pages: []models.OCRPage{
+ {Index: 0, Width: 1191, Height: 1684, Blocks: []models.OCRBlock{{Text: "first", Label: "text", BBox: []float64{174, 146, 1015, 240}}}},
+ {Index: 1, Width: 900, Height: 1200},
+ {Index: 2, Width: 600, Height: 800, Blocks: []models.OCRBlock{{Text: "third", Label: "text", BBox: []float64{30, 40, 300, 160}}, {Text: "<table><tr><td>42</td></tr></table>", Label: "table", BBox: []float64{30, 200, 300, 400}}}},
+ }}
+ result := PaddleOCRPDFResult(t.Context(), "sample.pdf", data, response)
+ if result.Err != nil {
+ t.Fatal(result.Err)
+ }
+ if result.File["page_count"] != 3 || len(result.JSON) != 3 {
+ t.Fatalf("result=%+v", result)
+ }
+ third := result.JSON[1].ToMap()
+ if !reflect.DeepEqual(third["positions"], [][]any{{3, float64(15), float64(150), float64(20), float64(80)}}) {
+ t.Fatalf("third positions=%v", third["positions"])
+ }
+ table := result.JSON[2].ToMap()
+ // TableData must carry the same source page as the highlight boxes.
+ if table["table"].(*entity.TableData).Page != 3 {
+ t.Fatalf("table data page=%v", table["table"])
+ }
+ if table["page_number"] != 3 {
+ t.Fatalf("table page=%v", table)
+ }
+}
+
+func TestPaddleOCRPDFResultLocatesUnstructuredTableText(t *testing.T) {
+ data, err := os.ReadFile("testdata/paddleocr_pages.pdf")
+ if err != nil {
+ t.Fatal(err)
+ }
+ response := &models.OCRFileResponse{Pages: []models.OCRPage{{Index: 0, Width: 1191, Height: 1684, Blocks: []models.OCRBlock{{Text: "Item: 42", Label: "table", BBox: []float64{20, 40, 100, 120}}}}}}
+ result := PaddleOCRPDFResult(t.Context(), "sample.pdf", data, response)
+ if result.Err != nil || len(result.JSON) != 1 {
+ t.Fatalf("result=%+v", result)
+ }
+ item := result.JSON[0].ToMap()
+ if item["doc_type_kwd"] != "text" || item["text"] != "Item: 42" || item["positions"] == nil {
+ t.Fatalf("item=%v", item)
+ }
+}
+
+func TestPaddleOCRPDFResultKeepsGeometryAfterOnePageFails(t *testing.T) {
+ data, err := os.ReadFile("testdata/paddleocr_pages.pdf")
+ if err != nil {
+ t.Fatal(err)
+ }
+ block := models.OCRBlock{Label: "text", Text: "body", BBox: []float64{10, 20, 100, 200}}
+ response := &models.OCRFileResponse{Pages: []models.OCRPage{{Index: 0, Width: 1191, Height: 1684, Blocks: []models.OCRBlock{block}}, {Index: 99, Width: 1191, Height: 1684, Blocks: []models.OCRBlock{block}}}}
+ result := PaddleOCRPDFResult(t.Context(), "sample.pdf", data, response)
+ if result.Err != nil {
+ t.Fatal(result.Err)
+ }
+ if result.File["page_count"] != 3 {
+ t.Fatalf("source page count=%v", result.File["page_count"])
+ }
+ if len(result.Warnings) == 0 || result.JSON[0].ToMap()["positions"] == nil || result.JSON[1].ToMap()["positions"] != nil {
+ t.Fatalf("result=%+v", result)
+ }
+}
+
+func TestPaddleOCRPDFResultDropsMismatchedPageGeometry(t *testing.T) {
+ data, err := os.ReadFile("testdata/paddleocr_pages.pdf")
+ if err != nil {
+ t.Fatal(err)
+ }
+ for _, tc := range []struct {
+ name string
+ index int
+ width, height float64
+ located bool
+ }{
+ {"resized", 0, 1191, 1684, true},
+ {"pixel rounding", 0, 1192, 1684, true},
+ {"cropped proportions", 0, 1191, 1400, false},
+ {"post-rotation proportions", 2, 600, 800, true},
+ {"unrotated OCR", 2, 800, 600, false},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ response := &models.OCRFileResponse{Pages: []models.OCRPage{{
+ Index: tc.index, Width: tc.width, Height: tc.height,
+ Blocks: []models.OCRBlock{
+ {Text: "body", BBox: []float64{30, 40, 100, 160}},
+ {Text: "second", BBox: []float64{30, 200, 100, 240}},
+ },
+ }}}
+ result := PaddleOCRPDFResult(t.Context(), "sample.pdf", data, response)
+ if result.Err != nil || len(result.JSON) != 2 {
+ t.Fatalf("result=%+v", result)
+ }
+ for _, item := range result.JSON {
+ fields := item.ToMap()
+ if fields["page_number"] != tc.index+1 || (fields["positions"] != nil) != tc.located {
+ t.Fatalf("item=%v", fields)
+ }
+ }
+ warnings := 0
+ for _, warning := range result.Warnings {
+ if strings.Contains(warning, "aspect ratio") {
+ warnings++
+ }
+ }
+ wantWarnings := 0
+ if !tc.located {
+ wantWarnings = 1
+ }
+ if warnings != wantWarnings {
+ t.Fatalf("warnings=%v", result.Warnings)
+ }
+ })
+ }
+}
diff --git a/internal/parser/parser/pdf_parser_paddleocr_nocgo.go b/internal/parser/parser/pdf_parser_paddleocr_nocgo.go
new file mode 100644
index 0000000000..6e661d892f
--- /dev/null
+++ b/internal/parser/parser/pdf_parser_paddleocr_nocgo.go
@@ -0,0 +1,17 @@
+//go:build !cgo
+
+package parser
+
+import (
+ "context"
+ "fmt"
+
+ "ragflow/internal/entity/models"
+)
+
+func paddleOCRPageSizes(ctx context.Context, _ []byte, _ []models.OCRPage) (map[int][2]float64, int, error) {
+ if err := ctx.Err(); err != nil {
+ return nil, 0, err
+ }
+ return nil, 0, fmt.Errorf("PDF page geometry requires cgo")
+}
diff --git a/internal/parser/parser/pdf_parser_paddleocr_nocgo_test.go b/internal/parser/parser/pdf_parser_paddleocr_nocgo_test.go
new file mode 100644
index 0000000000..a4da92a8b8
--- /dev/null
+++ b/internal/parser/parser/pdf_parser_paddleocr_nocgo_test.go
@@ -0,0 +1,33 @@
+//go:build !cgo
+
+package parser
+
+import (
+ "os"
+ "strings"
+ "testing"
+
+ "ragflow/internal/entity/models"
+)
+
+func TestPaddleOCRPDFResultWithoutCGOKeepsTextAndPageIdentity(t *testing.T) {
+ data, err := os.ReadFile("testdata/paddleocr_pages.pdf")
+ if err != nil {
+ t.Fatal(err)
+ }
+ response := &models.OCRFileResponse{Pages: []models.OCRPage{{Index: 2, Width: 900, Height: 800, Blocks: []models.OCRBlock{{Text: "body", Label: "text", BBox: []float64{30, 40, 300, 160}}}}}}
+ result := PaddleOCRPDFResult(t.Context(), "sample.pdf", data, response)
+ if result.Err != nil {
+ t.Fatal(result.Err)
+ }
+ if len(result.JSON) != 1 || result.File["page_count"] != 3 {
+ t.Fatalf("result=%+v", result)
+ }
+ item := result.JSON[0].ToMap()
+ if item["text"] != "body" || item["page_number"] != 3 || item["positions"] != nil || item["_pdf_positions"] != nil {
+ t.Fatalf("degraded item=%v", item)
+ }
+ if len(result.Warnings) != 1 || !strings.Contains(result.Warnings[0], "requires cgo") {
+ t.Fatalf("warnings=%v", result.Warnings)
+ }
+}
diff --git a/internal/parser/parser/pdf_parser_paddleocr_test.go b/internal/parser/parser/pdf_parser_paddleocr_test.go
index 172c1d7c53..0530e041bc 100644
--- a/internal/parser/parser/pdf_parser_paddleocr_test.go
+++ b/internal/parser/parser/pdf_parser_paddleocr_test.go
@@ -1,145 +1,143 @@
package parser
import (
- "fmt"
- "io"
- "net/http"
- "net/http/httptest"
+ "context"
+ "math"
+ "os"
+ "reflect"
"strings"
- "sync/atomic"
"testing"
+
+ "ragflow/internal/entity/models"
)
-// paddleOCRJobServer stubs the async PaddleOCR Job API
-// (submit → poll → fetch JSONL), mirroring Python's PaddleOCRParser.
-// wantToken is the expected Authorization bearer token; empty skips the check.
-func paddleOCRJobServer(t *testing.T, called *atomic.Bool, wantToken string) *httptest.Server {
- t.Helper()
- return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- switch {
- case r.Method == http.MethodPost && r.URL.Path == "/api/v2/ocr/jobs":
- if wantToken != "" {
- if got, want := r.Header.Get("Authorization"), "Bearer "+wantToken; got != want {
- t.Errorf("Authorization = %q, want %q", got, want)
- return
- }
- }
- if err := r.ParseMultipartForm(32 << 20); err != nil {
- t.Errorf("ParseMultipartForm: %v", err)
- return
- }
- f, _, err := r.FormFile("file")
- if err != nil {
- t.Errorf("FormFile: %v", err)
- return
- }
- raw, readErr := io.ReadAll(f)
- f.Close()
- if readErr != nil {
- t.Errorf("ReadAll: %v", readErr)
- return
- }
- if got := string(raw); !strings.HasPrefix(got, "%PDF") {
- t.Errorf("uploaded file = %q, want PDF bytes", got)
- return
- }
- if got, want := r.FormValue("model"), "PaddleOCR-VL"; got != want {
- t.Errorf("model = %q, want %q", got, want)
- return
- }
- if got := r.FormValue("optionalPayload"); !strings.Contains(got, "formatBlockContent") {
- t.Errorf("optionalPayload = %q, want formatBlockContent", got)
- return
+func TestPaddleOCRBlockPosition(t *testing.T) {
+ page := models.OCRPage{Index: 2, Width: 600, Height: 800}
+ for _, tc := range []struct {
+ name string
+ box []float64
+ want []any
+ }{
+ {"rectangle", []float64{30, 40, 300, 160}, []any{3, float64(15), float64(150), float64(20), float64(80)}},
+ {"noncanonical polygon", []float64{30, 40, 300, 40, 300, 160, 30, 160}, nil},
+ {"clip", []float64{-30, -40, 1200, 1000}, []any{3, float64(0), float64(300), float64(0), float64(400)}},
+ {"zero", []float64{1, 1, 1, 2}, nil},
+ {"outside", []float64{1000, 900, 1100, 1000}, nil},
+ {"nonfinite", []float64{0, 0, math.Inf(1), 100}, nil},
+ {"missing", nil, nil},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ if got := paddleOCRBlockPosition(page, tc.box, [2]float64{300, 400}); !reflect.DeepEqual(got, tc.want) {
+ t.Fatalf("got=%v want=%v", got, tc.want)
}
- if called != nil {
- called.Store(true)
- }
- w.Header().Set("Content-Type", "application/json")
- _, _ = w.Write([]byte(`{"data":{"jobId":"job-1"}}`))
- case r.Method == http.MethodGet && r.URL.Path == "/api/v2/ocr/jobs/job-1":
- w.Header().Set("Content-Type", "application/json")
- resultURL := "http://" + r.Host + "/result"
- _, _ = fmt.Fprintf(w, `{"data":{"state":"done","resultJsonUrl":%q}}`, resultURL)
- case r.Method == http.MethodGet && r.URL.Path == "/result":
- w.Header().Set("Content-Type", "application/jsonl")
- _, _ = w.Write([]byte(`{"result":{"layoutParsingResults":[{"prunedResult":{"parsing_res_list":[{"block_content":"# Paddle Title\n\nBody paragraph.\n"}]}}]}}`))
- default:
- http.NotFound(w, r)
- }
- }))
+ })
+ }
}
-func TestPDFParser_ParseWithResult_PaddleOCRMarkdownIntegration(t *testing.T) {
- withSSRFBypass(t)
- var called atomic.Bool
- server := paddleOCRJobServer(t, &called, "paddle-secret")
- defer server.Close()
-
- pdf := NewPDFParser()
- pdf.ConfigureFromSetup(map[string]any{
- "parse_method": "PaddleOCR",
- "output_format": "markdown",
- "paddleocr_base_url": server.URL,
- "paddleocr_api_key": "paddle-secret",
- })
-
- ctx := t.Context()
- res := pdf.ParseWithResult(ctx, "sample.pdf", []byte("%PDF-1.4\nmock"))
- if res.Err != nil {
- t.Fatalf("ParseWithResult: %v", res.Err)
+func TestPaddleOCRPDFResultKeepsUnlocatedText(t *testing.T) {
+ data, err := os.ReadFile("testdata/paddleocr_pages.pdf")
+ if err != nil {
+ t.Fatal(err)
}
- if !called.Load() {
- t.Fatal("PaddleOCR server was not called")
+ response := &models.OCRFileResponse{Pages: []models.OCRPage{{Index: 0, Blocks: []models.OCRBlock{{Text: "keep me", Label: "text", BBox: []float64{10, 20, 30, 40}}, {Text: "![figure](https://example.invalid/a(1).png)", Label: "image"}}}}}
+ result := PaddleOCRPDFResult(t.Context(), "sample.pdf", data, response)
+ if result.Err != nil || len(result.JSON) != 1 {
+ t.Fatalf("result=%+v", result)
}
- if got, want := res.OutputFormat, "markdown"; got != want {
- t.Fatalf("OutputFormat = %q, want %q", got, want)
+ item := result.JSON[0].ToMap()
+ if item["text"] != "keep me" || item["positions"] != nil {
+ t.Fatalf("item=%v", item)
+ }
+}
+
+func TestPaddleOCRPDFResultKeepsUnstructuredTableText(t *testing.T) {
+ data, err := os.ReadFile("testdata/paddleocr_pages.pdf")
+ if err != nil {
+ t.Fatal(err)
}
- if got, want := res.Markdown, "# Paddle Title\n\nBody paragraph."; got != want {
- t.Fatalf("Markdown = %q, want %q", got, want)
+ response := &models.OCRFileResponse{Pages: []models.OCRPage{{Index: 0, Width: 1191, Height: 1684, Blocks: []models.OCRBlock{{Text: "Item: 42", Label: "table", BBox: []float64{20, 40, 100, 120}}}}}}
+ result := PaddleOCRPDFResult(t.Context(), "sample.pdf", data, response)
+ if result.Err != nil || len(result.JSON) != 1 {
+ t.Fatalf("result=%+v", result)
}
- if got, want := res.File["name"], "sample.pdf"; got != want {
- t.Fatalf("File.name = %v, want %v", got, want)
+ item := result.JSON[0].ToMap()
+ if item["doc_type_kwd"] != "text" || item["text"] != "Item: 42" {
+ t.Fatalf("item=%v", item)
}
}
-func TestPDFParser_ParseWithResult_PaddleOCRJSONIntegration(t *testing.T) {
- withSSRFBypass(t)
- server := paddleOCRJobServer(t, nil, "")
- defer server.Close()
-
- pdf := NewPDFParser()
- pdf.ConfigureFromSetup(map[string]any{
- "parse_method": "PaddleOCR",
- "output_format": "json",
- "paddleocr_base_url": server.URL,
- })
-
- ctx := t.Context()
- res := pdf.ParseWithResult(ctx, "sample.pdf", []byte("%PDF-1.4\nmock"))
- if res.Err != nil {
- t.Fatalf("ParseWithResult: %v", res.Err)
+func TestPaddleOCRMarkdownTable(t *testing.T) {
+ data, err := os.ReadFile("testdata/paddleocr_pages.pdf")
+ if err != nil {
+ t.Fatal(err)
}
- if got, want := res.OutputFormat, "json"; got != want {
- t.Fatalf("OutputFormat = %q, want %q", got, want)
+ response := &models.OCRFileResponse{Pages: []models.OCRPage{{Index: 0, Width: 1191, Height: 1684, Blocks: []models.OCRBlock{{Label: "table", Text: "| Item | Value |\n| --- | --- |\n| A | 42 |", BBox: []float64{10, 20, 100, 200}}}}}}
+ result := PaddleOCRPDFResult(t.Context(), "sample.pdf", data, response)
+ if result.Err != nil {
+ t.Fatal(result.Err)
}
- if len(res.JSON) == 0 {
- t.Fatal("JSON is empty; want markdown-normalized items")
- }
- if got := res.JSON[0].ToMap()["text"]; got == nil {
- t.Fatal("JSON[0].text missing")
+ if len(result.JSON) != 1 || result.JSON[0].ToMap()["doc_type_kwd"] != "table" {
+ t.Fatalf("GFM table lost structure: %v", ItemsToMaps(result.JSON))
}
}
-func TestPDFParser_ParseWithResult_PaddleOCRRequiresBaseURL(t *testing.T) {
- pdf := NewPDFParser()
- pdf.ConfigureFromSetup(map[string]any{"parse_method": "PaddleOCR"})
+func TestPaddleOCRCanceledContext(t *testing.T) {
+ data, err := os.ReadFile("testdata/paddleocr_pages.pdf")
+ if err != nil {
+ t.Fatal(err)
+ }
+ ctx, cancel := context.WithCancel(t.Context())
+ cancel()
+ response := &models.OCRFileResponse{Pages: []models.OCRPage{{Index: 0, Width: 1191, Height: 1684, Blocks: []models.OCRBlock{{Label: "text", Text: "body", BBox: []float64{10, 20, 100, 200}}}}}}
+ result := PaddleOCRPDFResult(ctx, "sample.pdf", data, response)
+ if result.Err == nil {
+ t.Fatal("canceled parse returned success")
+ }
+}
- ctx := t.Context()
- res := pdf.ParseWithResult(ctx, "sample.pdf", []byte("%PDF-1.4\nmock"))
- if res.Err == nil {
- t.Fatal("ParseWithResult: want error when paddleocr_base_url is missing, got nil")
+func TestPaddleOCRTitleLabels(t *testing.T) {
+ data, err := os.ReadFile("testdata/paddleocr_pages.pdf")
+ if err != nil {
+ t.Fatal(err)
+ }
+ for _, tc := range []struct{ label, text, want, ckType string }{
+ {"doc_title", "Document title", "# Document title", "heading"},
+ {"paragraph_title", "Section title", "## Section title", "heading"},
+ {"doc_title", "### Existing title", "### Existing title", "heading"},
+ {"figure_title", "Figure caption", "Figure caption", "text"},
+ {"table_title", "Table caption", "Table caption", "text"},
+ } {
+ t.Run(tc.label+tc.text, func(t *testing.T) {
+ response := &models.OCRFileResponse{Pages: []models.OCRPage{{Index: 0, Blocks: []models.OCRBlock{{Text: tc.text, Label: tc.label}}}}}
+ result := PaddleOCRPDFResult(t.Context(), "title.pdf", data, response)
+ if result.Err != nil {
+ t.Fatal(result.Err)
+ }
+ item := result.JSON[0].ToMap()
+ if item["ck_type"] != tc.ckType || item["text"] != tc.want {
+ t.Fatalf("title semantics: %v", item)
+ }
+ formatted := pdfItemsToResult("title.pdf", ItemsToMaps(result.JSON), "markdown", 1)
+ if formatted.Markdown != tc.want {
+ t.Fatalf("markdown=%q; want %q", formatted.Markdown, tc.want)
+ }
+ })
}
- if !strings.Contains(res.Err.Error(), "paddleocr_base_url") {
- t.Fatalf("error = %q, want paddleocr_base_url context", res.Err.Error())
+}
+
+func TestPaddleOCRPDFResultRejectsRenderedEmptyText(t *testing.T) {
+ for _, label := range []string{"text", "doc_title", "paragraph_title"} {
+ t.Run(label, func(t *testing.T) {
+ response := &models.OCRFileResponse{Pages: []models.OCRPage{{Index: 0, Blocks: []models.OCRBlock{{Label: label, Text: "---"}}}}}
+ result := PaddleOCRPDFResult(t.Context(), "separator.pdf", nil, response)
+ if result.Err == nil || !strings.Contains(result.Err.Error(), "empty text") {
+ t.Fatalf("rendered-empty document accepted: %+v", result)
+ }
+ response.Pages[0].Blocks = append(response.Pages[0].Blocks, models.OCRBlock{Text: "body"})
+ result = PaddleOCRPDFResult(t.Context(), "mixed.pdf", nil, response)
+ if result.Err != nil || len(result.JSON) != 1 || result.JSON[0].ToMap()["text"] != "body" {
+ t.Fatalf("empty placeholder survived beside body: %+v", result)
+ }
+ })
}
}
diff --git a/internal/parser/parser/testdata/paddleocr_pages.pdf b/internal/parser/parser/testdata/paddleocr_pages.pdf
new file mode 100644
index 0000000000..553eef427e
--- /dev/null
+++ b/internal/parser/parser/testdata/paddleocr_pages.pdf
@@ -0,0 +1,29 @@
+%PDF-1.4
+1 0 obj
+<< /Type /Catalog /Pages 2 0 R >>
+endobj
+2 0 obj
+<< /Type /Pages /Kids [3 0 R 4 0 R 5 0 R] /Count 3 >>
+endobj
+3 0 obj
+<< /Type /Page /Parent 2 0 R /MediaBox [0 0 595.5 842] >>
+endobj
+4 0 obj
+<< /Type /Page /Parent 2 0 R /MediaBox [0 0 300 400] >>
+endobj
+5 0 obj
+<< /Type /Page /Parent 2 0 R /MediaBox [0 0 400 300] /Rotate 90 >>
+endobj
+xref
+0 6
+0000000000 65535 f
+0000000009 00000 n
+0000000058 00000 n
+0000000127 00000 n
+0000000200 00000 n
+0000000271 00000 n
+trailer
+<< /Size 6 /Root 1 0 R >>
+startxref
+353
+%%EOF
diff --git a/internal/utility/markdown_images.go b/internal/utility/markdown_images.go
new file mode 100644
index 0000000000..9513ff4ddf
--- /dev/null
+++ b/internal/utility/markdown_images.go
@@ -0,0 +1,11 @@
+package utility
+
+import "regexp"
+
+var markdownImageMarkup = regexp.MustCompile(`(?is)!\[[^\]]*\]\(([^()]|\([^()]*\))*\)|<div[^>]*>\s*<img[^>]*>\s*</div>|<img[^>]*>`)
+
+// StripMarkdownImages removes inline Markdown images and HTML image markup.
+// Inline destinations may contain one level of balanced parentheses.
+func StripMarkdownImages(text string) string {
+ return markdownImageMarkup.ReplaceAllString(text, "")
+}
diff --git a/internal/utility/markdown_images_test.go b/internal/utility/markdown_images_test.go
new file mode 100644
index 0000000000..813847ca0e
--- /dev/null
+++ b/internal/utility/markdown_images_test.go
@@ -0,0 +1,23 @@
+package utility
+
+import "testing"
+
+func TestStripMarkdownImages(t *testing.T) {
+ for _, tc := range []struct {
+ name, text, want string
+ }{
+ {"plain image", "![a](https://example.invalid/a.png)", ""},
+ {"parenthesized filename", "![a](http://x/img_(1).png)", ""},
+ {"multiple parentheses", "![a](http://x/(folder)/img_(1).png)", ""},
+ {"surrounding text", "before ![a](http://x/img_(1).png) after", "before after"},
+ {"multiple images", "![a](a(1).png) ![b](b(2).png)", " "},
+ {"html image", `<div><img src="a(1).png"></div>`, ""},
+ {"regular link", "[a](http://x/img_(1).png)", "[a](http://x/img_(1).png)"},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ if got := StripMarkdownImages(tc.text); got != tc.want {
+ t.Fatalf("got=%q want=%q", got, tc.want)
+ }
+ })
+ }
+}