Skip to content

Markdown to PDF

The optional io.github.conamobiledev:pdfkmp-markdown module renders a CommonMark-lite subset through the PdfKmp DSL with a single extension on ContainerScope.

Install it alongside the core dependency:

implementation("io.github.conamobiledev:pdfkmp:1.3.0")
implementation("io.github.conamobiledev:pdfkmp-markdown:1.3.0")

Usage

import com.conamobile.pdfkmp.markdown.markdown
import com.conamobile.pdfkmp.markdown.MarkdownTheme

pdf {
    page {
        markdown(
            """
            # Release notes
            **Bold**, *italic*, `code`, ~~strike~~ and [a link](https://example.com).
            - bullet one
            - bullet two

            > A blockquote.

            | Col A | Col B |
            |-------|-------|
            | 1     | 2     |
            """.trimIndent(),
            theme = MarkdownTheme(),
        )
    }
}

Supported syntax

Syntax Supported
ATX headings (#######)
Paragraphs
Inline **bold**
Inline *italic* / _italic_ ✅ (both asterisk and underscore)
Inline ***bold italic*** ✅ (combined emphasis)
Inline `code`
Inline ~~strike~~
Inline [text](url) ✅ (clickable only when the paragraph is the link — see below)
Ordered & unordered lists
Fenced code blocks
Blockquotes
Horizontal rules
GitHub pipe tables
Anything else degrades to plain paragraph text (never throws)

Theme

MarkdownTheme() carries every styling knob; pass a customised instance to restyle the output. The data class has five fields:

Field Type Default Purpose
baseTextStyle TextStyle TextStyle() Body text style. Headings scale its fontSize; all other blocks inherit its colour, font, and size.
codeBackground PdfColor light grey (0.95, 0.95, 0.95) Fill behind fenced and inline code.
linkColor PdfColor PdfColor.Blue Colour of link text and the underline drawn under inline links.
headingScales List<Float> [2.0, 1.6, 1.3, 1.15, 1.05, 1.0] Six multipliers applied to baseTextStyle.fontSize for h1..h6. Missing levels fall back to 1f.
blockSpacing Dp 8.dp Vertical gap inserted between consecutive blocks.
MarkdownTheme(
    baseTextStyle = TextStyle(fontSize = 11.sp, color = PdfColor.DarkGray),
    linkColor = PdfColor(0.1f, 0.45f, 0.85f),
    headingScales = listOf(2.2f, 1.7f, 1.4f, 1.2f, 1.05f, 1f),
    blockSpacing = 10.dp,
)

Limitations

Link clickability

A link gets a real clickable area only when a paragraph is entirely a single [text](url). A link within running text is styled (coloured + underlined) but not clickable.

Only allowlisted schemes are styled as links

Targets a PDF annotation cannot carry — #anchor, ./other.md, a bare www. domain, anything outside PdfUrls.allowedSchemes — render as plain body text: no annotation, no link colour, no underline. The rule applies in both positions, so one target never looks clickable inside a sentence and plain on a line of its own. Markdown written for the web leans heavily on relative and anchor links, so expect them to come out unstyled; rewrite them as absolute https:// URLs if they should be followable from the PDF.

No monospace font

Code has no guaranteed monospace font (PdfKmp bundles none). It renders in the base font at a slightly smaller size on the theme's code background, so column alignment in code blocks is approximate.

See also