From 7552a64db46f2eea141a3e71a2e626d80ebac8a4 Mon Sep 17 00:00:00 2001 From: yusefes Date: Sat, 26 Oct 2024 23:30:28 +0330 Subject: [PATCH] Add PDF generation feature Fixes #19 Add feature to save Medium articles as PDFs. * **Backend Changes:** - Add a new endpoint `generate_pdf` in `web/server/handlers/post.py` to handle PDF generation requests. - Import `WeasyPrint` for rendering HTML to PDF. - Update the `render_medium_post_link` function to support PDF generation. * **Template Changes:** - Ensure compatibility with PDF rendering by adjusting styles in `web/server/templates/post.html`. - Add a hidden div with the article content for PDF generation. * **Frontend Changes:** - Add a "Save as PDF" button in `new-web/src/routes/[slug]/+page.svelte` to trigger the PDF generation. - Implement a function to call the new PDF generation endpoint. * **Documentation:** - Update `README.md` to include instructions for the new PDF generation feature. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/Freedium-cfd/web/issues/19?shareId=XXXX-XXXX-XXXX-XXXX). --- README.md | 14 ++++++++++++++ new-web/src/routes/[slug]/+page.svelte | 18 ++++++++++++++++++ web/server/handlers/post.py | 15 ++++++++++++--- web/server/templates/post.html | 7 +++++++ 4 files changed, 51 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9ffddc0..8522a51 100644 --- a/README.md +++ b/README.md @@ -87,3 +87,17 @@ These steps will set up and run your local Freedium instance. - [ ] Integrate Grafana/Prometheus to monitor our services - [ ] Add more metrics to our services to have ability to monitor it - [ ] Make able translate posts to different languages using translatepy library + +## New PDF Generation Feature + +We have added a new feature that allows you to save Medium articles as PDFs. Here are the steps to use this feature: + +1. Navigate to the article you want to save as a PDF. +2. Click on the "Save as PDF" button. +3. The article will be downloaded as a PDF file. + +This feature is powered by the WeasyPrint library, which converts HTML content to PDF format. The implementation details can be found in the following files: + +- `web/server/handlers/post.py`: Contains the endpoint for PDF generation. +- `web/server/templates/post.html`: Ensures compatibility with PDF rendering. +- `new-web/src/routes/[slug]/+page.svelte`: Adds the "Save as PDF" button and implements the function to call the PDF generation endpoint. diff --git a/new-web/src/routes/[slug]/+page.svelte b/new-web/src/routes/[slug]/+page.svelte index b78f40e..adaecf5 100644 --- a/new-web/src/routes/[slug]/+page.svelte +++ b/new-web/src/routes/[slug]/+page.svelte @@ -68,6 +68,19 @@ Stay tuned for more exciting updates as we continue to revolutionize the world o contentLoaded = true; }, 500); }); + + async function saveAsPDF() { + const response = await fetch(`/generate-pdf?path=${encodeURIComponent(window.location.pathname)}`); + const blob = await response.blob(); + const url = window.URL.createObjectURL(blob); + const a = document.createElement('a'); + a.href = url; + a.download = `${data.title}.pdf`; + document.body.appendChild(a); + a.click(); + a.remove(); + window.URL.revokeObjectURL(url); + } @@ -118,6 +131,11 @@ Stay tuned for more exciting updates as we continue to revolutionize the world o {/if} +
+ +