Generating a PDF from Markdown is easy. Generating a beautiful, dark-themed PDF without those ugly white system margins? Not so much. Here's how I solved it and turned it into an AI skill so I never have to touch CSS again.
The White Border Problem
The Solution: Zero-Margin Playwright
- Tell Playwright to use 0 margins:
Python
# Playwright PDF generation
page.pdf(
path=output,
format="A4",
margin={"top": "0", "bottom": "0", "left": "0", "right": "0"},
print_background=True
)
- Tell CSS to fill the void:
Css
@page {
size: A4;
margin: 0; /* Kills the physical print margin */
}
html, body {
margin: 0;
padding: 0;
background: #0d1117; /* Full bleed background! */
}
body {
/* Use CSS padding to push content inward */
padding: 60px 80px;
}
Fixing Collapsed Tables
Css
table {
table-layout: auto;
}
th, td {
white-space: normal;
word-break: break-word;
}
/* Force the 1st and 3rd columns to shrink to their content */
td:first-child, th:first-child,
td:nth-child(3), th:nth-child(3) {
white-space: nowrap;
width: 1%;
}
Wrapping it in an AI Agent Skill
Markdown
# /pdf — Markdown to PDF
Convert Markdown files to styled PDF using pandoc → HTML → Playwright pipeline.
**Argument**: Path to `.md` file. Required.
```bash
uv run --with playwright ~/.agents/skills/md2pdf/scripts/md2pdf.py $ARGUMENTS
```
把 Markdown 转成 PDF 很容易,但要转成一个漂亮、没有丑陋系统白边的深色主题 PDF?这就有点折腾了。这篇文章记录了我是如何解决这个问题,并把它封装成 AI Agent 的专属技能的,这样我就再也不用碰 CSS 了。
恼人的“白边”问题
解决方案:零边距的 Playwright
- 告诉 Playwright 把 margin 设为 0:
Python
# Playwright PDF generation
page.pdf(
path=output,
format="A4",
margin={"top": "0", "bottom": "0", "left": "0", "right": "0"},
print_background=True
)
- 用 CSS 填补空白:
Css
@page {
size: A4;
margin: 0; /* 杀掉物理打印白边 */
}
html, body {
margin: 0;
padding: 0;
background: #0d1117; /* 背景色铺满全画幅! */
}
body {
/* 用 CSS padding 把内容往里挤,营造留白 */
padding: 60px 80px;
}
修复表格列宽“被挤爆”的问题
Css
table {
table-layout: auto;
}
th, td {
white-space: normal;
word-break: break-word;
}
/* 强迫第 1 列和第 3 列收缩到内容的最小宽度 */
td:first-child, th:first-child,
td:nth-child(3), th:nth-child(3) {
white-space: nowrap;
width: 1%;
}
把这些脏活累活丢给 AI Agent
Markdown
# /pdf — Markdown to PDF
Convert Markdown files to styled PDF using pandoc → HTML → Playwright pipeline.
**Argument**: Path to `.md` file. Required.
```bash
uv run --with playwright ~/.agents/skills/md2pdf/scripts/md2pdf.py $ARGUMENTS
```
让 AI 帮你搞定 (AI Deploy Prompt)
Text
Please help me set up a Markdown to PDF conversion pipeline on my machine.
Requirements:
1. I want a Python script that uses `pandoc` to convert MD to HTML, and `playwright` to render the HTML to PDF.
2. The script should use `uv run --with playwright` so it doesn't pollute my global environment.
3. The PDF must have ZERO margins in Playwright (`margin: 0`), using CSS `body { padding: 60px 80px; }` for visual spacing to ensure the background color bleeds to the absolute edge.
4. It should have two themes: a Dark theme (background `#0d1117`, text `#f0f6fc`) as default, and a Light theme (GitHub style) triggered by a `--light` flag.
5. Please handle Markdown tables properly: make the first column and third column "shrink-wrap" their content (`white-space: nowrap; width: 1%`) so short status text doesn't awkwardly line-break.
Please write the script to a path like `~/.local/bin/md2pdf` or my agent skills folder, make it executable, and show me how to test it.