Skip to content

Instantly share code, notes, and snippets.

@robotii
Last active June 19, 2025 14:25
Show Gist options
  • Save robotii/aa580b1c00937d37c44f1033034b0e1a to your computer and use it in GitHub Desktop.
Save robotii/aa580b1c00937d37c44f1033034b0e1a to your computer and use it in GitHub Desktop.
Obsidian Pagebreaks
/*
Create pagebreaks in exported Obsidian PDFs.
Rules:
- Convert ALL hr to page breaks.
- Page break before h1 and h2, except for the h1 on the first page.
This should now work correctly
- Don't break immediately after a heading.
- Don't break in the middle of preformatted text or blockquotes.
- Avoid splitting up h1 immediately followed by h2 etc.
*/
@media print {
/* Avoid page breaks on first page for h1 */
div:has(pre.frontmatter)+div:has(h1) h1 {
break-before: avoid;
}
/* Break before each h1 and h2 heading */
h1, h2 {
break-before: page;
}
/* Avoid splitting adjacent headers */
div:has(h1)+div:has(h2) h2,
div:has(h2)+div:has(h3) h3,
div:has(h3)+div:has(h4) h4,
div:has(h4)+div:has(h5) h5,
div:has(h5)+div:has(h6) h6 {
break-before: avoid;
}
/* Avoid breaks directly after headings */
h1, h2, h3, h4, h5, h6 {
break-after: avoid;
}
/* Avoid breaking in the middle of a pre or blockquote */
pre, blockquote {
break-inside: avoid;
}
/* Convert hr to page break and hide it */
hr {
visibility: hidden;
break-before: page;
}
/* Set background to white for better printing */
body {
background-color: white;
}
}
@robotii
Copy link
Author

robotii commented Jun 19, 2025

Updated gist to properly select the first h1 and remove page break before it.
Added support to not split adjacent headings.
Change background colour to white for better PDF generation.

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