feat(api): Custom PDF Metadata
This commit is contained in:
@@ -3,9 +3,10 @@ from io import BytesIO
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from tempfile import NamedTemporaryFile, _TemporaryFileWrapper
|
from tempfile import NamedTemporaryFile, _TemporaryFileWrapper
|
||||||
|
|
||||||
|
import pydyf
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
from jinja2 import Template
|
from jinja2 import Template
|
||||||
from weasyprint import CSS, HTML
|
from weasyprint import CSS, HTML, Document
|
||||||
from weasyprint.text.fonts import FontConfiguration
|
from weasyprint.text.fonts import FontConfiguration
|
||||||
|
|
||||||
from ..models import Story
|
from ..models import Story
|
||||||
@@ -154,6 +155,11 @@ class PDFGenerator(AbstractGenerator):
|
|||||||
|
|
||||||
self.content: str = Template(self.content).render(data)
|
self.content: str = Template(self.content).render(data)
|
||||||
|
|
||||||
|
def write_custom_metadata(self, document: Document, pdf: pydyf.PDF):
|
||||||
|
"""Write non-standard metadata fields to the PDF."""
|
||||||
|
pdf.info["completed"] = pydyf.String(str(self.story["completed"]))
|
||||||
|
pdf.info["mature"] = pydyf.String(str(self.story["mature"]))
|
||||||
|
|
||||||
def generate_pdf(self):
|
def generate_pdf(self):
|
||||||
"""Generate and write the PDF to a temporary file (self.book)."""
|
"""Generate and write the PDF to a temporary file (self.book)."""
|
||||||
font_config = FontConfiguration()
|
font_config = FontConfiguration()
|
||||||
@@ -162,7 +168,11 @@ class PDFGenerator(AbstractGenerator):
|
|||||||
|
|
||||||
html_obj = HTML(string=self.content)
|
html_obj = HTML(string=self.content)
|
||||||
html_obj.write_pdf(
|
html_obj.write_pdf(
|
||||||
self.book.name, stylesheets=[stylesheet_obj], font_config=font_config
|
self.book.name,
|
||||||
|
stylesheets=[stylesheet_obj],
|
||||||
|
font_config=font_config,
|
||||||
|
finisher=self.write_custom_metadata,
|
||||||
|
options={"custom_metadata": True},
|
||||||
)
|
)
|
||||||
|
|
||||||
def compile(self):
|
def compile(self):
|
||||||
|
|||||||
@@ -3,22 +3,19 @@
|
|||||||
|
|
||||||
|
|
||||||
<head>
|
<head>
|
||||||
<meta name=keywords content="{{clean_tags}}">
|
|
||||||
|
|
||||||
<title>{{ book_title }}</title>
|
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
|
||||||
<meta name=Subject content="{{description}}">
|
<!-- https://doc.courtbouillon.org/weasyprint/stable/api_reference.html#weasyprint.document.DocumentMetadata -->
|
||||||
<meta name=Author content="{{author}}">
|
<title>{{ book_title }}</title>
|
||||||
<meta name=Keywords content="{{tags}}">
|
<meta name=description content="{{description}}">
|
||||||
<meta name=Language content="{{langcode}}">
|
<meta name=author content="{{author}}">
|
||||||
<meta name=CreationDate content="{{created}}">
|
<meta name=keywords content="{{clean_tags}}">
|
||||||
<meta name=ModDate content="{{modified}}">
|
<meta name=language content="{{langcode}}">
|
||||||
<meta name=Generator content="Dhanush Rambhatla (TheOnlyWayUp - https://rambhat.la) and WattpadDownloader">
|
<meta name=dcterms.created content="{{created}}">
|
||||||
|
<meta name=dcterms.modified content="{{modified}}">
|
||||||
<meta name=completed content="{{is_completed}}">
|
<meta name=generator content="Dhanush Rambhatla (TheOnlyWayUp - https://rambhat.la) and WattpadDownloader">
|
||||||
<meta name=maturecontent content="{{is_mature}}">
|
|
||||||
|
|
||||||
|
|
||||||
</head>
|
</head>
|
||||||
|
|||||||
Reference in New Issue
Block a user