feat(api): Include Copyright notice in PDFs!

This commit is contained in:
TheOnlyWayUp
2024-12-08 04:46:57 +00:00
parent e30bbf40b9
commit 1948ed67ee
2 changed files with 235 additions and 23 deletions
+51 -23
View File
@@ -464,7 +464,7 @@ wp_copyright: Dict[str, CopyrightData] = {
}, },
"2": { "2": {
"name": "Public Domain", "name": "Public Domain",
"statement": "This work is in the public domain. Originally published in {published_year}.", "statement": "This work is in the public domain. Originally published in {published_year} by {username}.",
"freedoms": "Free to use for any purpose without permission.", "freedoms": "Free to use for any purpose without permission.",
"printing": "Allowed for personal or commercial purposes.", "printing": "Allowed for personal or commercial purposes.",
"image_url": "http://mirrors.creativecommons.org/presskit/buttons/88x31/png/cc-zero.png", "image_url": "http://mirrors.creativecommons.org/presskit/buttons/88x31/png/cc-zero.png",
@@ -590,28 +590,56 @@ class PDFGenerator:
f'<html><body><img width="993" height="1404" src="data:image/jpg;base64,{b64encode(self.cover).decode()}"></img></body></html>'.encode() # A4 Size f'<html><body><img width="993" height="1404" src="data:image/jpg;base64,{b64encode(self.cover).decode()}"></img></body></html>'.encode() # A4 Size
) )
# copyright_data = wp_copyright[str(self.data["copyright"])] with open("./pdf/copyright.html") as reader:
# copyright_image = ( copyright_template = reader.read()
# await fetch_cover(copyright_data["image_url"])
# if copyright_data["image_url"] copyright_data = wp_copyright[str(self.data["copyright"])]
# else None copyright_image = (
# ) await fetch_cover(copyright_data["image_url"])
# about_copyright = copyright_template.format( if copyright_data["image_url"]
# statement=copyright_data["statement"].format( else None
# username=self.data["user"]["username"], )
# published_year=self.data["createDate"], about_copyright = (
# ), copyright_template.replace(
# freedoms=copyright_data["freedoms"], "{statement}",
# printing=copyright_data["printing"], copyright_data["statement"].format(
# ) username=self.data["user"]["username"],
# about_copyright = ( published_year=self.data["createDate"].split("-", 2)[0],
# about_copyright.replace( ),
# "{avatar}", )
# f"data:image/jpg;base64,{b64encode(copyright_image).decode()}", .replace("{freedoms}", copyright_data["freedoms"])
# ) .replace(
# if copyright_image "{printing}",
# else about_copyright.replace("{avatar}", "") copyright_data["printing"],
# ) )
.replace("{book_id}", self.data["id"])
.replace("{book_title}", self.data["title"])
)
image_block = (
"""<img src="{image_url}"
alt="{name}"
width="88"
height="31"
style="margin-bottom: 1rem;">""".format(
image_url=f"data:image/jpg;base64,{b64encode(copyright_image).decode()}",
name=copyright_data["name"],
)
if copyright_image
else ""
)
about_copyright = (
about_copyright.replace(
"{copyright_image}",
image_block,
)
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)
author_avatar = ( author_avatar = (
await fetch_cover( await fetch_cover(
+184
View File
@@ -0,0 +1,184 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Copyright Page</title>
<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
background-color: #f8f8f8;
}
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;
}
#copyright-container {
/* Basic styles */
width: 100%;
max-width: 600px;
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 */
box-sizing: border-box;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
}
#copyright-notice {
font-size: 2rem;
color: #2d3748;
margin-bottom: 1rem;
}
#copyright-title {
font-size: 1.8rem;
color: #2d3748;
margin-top: 1rem;
margin-bottom: 2rem;
line-height: 1.4;
}
#copyright-ex-libris {
font-size: 1.5rem;
font-style: italic;
color: #4a5568;
margin: 2rem 0;
}
.copyright-separator {
width: 100%;
max-width: 400px;
height: 1px;
background: #e2e8f0;
position: relative;
margin: 2rem auto;
/* Gradient fallback */
background: -webkit-gradient(linear, left top, right top, from(transparent), color-stop(#718096), to(transparent));
background: -webkit-linear-gradient(left, transparent, #718096, transparent);
background: -moz-linear-gradient(left, transparent, #718096, transparent);
background: -o-linear-gradient(left, transparent, #718096, transparent);
background: linear-gradient(to right, transparent, #718096, transparent);
}
#copyright-info {
max-width: 400px;
margin: 0 auto;
font-family: 'Garamond', Georgia, serif;
}
#copyright-statement {
font-size: 0.9rem;
line-height: 1.6;
color: #2d3748;
margin-bottom: 1rem;
}
#copyright-freedoms,
#copyright-printing,
#copyright-isbn {
font-size: 0.8rem;
color: #718096;
margin-bottom: 0.8rem;
}
#copyright-book-link {
font-size: 0.9rem;
color: #3182ce;
text-decoration: none;
display: inline-block;
margin-top: 1rem;
/* Cross-browser transition */
-webkit-transition: all 0.2s ease;
-moz-transition: all 0.2s ease;
-o-transition: all 0.2s ease;
transition: all 0.2s ease;
}
#copyright-book-link:hover {
text-decoration: underline;
color: #2c5282;
}
/* Simple horizontal line instead of decorative separator for older browsers */
@media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) {
.copyright-separator {
border-top: 1px solid #718096;
background: none;
}
}
</style>
</head>
<body>
<div id="copyright-container">
<h1 id="copyright-notice">Copyright Notice</h1>
<h2 id="copyright-title">{book_title}</h2>
<div class="copyright-separator"></div>
<p id="copyright-ex-libris">Ex Libris Sapientiae</p>
<div class="copyright-separator"></div>
<div id="copyright-info">
{copyright_image}
<p id="copyright-statement">
{statement}
</p>
<p id="copyright-freedoms">
{freedoms}
</p>
<p id="copyright-printing">
Printing: {printing}
</p>
<p id="copyright-isbn">
ID: {book_id}
</p>
<a href="https://wattpad.com/story/{book_id}" id="copyright-book-link">
View Book Online
</a>
</div>
</div>
</body>
</html>