From eb3ad2b6304f0f54a240b54ec09abd775f731dc2 Mon Sep 17 00:00:00 2001 From: Codex Date: Tue, 7 Jul 2026 00:28:06 +0800 Subject: [PATCH] =?UTF-8?q?S-09=20PDF=20=E8=BE=93=E5=87=BA=E5=85=BC?= =?UTF-8?q?=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 ++ _conf_schema.json | 5 ++++ main.py | 1 + requirements.txt | 1 + services/archive.py | 63 ++++++++++++++++++++++++++++++++++++++------ services/settings.py | 2 ++ 6 files changed, 66 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 129e72f..5e3ca69 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,8 @@ CBZ 内部会包含: - `ComicInfo.xml`:标题、系列、页数、JM ID、来源链接等 Komga 可读取的元数据。 - `0001.jpg`、`0002.jpg` ...:按下载顺序编号的页面图片。 +当 `archive_format` 设置为 `pdf` 时,插件会按 `pdf_chunk_size` 分块生成临时 PDF,再合并为最终文件,避免大本子转换时一次性占用过多内存。 + ## Komga 联动 在 AstrBot 插件配置里填写: diff --git a/_conf_schema.json b/_conf_schema.json index 39e8311..b7df1c1 100644 --- a/_conf_schema.json +++ b/_conf_schema.json @@ -10,6 +10,11 @@ "default": "cbz", "options": ["cbz", "pdf"] }, + "pdf_chunk_size": { + "description": "生成 PDF 时每个临时 PDF 分块包含的图片数量,降低大本子转换时的内存占用。", + "type": "int", + "default": 80 + }, "admin_only": { "description": "仅允许 AstrBot 管理员执行下载和扫描命令。", "type": "bool", diff --git a/main.py b/main.py index 21c7439..e12adbd 100644 --- a/main.py +++ b/main.py @@ -87,6 +87,7 @@ class JMDownloadPlugin(Star): "JMDownload 状态", f"归档目录:{self.settings.archive_root}", f"归档格式:{self.settings.archive_format}", + f"PDF 分块大小:{self.settings.pdf_chunk_size}", f"并发任务数:{self.settings.max_concurrent_tasks}", f"下载重试次数:{self.settings.download_retry_count}", f"进度提示间隔:{self.settings.progress_interval_seconds} 秒", diff --git a/requirements.txt b/requirements.txt index 0dedd40..f336437 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,4 @@ jmcomic>=0.3.0 PyYAML>=6.0 Pillow>=9.0.0 httpx>=0.27.0 +PyPDF2>=2.0.0 diff --git a/services/archive.py b/services/archive.py index 9583777..185306f 100644 --- a/services/archive.py +++ b/services/archive.py @@ -2,6 +2,7 @@ from __future__ import annotations import asyncio import os +import tempfile import zipfile from dataclasses import dataclass from datetime import datetime @@ -94,17 +95,38 @@ class ArchiveService: archive.write(image_path, page_name) def _create_pdf(self, target_path: Path, downloaded: DownloadedComic) -> None: + if not downloaded.image_files: + raise ArchiveError("no images available for PDF creation") + + with tempfile.TemporaryDirectory( + prefix=".jm_pdf_", + dir=str(target_path.parent), + ) as temp_dir: + temp_dir_path = Path(temp_dir) + temp_pdfs = self._create_pdf_chunks(downloaded.image_files, temp_dir_path) + self._merge_pdf_chunks(temp_pdfs, target_path) + + def _create_pdf_chunks( + self, + image_files: list[Path], + temp_dir: Path, + ) -> list[Path]: + temp_pdfs: list[Path] = [] + chunk_size = self.settings.pdf_chunk_size + for chunk_index, start in enumerate(range(0, len(image_files), chunk_size), start=1): + chunk_paths = image_files[start : start + chunk_size] + chunk_pdf = temp_dir / f"part-{chunk_index:04d}.pdf" + self._write_pdf_chunk(chunk_pdf, chunk_paths) + temp_pdfs.append(chunk_pdf) + return temp_pdfs + + def _write_pdf_chunk(self, target_path: Path, image_files: list[Path]) -> None: images: list[Image.Image] = [] try: - for image_path in downloaded.image_files: - image = Image.open(image_path) - if image.mode != "RGB": - image = image.convert("RGB") - images.append(image) - + for image_path in image_files: + images.append(self._open_pdf_image(image_path)) if not images: - raise ArchiveError("no images available for PDF creation") - + raise ArchiveError("no valid images available for PDF chunk") images[0].save( target_path, "PDF", @@ -115,6 +137,31 @@ class ArchiveService: for image in images: image.close() + def _open_pdf_image(self, image_path: Path) -> Image.Image: + with Image.open(image_path) as image: + if image.mode != "RGB": + image = image.convert("RGB") + else: + image = image.copy() + return image + + def _merge_pdf_chunks(self, temp_pdfs: list[Path], target_path: Path) -> None: + if not temp_pdfs: + raise ArchiveError("no PDF chunks were generated") + + try: + from PyPDF2 import PdfMerger + except ImportError as exc: + raise ArchiveError("PyPDF2 is not installed; check plugin requirements.txt") from exc + + merger = PdfMerger() + try: + for temp_pdf in temp_pdfs: + merger.append(str(temp_pdf)) + merger.write(str(target_path)) + finally: + merger.close() + def _comic_info_xml( self, downloaded: DownloadedComic, diff --git a/services/settings.py b/services/settings.py index ab03cfe..1313a95 100644 --- a/services/settings.py +++ b/services/settings.py @@ -51,6 +51,7 @@ class PluginSettings: work_root: Path archive_root: Path archive_format: str = "cbz" + pdf_chunk_size: int = 80 admin_only: bool = True send_archive_file: bool = False overwrite_existing: bool = False @@ -89,6 +90,7 @@ class PluginSettings: work_root=data_dir / "work", archive_root=archive_root, archive_format=archive_format, + pdf_chunk_size=max(1, int(config.get("pdf_chunk_size", 80) or 80)), admin_only=bool(config.get("admin_only", True)), send_archive_file=bool(config.get("send_archive_file", False)), overwrite_existing=bool(config.get("overwrite_existing", False)),