fix: fix various issues before merging
This commit is contained in:
@@ -2,23 +2,23 @@
|
||||
PDF 生成服务
|
||||
"""
|
||||
import logging
|
||||
import re
|
||||
from io import BytesIO
|
||||
from typing import List
|
||||
|
||||
import httpx
|
||||
from PIL import Image
|
||||
from reportlab.lib.pagesizes import A4
|
||||
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
||||
from reportlab.lib.units import inch
|
||||
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, PageBreak, Image as ReportLabImage
|
||||
from reportlab.pdfbase import pdfmetrics
|
||||
from reportlab.pdfbase.cidfonts import UnicodeCIDFont
|
||||
from io import BytesIO
|
||||
from services.memoir_images.parser import PLACEHOLDER_RE
|
||||
from services.memoir_images.schema import IMAGE_STATUS_COMPLETED, normalize_image_assets
|
||||
from services.memoir_images.storage import TencentCosStorageService, resolve_image_storage_key
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
PLACEHOLDER_RE = re.compile(r"\{\{\{\{IMAGE:.*?\}\}\}\}|\{\{IMAGE:.*?\}\}", re.DOTALL)
|
||||
|
||||
|
||||
def strip_image_placeholders(text: str) -> str:
|
||||
return PLACEHOLDER_RE.sub("", text or "").strip()
|
||||
@@ -35,7 +35,7 @@ def split_content_blocks(content: str, images: list[dict]) -> list[dict]:
|
||||
cleaned_before = strip_image_placeholders(before)
|
||||
if cleaned_before:
|
||||
blocks.append({"type": "text", "value": cleaned_before})
|
||||
if image.get("status") == "completed" and image.get("url"):
|
||||
if image.get("status") == IMAGE_STATUS_COMPLETED and image.get("url"):
|
||||
blocks.append({"type": "image", "url": image["url"]})
|
||||
cleaned_remaining = strip_image_placeholders(remaining)
|
||||
if cleaned_remaining:
|
||||
@@ -47,10 +47,10 @@ def _prepare_pdf_image_assets(images: list[dict]) -> list[dict]:
|
||||
storage = TencentCosStorageService.from_env()
|
||||
prepared_assets: list[dict] = []
|
||||
|
||||
for item in images or []:
|
||||
for item in normalize_image_assets(images):
|
||||
asset = dict(item)
|
||||
storage_key = resolve_image_storage_key(asset)
|
||||
if asset.get("status") == "completed" and storage_key:
|
||||
if asset.get("status") == IMAGE_STATUS_COMPLETED and storage_key:
|
||||
try:
|
||||
asset["url"] = storage.get_download_url(storage_key)
|
||||
except Exception as exc:
|
||||
@@ -60,6 +60,16 @@ def _prepare_pdf_image_assets(images: list[dict]) -> list[dict]:
|
||||
return prepared_assets
|
||||
|
||||
|
||||
def _fit_image_size(image_bytes: bytes, max_width: float, max_height: float) -> tuple[float, float]:
|
||||
with Image.open(BytesIO(image_bytes)) as image:
|
||||
width, height = image.size
|
||||
if width <= 0 or height <= 0:
|
||||
return max_width, max_height
|
||||
|
||||
scale = min(max_width / width, max_height / height)
|
||||
return width * scale, height * scale
|
||||
|
||||
|
||||
class PDFService:
|
||||
"""PDF 生成服务"""
|
||||
|
||||
@@ -140,7 +150,12 @@ class PDFService:
|
||||
image_bytes = await self._fetch_image_bytes(block["url"])
|
||||
if image_bytes:
|
||||
try:
|
||||
img = ReportLabImage(BytesIO(image_bytes), width=5 * inch, height=3.75 * inch)
|
||||
width, height = _fit_image_size(
|
||||
image_bytes,
|
||||
max_width=5 * inch,
|
||||
max_height=3.75 * inch,
|
||||
)
|
||||
img = ReportLabImage(BytesIO(image_bytes), width=width, height=height)
|
||||
story.append(img)
|
||||
story.append(Spacer(1, 0.2 * inch))
|
||||
except Exception as exc:
|
||||
|
||||
Reference in New Issue
Block a user