From c33c773fe7fda1cdf95a49d61975a2bf872a74b8 Mon Sep 17 00:00:00 2001 From: TheOnlyWayUp Date: Sat, 7 Dec 2024 05:59:17 +0000 Subject: [PATCH] feat(api): Include high-res cover with EPUB and PDF Downloads --- src/api/src/create_book.py | 18 ++++++++++++++++++ src/api/src/main.py | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/api/src/create_book.py b/src/api/src/create_book.py index 16fe1c5..1bab9c5 100644 --- a/src/api/src/create_book.py +++ b/src/api/src/create_book.py @@ -418,6 +418,7 @@ class PDFGenerator: def __init__(self, data: Story, cover: bytes): self.data = data self.file = tempfile.NamedTemporaryFile(suffix=".pdf", delete=True) + self.cover = cover # self.canvas = Canvas(self.file) async def add_chapters(self, contents: List[str], download_images: bool = False): @@ -460,9 +461,26 @@ class PDFGenerator: yield part["title"] + cover_file = tempfile.NamedTemporaryFile(suffix=".html") + cover_file.write( + f''.encode() + ) + pdfkit.from_file( [chapter.file.name for chapter in chapters], self.file.name, + cover=cover_file.file.name, + toc={"toc-header-text": "Table of Contents"}, + options={ + "images" if download_images else "no-images": "" + # "margin-top": "-10mm", + # "margin-left": "-10mm", + # "margin-right": "0mm", + # "margin-bottom": "0mm", + # "dump-default-toc-xsl": "", + # "dump-outline": "", + }, + cover_first=True, ) clean_description = self.data["description"].strip().replace("\n", "$/") diff --git a/src/api/src/main.py b/src/api/src/main.py index 282f0da..a9fc1cd 100644 --- a/src/api/src/main.py +++ b/src/api/src/main.py @@ -164,7 +164,7 @@ async def handle_download( logger.info(f"Retrieved story id ({story_id=})") - cover_data = await fetch_cover(metadata["cover"]) + cover_data = await fetch_cover(metadata["cover"].replace("-256-", "-512-")) part_contents = [ f"

{part['title']}

" + (await fetch_part_content(part["id"], cookies=cookies))