-
Notifications
You must be signed in to change notification settings - Fork 86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add PDF generation feature #29
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
import asyncio | ||
import pickle | ||
|
||
from fastapi.responses import HTMLResponse | ||
from fastapi.responses import HTMLResponse, Response | ||
from html5lib import serialize | ||
from html5lib.html5parser import parse | ||
from async_lru import alru_cache | ||
from loguru import logger | ||
from medium_parser import medium_parser_exceptions | ||
from weasyprint import HTML | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add weasyprint as a dependency |
||
|
||
from server import config, medium_cache, redis_storage, medium_parser | ||
from server.services.jinja import base_template, homepage_template | ||
|
@@ -47,7 +47,7 @@ async def fetch_post_metadata(post_id): | |
return HTMLResponse(homepage_template_rendered) | ||
|
||
|
||
async def render_medium_post_link(path: str, use_cache: bool = True, use_redis: bool = True): | ||
async def render_medium_post_link(path: str, use_cache: bool = True, use_redis: bool = True, as_pdf: bool = False): | ||
redis_available = await safe_check_redis_connection(redis_storage) | ||
logger.debug(f"Redis available: {redis_available}") | ||
|
||
|
@@ -100,4 +100,13 @@ async def render_medium_post_link(path: str, use_cache: bool = True, use_redis: | |
serialized_rendered_post = serialize(parsed_rendered_post, encoding="utf-8") | ||
|
||
send_message(f"✅ Successfully rendered post: {path}", True, "GOOD") | ||
|
||
if as_pdf: | ||
pdf = HTML(string=serialized_rendered_post).write_pdf() | ||
return Response(pdf, media_type='application/pdf', headers={'Content-Disposition': f'attachment; filename="{post_id}.pdf"'}) | ||
|
||
return HTMLResponse(serialized_rendered_post) | ||
|
||
|
||
async def generate_pdf(path: str): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see endpoint registration |
||
return await render_medium_post_link(path, as_pdf=True) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -114,3 +114,10 @@ <h1 class="font-bold font-sans break-normal text-gray-900 dark:text-gray-100 pt- | |
cursor: pointer; | ||
} | ||
</style> | ||
<div id="pdf-content" style="display: none;"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. didn't understand what is this |
||
<h1>{{ title }}</h1> | ||
{% if subtitle %}<h2>{{ subtitle }}</h2>{% endif %} | ||
<div> | ||
{% for paragraph in content %}{{ paragraph }}{% endfor %} | ||
</div> | ||
</div> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nahhh, this should be definetly refactored