Skip to content

feat: Add high-fidelity "Save as PDF" using Playwright#42

Open
kalmastenitin wants to merge 1 commit intoFreedium-cfd:mainfrom
kalmastenitin:save_the_article_as_pdf#19
Open

feat: Add high-fidelity "Save as PDF" using Playwright#42
kalmastenitin wants to merge 1 commit intoFreedium-cfd:mainfrom
kalmastenitin:save_the_article_as_pdf#19

Conversation

@kalmastenitin
Copy link

This PR implements a robust "Save as PDF" feature that allows users to download Medium articles as high-quality PDF documents.

Unlike standard HTML-to-PDF converters, this implementation uses Playwright (Headless Chromium) to render the page. This ensures that the PDF looks exactly like the browser's "Print" view ("Ctrl+P"), preserving:

✅ Syntax Highlighting (Code blocks retain colors/formatting).
✅ Complex Layouts (Images, blockquotes, and headers render correctly).
✅ Fonts & Typography (Matches the web reading experience).

Technical Changes
Backend (server/handlers/main.py):

  • Added a new endpoint: GET /render_pdf/{path}.
  • Implemented render_pdf using async_playwright.
  • Injecting custom print CSS to hide navbars, buttons, and footers before rendering.

Docker (Dockerfile):

  • Replaced weasyprint dependencies with playwright.
  • Added installation of Chromium browser and system dependencies.
  • Configured a shared PLAYWRIGHT_BROWSERS_PATH for the freedium user.

Dependencies:
Added: playwright.

How to Test

Rebuild the container to install browsers:
docker compose up --build --no-cache
Navigate to any article.
Use the button : Near Free No. section: PDF
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant