fix(api): Move copyright before TOC, Merge copyright and cover pages, Move HTML to seperate files

This commit is contained in:
TheOnlyWayUp
2024-12-08 11:08:33 +00:00
parent 1948ed67ee
commit ca0b59cb0c
4 changed files with 132 additions and 148 deletions
+15 -34
View File
@@ -514,27 +514,10 @@ wp_copyright: Dict[str, CopyrightData] = {
} }
author_template = """ with open("./pdf/cover_and_copyright.html") as reader:
<html> copyright_template = reader.read()
<head> with open("./pdf/author.html") as reader:
<meta name="viewport" content="width=device-width, initial-scale=1.0"> author_template = reader.read()
<title>About the Author</title>
</head>
<body id="author-body">
<h1>About the Author</h1>
<div id="author-container">
<div id="author-about">
{avatar}
<h2 id="author-name"><a href="https://wattpad.com/user/{username}" id="author-link">{username}</a></h2>
<hr id="author-divider">
<p id="author-bio">
{description}
</p>
</div>
</div>
</body>
</html>
"""
class PDFGenerator: class PDFGenerator:
@@ -585,14 +568,7 @@ class PDFGenerator:
yield part["title"] yield part["title"]
cover_file = tempfile.NamedTemporaryFile(suffix=".html") # Cover and Copyright Page
cover_file.write(
f'<html><body><img width="993" height="1404" src="data:image/jpg;base64,{b64encode(self.cover).decode()}"></img></body></html>'.encode() # A4 Size
)
with open("./pdf/copyright.html") as reader:
copyright_template = reader.read()
copyright_data = wp_copyright[str(self.data["copyright"])] copyright_data = wp_copyright[str(self.data["copyright"])]
copyright_image = ( copyright_image = (
await fetch_cover(copyright_data["image_url"]) await fetch_cover(copyright_data["image_url"])
@@ -636,10 +612,15 @@ style="margin-bottom: 1rem;">""".format(
if image_block if image_block
else about_copyright.replace("{copyright_image}", "") else about_copyright.replace("{copyright_image}", "")
) )
about_copyright_file = tempfile.NamedTemporaryFile(suffix=".html", delete=True) about_copyright = about_copyright.replace(
about_copyright_file.write(about_copyright.encode()) "{cover}", f"data:image/jpg;base64,{b64encode(self.cover).decode()}"
chapters.insert(0, about_copyright_file) )
about_copyright_file.seek(0)
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 = ( author_avatar = (
await fetch_cover( await fetch_cover(
@@ -669,7 +650,7 @@ style="margin-bottom: 1rem;">""".format(
pdfkit.from_file( pdfkit.from_file(
[chapter.file.name for chapter in chapters], [chapter.file.name for chapter in chapters],
self.file.name, self.file.name,
cover=cover_file.file.name, cover=cover_and_copyright_file.file.name,
toc={ toc={
"toc-header-text": "Table of Contents", "toc-header-text": "Table of Contents",
"xsl-style-sheet": "./pdf/toc.xsl", "xsl-style-sheet": "./pdf/toc.xsl",
+95
View File
@@ -0,0 +1,95 @@
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>About the Author</title>
<style>
/* Reset */
body, html {
margin: 0;
padding: 0;
height: 100%;
}
body {
font-family: Arial, sans-serif;
background-color: #f3f4f6;
}
/* Container centering for older browsers */
#author-container {
position: absolute;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%); /* Old WebKit */
transform: translate(-50%, -50%);
width: 90%;
max-width: 400px;
text-align: center;
}
#author-about {
padding: 20px;
/* Fallback for older browsers */
display: block;
margin: 0 auto;
}
#author-profile-picture {
width: 200px;
height: 200px;
-webkit-border-radius: 100px; /* Old WebKit */
border-radius: 100px;
margin: 0 auto 20px auto;
display: block;
}
#author-name {
font-size: 24px;
font-weight: bold;
margin: 0 0 10px 0;
padding: 0;
}
#author-link {
color: #1a202c;
text-decoration: none;
}
#author-link:hover {
color: #4a5568;
text-decoration: underline;
}
#author-divider {
width: 60px;
height: 2px;
background-color: #d1d5db;
border: none;
margin: 0 auto 20px auto;
}
#author-bio {
color: #4b5563;
line-height: 1.6;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<h1>About the Author</h1>
<div id="author-container">
<div id="author-about">
{avatar}
<h2 id="author-name"><a href="https://wattpad.com/user/{username}" id="author-link">{username}</a></h2>
<hr id="author-divider">
<p id="author-bio">
{description}
</p>
</div>
</div>
</body>
</html>
@@ -5,64 +5,42 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Copyright Page</title> <title>Copyright Page</title>
<style> <style>
html, body { html, body {
height: 100%; height: 100%;
margin: 0; margin: 0;
padding: 0; padding: 0;
background-color: #f8f8f8; background-color: #f8f8f8;
} }
body { body {
/* Modern flexbox centering */ text-align: center; /* For inline-block fallback centering */
display: flex; line-height: 1.5;
justify-content: center;
align-items: center;
/* Fallback for older browsers */
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
-webkit-box-align: center;
-webkit-align-items: center;
-ms-flex-align: center;
-webkit-box-pack: center;
-webkit-justify-content: center;
-ms-flex-pack: center;
} }
/* Table-cell fallback for even older browsers */
body:before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
margin-right: -0.25em;
}
#copyright-container { #copyright-container {
/* Basic styles */ position: relative;
width: 100%; margin: 20px auto;
max-width: 600px; width: 100%; /* Make it span the entire page */
max-width: 100%; /* Ensures no unintended shrinking */
/* Styling */
background-color: #ffffff; background-color: #ffffff;
border: 1px solid #e2e8f0; border: 1px solid #e2e8f0;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
padding: 2rem; padding: 2rem;
text-align: center; text-align: center;
/* Fallback positioning */ /* Old WebKit Box Model Compatibility */
display: inline-block;
vertical-align: middle;
/* Ensure proper margin on all browsers */
margin: 20px;
/* Prevent overflow issues */
box-sizing: border-box; box-sizing: border-box;
-webkit-box-sizing: border-box; -webkit-box-sizing: border-box;
-moz-box-sizing: border-box; -moz-box-sizing: border-box;
/* Center the text content */
text-align: center;
} }
#copyright-notice { #copyright-notice {
font-size: 2rem; font-size: 2rem;
@@ -151,6 +129,10 @@
</style> </style>
</head> </head>
<body> <body>
<div style="text-align: center; margin-top: 20px;">
<img width="993" height="1404" src="{cover}" alt="Book Cover" />
</div>
<div id="copyright-container"> <div id="copyright-container">
<h1 id="copyright-notice">Copyright Notice</h1> <h1 id="copyright-notice">Copyright Notice</h1>
<h2 id="copyright-title">{book_title}</h2> <h2 id="copyright-title">{book_title}</h2>
-74
View File
@@ -68,77 +68,3 @@ p {
line-height: 1.6 !important; /* Improve readability */ line-height: 1.6 !important; /* Improve readability */
margin: 10px 0 !important; /* Space between paragraphs */ margin: 10px 0 !important; /* Space between paragraphs */
} }
/* Reset */
#author-body, #author-html {
margin: 0;
padding: 0;
height: 100%;
}
#author-body {
font-family: Arial, sans-serif;
background-color: #f3f4f6;
}
/* Container centering for older browsers */
#author-container {
position: absolute;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%); /* Old WebKit */
transform: translate(-50%, -50%);
width: 90%;
max-width: 400px;
text-align: center;
}
#author-about {
padding: 20px;
/* Fallback for older browsers */
display: block;
margin: 0 auto;
}
#author-profile-picture {
width: 200px;
height: 200px;
-webkit-border-radius: 100px; /* Old WebKit */
border-radius: 100px;
margin: 0 auto 20px auto;
display: block;
}
#author-name {
font-size: 24px;
font-weight: bold;
margin: 0 0 10px 0;
padding: 0;
}
#author-link {
color: #1a202c;
text-decoration: none;
}
#author-link:hover {
color: #4a5568;
text-decoration: underline;
}
#author-divider {
width: 60px;
height: 2px;
background-color: #d1d5db;
border: none;
margin: 0 auto 20px auto;
}
#author-bio {
color: #4b5563;
line-height: 1.6;
margin: 0;
padding: 0;
}