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 = """
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<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>
"""
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'<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()
# 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",
+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,6 +5,8 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Copyright Page</title>
<style>
html, body {
height: 100%;
margin: 0;
@@ -13,57 +15,33 @@
}
body {
/* Modern flexbox centering */
display: flex;
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;
text-align: center; /* For inline-block fallback centering */
line-height: 1.5;
}
#copyright-container {
/* Basic styles */
width: 100%;
max-width: 600px;
position: relative;
margin: 20px auto;
width: 100%; /* Make it span the entire page */
max-width: 100%; /* Ensures no unintended shrinking */
/* Styling */
background-color: #ffffff;
border: 1px solid #e2e8f0;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
padding: 2rem;
text-align: center;
/* Fallback positioning */
display: inline-block;
vertical-align: middle;
/* Ensure proper margin on all browsers */
margin: 20px;
/* Prevent overflow issues */
/* Old WebKit Box Model Compatibility */
box-sizing: border-box;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
/* Center the text content */
text-align: center;
}
#copyright-notice {
font-size: 2rem;
color: #2d3748;
@@ -151,6 +129,10 @@
</style>
</head>
<body>
<div style="text-align: center; margin-top: 20px;">
<img width="993" height="1404" src="{cover}" alt="Book Cover" />
</div>
<div id="copyright-container">
<h1 id="copyright-notice">Copyright Notice</h1>
<h2 id="copyright-title">{book_title}</h2>
-74
View File
@@ -68,77 +68,3 @@ p {
line-height: 1.6 !important; /* Improve readability */
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;
}