Skip to content

Instantly share code, notes, and snippets.

@robotii
Last active February 24, 2026 18:13
Show Gist options
  • Select an option

  • Save robotii/aa580b1c00937d37c44f1033034b0e1a to your computer and use it in GitHub Desktop.

Select an option

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.

@EntropicNinja
Copy link

Thanks for this update, I indeed took inspiration and I updated to include:

  • Natural page breaks at A4 boundaries (no forced breaks at headings)
  • Headings never stranded at bottom of page (widow protection)
  • Paragraphs that would end near the bottom move to the next page instead
  • Code blocks, tables, images, and callouts never split across pages
  • hr elements converted to explicit page breaks
  • Manual page break: type > [!pagebreak] anywhere in your note
    • Invisible in reading view, forces a page break in PDF export
    • Can be used as many times as needed
    • All clean-break rules apply from that point forward

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