Remove temporary replica render debug logging
This commit is contained in:
parent
ea314d733d
commit
b27332e530
|
|
@ -1195,166 +1195,3 @@ def _render_replica_pdf_from_layout(
|
|||
if (isinstance(w.get("manual_flags"), dict) and w.get("manual_flags", {}).get("style_edited"))
|
||||
or str(w.get("text_color_guess") or "#000000").lower() != "#000000"
|
||||
]
|
||||
if edited_words:
|
||||
print(
|
||||
"[replica-render-debug]",
|
||||
"page=", page_num,
|
||||
"prefer_word_entries=", page_layout.get("prefer_word_entries"),
|
||||
"edited_words=",
|
||||
[
|
||||
(
|
||||
w.get("id"),
|
||||
w.get("text"),
|
||||
w.get("font_size_guess"),
|
||||
w.get("font_family_guess"),
|
||||
w.get("text_color_guess"),
|
||||
w.get("manual_flags"),
|
||||
)
|
||||
for w in edited_words[:20]
|
||||
],
|
||||
flush=True,
|
||||
)
|
||||
|
||||
render_entries = []
|
||||
if page_layout.get("prefer_word_entries") and page_layout.get("words"):
|
||||
render_entries = _build_word_entries_for_page(page_layout, page_h)
|
||||
if not render_entries and page_layout.get("lines"):
|
||||
render_entries = _build_line_entries_for_page(page_layout, page_h)
|
||||
if not render_entries and page_layout.get("words"):
|
||||
render_entries = _build_word_entries_for_page(page_layout, page_h)
|
||||
if not render_entries:
|
||||
render_entries = _page_layout_line_entries(page_layout)
|
||||
|
||||
for line in render_entries:
|
||||
text_line = (line.get("text") or "").strip()
|
||||
if not text_line:
|
||||
continue
|
||||
|
||||
text_obj = c.beginText()
|
||||
if mode == "scan_backed":
|
||||
text_obj.setTextRenderMode(3)
|
||||
else:
|
||||
text_obj.setTextRenderMode(0)
|
||||
|
||||
font_size = float(line.get("font_size_guess") or 10)
|
||||
font_name = _safe_pdf_font_name(line.get("font_family_guess") or "Helvetica")
|
||||
text_obj.setFont(font_name, font_size)
|
||||
|
||||
horizontal_scale = float(line.get("horizontal_scale") or 100.0)
|
||||
if horizontal_scale != 100.0:
|
||||
text_obj.setHorizScale(horizontal_scale)
|
||||
|
||||
text_obj.setTextOrigin(float(line["pdf_x"]), float(line["pdf_y"]))
|
||||
|
||||
if mode == "debug_overlay":
|
||||
c.setStrokeColorRGB(1, 0, 0)
|
||||
c.setFillColorRGB(1, 0, 0)
|
||||
else:
|
||||
color = str(line.get("text_color_guess") or "#000000").lstrip("#")
|
||||
try:
|
||||
if len(color) == 6:
|
||||
r = int(color[0:2], 16) / 255.0
|
||||
g = int(color[2:4], 16) / 255.0
|
||||
b = int(color[4:6], 16) / 255.0
|
||||
else:
|
||||
r = g = b = 0
|
||||
except Exception:
|
||||
r = g = b = 0
|
||||
c.setStrokeColorRGB(r, g, b)
|
||||
c.setFillColorRGB(r, g, b)
|
||||
|
||||
text_obj.textLine(text_line)
|
||||
c.drawText(text_obj)
|
||||
|
||||
if mode == "debug_overlay":
|
||||
bbox = line.get("bbox_source")
|
||||
if bbox and isinstance(bbox, (list, tuple)) and len(bbox) == 4:
|
||||
try:
|
||||
left, top, right, bottom = [float(v) for v in bbox]
|
||||
c.setStrokeColorRGB(1, 0, 0)
|
||||
c.setLineWidth(0.4)
|
||||
c.rect(left, page_h - bottom, max(0.5, right - left), max(0.5, bottom - top), stroke=1, fill=0)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
c.showPage()
|
||||
|
||||
if c is None:
|
||||
raise ValueError("Failed to build replica PDF")
|
||||
|
||||
c.save()
|
||||
shutil.copy2(overlay_pdf_path, out_path)
|
||||
|
||||
compress_pdf_with_ghostscript(out_path)
|
||||
|
||||
|
||||
def save_replica_pdf(db: Session, document: Document, output_path: Path, mode: str) -> None:
|
||||
if mode not in {"clean", "scan_backed", "debug_overlay"}:
|
||||
raise ValueError(f"Unsupported replica mode: {mode}")
|
||||
|
||||
current_file, _, _, _, _ = _get_replica_source_context(document)
|
||||
out_path = Path(output_path)
|
||||
out_path = out_path.with_name(re.sub(r"_v\d+(?=\.[^.]+$)", "", out_path.name))
|
||||
|
||||
stem = re.sub(r"(_replica_clean|_replica_scan_backed)$", "", out_path.stem)
|
||||
suffix = out_path.suffix or ".pdf"
|
||||
|
||||
if mode == "clean":
|
||||
out_path = out_path.with_name(f"{stem}_replica_clean{suffix}")
|
||||
elif mode == "scan_backed":
|
||||
out_path = out_path.with_name(f"{stem}_replica_scan_backed{suffix}")
|
||||
else:
|
||||
out_path = out_path.with_name(f"{stem}_replica_debug_overlay{suffix}")
|
||||
|
||||
out_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
requested_mode = mode
|
||||
actual_mode = mode
|
||||
|
||||
layout_json = build_replica_layout(document, mode=mode)
|
||||
|
||||
page_lines = []
|
||||
for page in (layout_json.get("pages") or []):
|
||||
page_lines.extend(page.get("lines") or [])
|
||||
|
||||
if mode == "clean" and not page_lines:
|
||||
raise ValueError("clean_replica_has_no_renderable_lines")
|
||||
if mode == "clean":
|
||||
has_text = False
|
||||
for page in layout_json.get("pages", []):
|
||||
if page.get("lines"):
|
||||
has_text = True
|
||||
break
|
||||
if not has_text:
|
||||
actual_mode = "scan_backed"
|
||||
out_path = out_path.with_name(f"{stem}_replica_scan_backed{suffix}")
|
||||
layout_json = build_replica_layout(document, mode="scan_backed")
|
||||
|
||||
layout_version = _save_replica_layout_version(db, document, layout_json, mode=actual_mode)
|
||||
|
||||
_render_replica_pdf_from_layout(current_file, layout_json, out_path, mode=actual_mode)
|
||||
|
||||
file_hash = sha256_for_file(out_path)
|
||||
file_size = out_path.stat().st_size
|
||||
|
||||
try:
|
||||
mirror_path = _mirror_to_secondary_owner(document, out_path)
|
||||
share_path_value = str(mirror_path) if mirror_path else None
|
||||
except Exception:
|
||||
share_path_value = None
|
||||
|
||||
output = DocumentReplicaOutput(
|
||||
document_id=document.id,
|
||||
replica_layout_version_id=layout_version.id,
|
||||
output_type=actual_mode,
|
||||
file_path=str(out_path),
|
||||
sha256=file_hash,
|
||||
file_size_bytes=file_size,
|
||||
created_by="save_replica_pdf",
|
||||
render_settings_json={"requested_mode": requested_mode, "actual_mode": actual_mode},
|
||||
)
|
||||
db.add(output)
|
||||
|
||||
# Replica outputs are non-destructive exports.
|
||||
# Do not replace the primary/current document path or prune sibling files.
|
||||
db.commit()
|
||||
|
|
|
|||
Loading…
Reference in New Issue