any-file to PDF (Phase 1): converters/ dispatcher, images/text/md/csv/json/xml/svg/pdf-passthrough, 112 tests green
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
"""JSON -> PDF (pretty-printed, monospace).
|
||||
|
||||
Validation is explicit (a JSON error is a *job* error with a clear message,
|
||||
not a render failure): whitespace-only input -> "empty JSON file: ...",
|
||||
unparseable input -> "Invalid JSON: <reason>". Valid documents are
|
||||
pretty-printed (indent=2, original key order) into <pre> with PRE_CSS so
|
||||
structure and indentation survive the render.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
from . import common
|
||||
from .text_converter import decode_bytes
|
||||
|
||||
|
||||
def convert(path: Path, job_id: str, progress_cb=None, output_dir=None,
|
||||
source_name=None) -> str:
|
||||
"""Render a JSON file to a pretty-printed monospace PDF."""
|
||||
raw = path.read_bytes()
|
||||
if not raw.strip():
|
||||
raise ValueError("empty JSON file: nothing to convert")
|
||||
text = decode_bytes(raw)
|
||||
try:
|
||||
obj = json.loads(text)
|
||||
except json.JSONDecodeError as exc:
|
||||
raise ValueError(f"Invalid JSON: {exc}") from exc
|
||||
pretty = json.dumps(obj, indent=2, ensure_ascii=False)
|
||||
|
||||
def _build():
|
||||
return f"<html><body><pre>{common.escape(pretty)}</pre></body></html>", path.name
|
||||
|
||||
return common.convert_single(
|
||||
path, job_id, build_html=_build, css=common.PRE_CSS,
|
||||
progress_cb=progress_cb, output_dir=output_dir,
|
||||
name_src=source_name or path.name,
|
||||
)
|
||||
Reference in New Issue
Block a user