From ca0b59cb0ce9e2c1edc32f690dc1781d04013a4d Mon Sep 17 00:00:00 2001 From: TheOnlyWayUp Date: Sun, 8 Dec 2024 11:08:33 +0000 Subject: [PATCH] fix(api): Move copyright before TOC, Merge copyright and cover pages, Move HTML to seperate files --- src/api/src/create_book.py | 49 +++------- src/api/src/pdf/author.html | 95 +++++++++++++++++++ ...opyright.html => cover_and_copyright.html} | 62 +++++------- src/api/src/pdf/stylesheet.css | 74 --------------- 4 files changed, 132 insertions(+), 148 deletions(-) create mode 100644 src/api/src/pdf/author.html rename src/api/src/pdf/{copyright.html => cover_and_copyright.html} (78%) diff --git a/src/api/src/create_book.py b/src/api/src/create_book.py index 4922b81..753a566 100644 --- a/src/api/src/create_book.py +++ b/src/api/src/create_book.py @@ -514,27 +514,10 @@ wp_copyright: Dict[str, CopyrightData] = { } -author_template = """ - - - - About the Author - - -

About the Author

-
-
- {avatar} -

{username}

-
-

- {description} -

-
-
- - -""" +with open("./pdf/cover_and_copyright.html") as reader: + copyright_template = reader.read() +with open("./pdf/author.html") as reader: + author_template = reader.read() class PDFGenerator: @@ -585,14 +568,7 @@ class PDFGenerator: yield part["title"] - cover_file = tempfile.NamedTemporaryFile(suffix=".html") - cover_file.write( - f''.encode() # A4 Size - ) - - with open("./pdf/copyright.html") as reader: - copyright_template = reader.read() - + # Cover and Copyright Page copyright_data = wp_copyright[str(self.data["copyright"])] copyright_image = ( await fetch_cover(copyright_data["image_url"]) @@ -636,10 +612,15 @@ style="margin-bottom: 1rem;">""".format( if image_block else about_copyright.replace("{copyright_image}", "") ) - about_copyright_file = tempfile.NamedTemporaryFile(suffix=".html", delete=True) - about_copyright_file.write(about_copyright.encode()) - chapters.insert(0, about_copyright_file) - about_copyright_file.seek(0) + about_copyright = about_copyright.replace( + "{cover}", f"data:image/jpg;base64,{b64encode(self.cover).decode()}" + ) + + cover_and_copyright_file = tempfile.NamedTemporaryFile( + suffix=".html", delete=True + ) + cover_and_copyright_file.write(about_copyright.encode()) + cover_and_copyright_file.seek(0) author_avatar = ( await fetch_cover( @@ -669,7 +650,7 @@ style="margin-bottom: 1rem;">""".format( pdfkit.from_file( [chapter.file.name for chapter in chapters], self.file.name, - cover=cover_file.file.name, + cover=cover_and_copyright_file.file.name, toc={ "toc-header-text": "Table of Contents", "xsl-style-sheet": "./pdf/toc.xsl", diff --git a/src/api/src/pdf/author.html b/src/api/src/pdf/author.html new file mode 100644 index 0000000..b0fa63d --- /dev/null +++ b/src/api/src/pdf/author.html @@ -0,0 +1,95 @@ + + + + About the Author + + + +

About the Author

+
+
+ {avatar} +

{username}

+
+

+ {description} +

+
+
+ + + diff --git a/src/api/src/pdf/copyright.html b/src/api/src/pdf/cover_and_copyright.html similarity index 78% rename from src/api/src/pdf/copyright.html rename to src/api/src/pdf/cover_and_copyright.html index ede4616..d02e3d3 100644 --- a/src/api/src/pdf/copyright.html +++ b/src/api/src/pdf/cover_and_copyright.html @@ -5,64 +5,42 @@ Copyright Page +
+ Book Cover +
+