S-09 PDF 输出兼容

这个提交存在于:
Codex 2026-07-07 00:28:06 +08:00
父节点 7d02e94e5f
当前提交 eb3ad2b630
共有 6 个文件被更改,包括 66 次插入 和 8 次删除

查看文件

@ -25,6 +25,8 @@ CBZ 内部会包含:
- `ComicInfo.xml`:标题、系列、页数、JM ID、来源链接等 Komga 可读取的元数据。 - `ComicInfo.xml`:标题、系列、页数、JM ID、来源链接等 Komga 可读取的元数据。
- `0001.jpg`、`0002.jpg` ...:按下载顺序编号的页面图片。 - `0001.jpg`、`0002.jpg` ...:按下载顺序编号的页面图片。
当 `archive_format` 设置为 `pdf` 时,插件会按 `pdf_chunk_size` 分块生成临时 PDF,再合并为最终文件,避免大本子转换时一次性占用过多内存。
## Komga 联动 ## Komga 联动
在 AstrBot 插件配置里填写: 在 AstrBot 插件配置里填写:

查看文件

@ -10,6 +10,11 @@
"default": "cbz", "default": "cbz",
"options": ["cbz", "pdf"] "options": ["cbz", "pdf"]
}, },
"pdf_chunk_size": {
"description": "生成 PDF 时每个临时 PDF 分块包含的图片数量,降低大本子转换时的内存占用。",
"type": "int",
"default": 80
},
"admin_only": { "admin_only": {
"description": "仅允许 AstrBot 管理员执行下载和扫描命令。", "description": "仅允许 AstrBot 管理员执行下载和扫描命令。",
"type": "bool", "type": "bool",

查看文件

@ -87,6 +87,7 @@ class JMDownloadPlugin(Star):
"JMDownload 状态", "JMDownload 状态",
f"归档目录:{self.settings.archive_root}", f"归档目录:{self.settings.archive_root}",
f"归档格式:{self.settings.archive_format}", f"归档格式:{self.settings.archive_format}",
f"PDF 分块大小:{self.settings.pdf_chunk_size}",
f"并发任务数:{self.settings.max_concurrent_tasks}", f"并发任务数:{self.settings.max_concurrent_tasks}",
f"下载重试次数:{self.settings.download_retry_count}", f"下载重试次数:{self.settings.download_retry_count}",
f"进度提示间隔:{self.settings.progress_interval_seconds} 秒", f"进度提示间隔:{self.settings.progress_interval_seconds} 秒",

查看文件

@ -2,3 +2,4 @@ jmcomic>=0.3.0
PyYAML>=6.0 PyYAML>=6.0
Pillow>=9.0.0 Pillow>=9.0.0
httpx>=0.27.0 httpx>=0.27.0
PyPDF2>=2.0.0

查看文件

@ -2,6 +2,7 @@ from __future__ import annotations
import asyncio import asyncio
import os import os
import tempfile
import zipfile import zipfile
from dataclasses import dataclass from dataclasses import dataclass
from datetime import datetime from datetime import datetime
@ -94,17 +95,38 @@ class ArchiveService:
archive.write(image_path, page_name) archive.write(image_path, page_name)
def _create_pdf(self, target_path: Path, downloaded: DownloadedComic) -> None: def _create_pdf(self, target_path: Path, downloaded: DownloadedComic) -> None:
images: list[Image.Image] = [] if not downloaded.image_files:
try:
for image_path in downloaded.image_files:
image = Image.open(image_path)
if image.mode != "RGB":
image = image.convert("RGB")
images.append(image)
if not images:
raise ArchiveError("no images available for PDF creation") 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 image_files:
images.append(self._open_pdf_image(image_path))
if not images:
raise ArchiveError("no valid images available for PDF chunk")
images[0].save( images[0].save(
target_path, target_path,
"PDF", "PDF",
@ -115,6 +137,31 @@ class ArchiveService:
for image in images: for image in images:
image.close() 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( def _comic_info_xml(
self, self,
downloaded: DownloadedComic, downloaded: DownloadedComic,

查看文件

@ -51,6 +51,7 @@ class PluginSettings:
work_root: Path work_root: Path
archive_root: Path archive_root: Path
archive_format: str = "cbz" archive_format: str = "cbz"
pdf_chunk_size: int = 80
admin_only: bool = True admin_only: bool = True
send_archive_file: bool = False send_archive_file: bool = False
overwrite_existing: bool = False overwrite_existing: bool = False
@ -89,6 +90,7 @@ class PluginSettings:
work_root=data_dir / "work", work_root=data_dir / "work",
archive_root=archive_root, archive_root=archive_root,
archive_format=archive_format, 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)), admin_only=bool(config.get("admin_only", True)),
send_archive_file=bool(config.get("send_archive_file", False)), send_archive_file=bool(config.get("send_archive_file", False)),
overwrite_existing=bool(config.get("overwrite_existing", False)), overwrite_existing=bool(config.get("overwrite_existing", False)),