pdf: white 9pt document-style print CSS, download named after report title; ui tweaks

This commit is contained in:
Stefano Zini
2026-11-05 17:16:46 +01:00
parent bc98e0d004
commit da309ce47f
2 changed files with 21 additions and 5 deletions
+16 -3
View File
@@ -21,7 +21,7 @@ import time
import urllib.request
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from pathlib import Path
from urllib.parse import parse_qs, urlparse
from urllib.parse import parse_qs, quote, urlparse
# Sibling modules: db (SQLite, paths), llama (model servers), memory (Q&A).
import db
@@ -54,7 +54,16 @@ NOPROXY = urllib.request.build_opener(urllib.request.ProxyHandler({}))
# Edge path; %ProgramFiles(x86)% expanded by Windows env.
EDGE = Path(os.path.expandvars(r"%ProgramFiles(x86)%\Microsoft\Edge\Application\msedge.exe"))
PRINT_CSS = (
"@media print{body{max-width:none;padding:0}img,tr{break-inside:avoid}h2,h3{break-after:avoid}}"
"@page{margin:15mm}"
"@media print{:root{--bg:#fff;--card:#fff;--soft:#f3f3f3;--line:#ccc}html,body{background:#fff}"
"body{max-width:none;padding:0;font-size:9pt;line-height:1.5}"
"h1{font-size:16pt}h2{font-size:12pt;margin-top:1.6em;border-top:0;padding-top:0}"
"table{font-size:8.5pt;border-radius:0;border:0;border-top:1px solid #999;border-bottom:1px solid #999}"
"td,th{padding:4px 8px}th{background:#eee}"
"#in-breve+ul{border-radius:0;padding:10px 16px 10px 2em}"
"details{border:0;padding:0;margin-top:1em;background:none}summary{display:none}"
"img{border-radius:0;max-width:100%}code{border-radius:0}"
"img,tr{break-inside:avoid}h2,h3{break-after:avoid}}"
)
# Serializes ask_once: one llama start/stop at a time.
LOCK = threading.Lock()
@@ -876,10 +885,14 @@ class Handler(BaseHTTPRequestHandler):
if file_path is None:
self._send(b"not found", 404)
else:
# File name from the report <title>; filename* carries the UTF-8 form, filename the ASCII fallback.
match = re.search(r"<title>(.*?)</title>", file_path.with_name("report.html").read_text(encoding="utf-8")[:2000])
label = re.sub(r'[\\/:*?"<>|\s]+', " ", match[1] if match else name).strip() or name
ascii_label = label.encode("ascii", "ignore").decode().strip() or name
self._send(
file_path.read_bytes(),
ctype="application/pdf",
headers=[("Content-Disposition", f'attachment; filename="{name}.pdf"')],
headers=[("Content-Disposition", f'attachment; filename="{ascii_label}.pdf"; filename*=UTF-8\'\'{quote(label + ".pdf")}')],
)
elif parsed.path == "/ask":
if not params.get("q", "").strip():