PDF Export¶
Medium Converter can export articles to PDF, creating professionally formatted documents that are ready for printing or sharing.
Installation¶
PDF export requires additional dependencies:
Or if installing everything:
Basic Usage¶
Command Line¶
Python API¶
from medium_converter import convert_article
await convert_article(
url="https://medium.com/example-article",
output_format="pdf",
output_path="article.pdf"
)
Customization Options¶
PDF export supports various customization options:
| Option | Description | Default |
|---|---|---|
page_size |
Page size (A4, Letter, etc.) | "A4" |
font_size |
Base font size in points | 11 |
font_family |
Font family for text | "Helvetica" |
margins |
Page margins in inches | {"top": 1, "right": 1, "bottom": 1, "left": 1} |
include_toc |
Generate table of contents | True |
include_cover |
Generate cover page | True |
include_images |
Include images in PDF | True |
syntax_highlighting |
Enable syntax highlighting for code | True |
header_text |
Custom header text | None |
footer_text |
Custom footer text | None |
page_numbers |
Include page numbers | True |
Command Line¶
medium convert https://medium.com/example-article -f pdf \
--option page_size=Letter \
--option font_size=12 \
--option include_cover=true \
--option header_text="Medium Article" \
--option footer_text="Generated with Medium Converter"
Python API¶
await convert_article(
url="https://medium.com/example-article",
output_format="pdf",
output_path="article.pdf",
export_options={
"page_size": "Letter",
"font_size": 12,
"font_family": "Times-Roman",
"include_cover": True,
"header_text": "Medium Article",
"footer_text": "Generated with Medium Converter"
}
)
Custom Styling¶
For advanced styling, you can provide a CSS stylesheet:
custom_css = """
@page {
@top-center {
content: "Custom Header";
}
@bottom-center {
content: "Page " counter(page);
}
}
h1 {
color: #0066cc;
font-size: 24pt;
}
pre {
background-color: #f5f5f5;
padding: 10pt;
border-radius: 5pt;
}
"""
await convert_article(
url="https://medium.com/example-article",
output_format="pdf",
output_path="article.pdf",
export_options={
"custom_stylesheet": custom_css
}
)
Or provide a stylesheet file:
await convert_article(
url="https://medium.com/example-article",
output_format="pdf",
output_path="article.pdf",
export_options={
"stylesheet_path": "path/to/custom.css"
}
)
PDF Features¶
The PDF exporter provides several features:
- Cover Page: Title, author, date, and estimated reading time
- Table of Contents: Automatically generated TOC with page numbers
- Header and Footer: Customizable header and footer content
- Syntax Highlighting: Code blocks with syntax highlighting
- Hyperlinks: Active hyperlinks to websites and internal references
- Images: Embedded images with captions
- Math Rendering: LaTeX math formulas rendered properly
- Bookmarks: PDF bookmarks for easy navigation
Implementation Details¶
Under the hood, Medium Converter uses ReportLab for PDF generation. The process involves:
- Parsing the article into a structured document
- Converting the content to ReportLab's flowable objects
- Applying styling and formatting
- Generating the PDF with proper layout
For extremely complex layouts, consider using the LaTeX exporter and converting to PDF with a LaTeX processor.