hect0x7 / JMComic-Crawler-Python

Python API for JMComic | 提供Python API访问禁漫天堂,同时支持网页端和移动端 | 禁漫天堂GitHub Actions下载器🚀
https://jmcomic.readthedocs.io/zh-cn/latest/option_file_syntax/#
MIT License
557 stars 1.18k forks source link

希望能添加一个download_album_for_pdf函数直接下载pdf文件 #227

Closed Guoziqi329 closed 2 months ago

Guoziqi329 commented 2 months ago

通过此程序下载的图片是webp格式的,这使得一些设备查看起来非常麻烦,比如说win7电脑,我希望能添加一个函数不仅能下载webp文件,还能直接把所有图片转成一个pdf文档,这样在老设备上查看跟简单一些。以下是我写过的一个webp转pdf的函数,您可以参考一下:

import os
import img2pdf

def convert_webp_to_pdf(folder_path, output_filename):
    """
    Converts all WebP files in the specified folder to a single PDF file.

    Args:
        folder_path (str): Path to the folder containing WebP files.
        output_filename (str): Name of the output PDF file.

    Returns:
        None
    """
    webp_files = [file for file in os.listdir(folder_path) if file.lower().endswith(".webp")]

    if not webp_files:
        print("No WebP files found in the specified folder.")
        return

    images = [os.path.join(folder_path, file) for file in webp_files]

    try:
        with open(output_filename, "wb") as pdf_file:
            pdf_content = img2pdf.convert(images)
            pdf_file.write(pdf_content)
        print(f"Conversion successful! Output saved as {output_filename}")
    except Exception as e:
        print(f"Error during conversion: {e}")
hect0x7 commented 2 months ago

感谢你的建议,我的初步想法,不太会加这么一个函数,而是通过插件的形式来支持用户的功能,这样做可扩展性、灵活性、可配置性都是最好的。 但说回你的需求,通过此程序下载的图片是webp格式的 这是由于禁漫目前提供的图片是webp的,所以默认下载的就是webp格式。但你可以通过option配置轻松转为其他格式,比如jpg, png,这一点请你参考option配置文档。 然后,关于转为pdf文件这一点,你提供的img2pdf库应该会很有用。你也可以先尝试一下目前jmcomic已内置的j2p插件,看看能否解决你的需求。

hect0x7 commented 2 months ago

非常感谢你提供的img2pdf库,我上手了下,非常好用👍 但正如我上面说得,我将通过插件来支持这一功能,而非 download_album_for_pdf 函数。

下面是插件配置:

plugins:
  after_photo:
    # 把章节的所有图片合并为一个pdf的插件
    # 使用前需要安装依赖库: [pip install img2pdf]
    - plugin: img2pdf
      kwargs:
        pdf_dir: D:/pdf/ # pdf存放文件夹
        filename_rule: Pid # pdf命名规则

希望你满意此插件