/kMD2PDF

Markdown to PDF conversion library written in Kotlin

Primary LanguageKotlinMIT LicenseMIT

Build Status GitHub license Website shields.io Maven Central

Simple and highly customizable markdown to PDF conversion library

Note: Version 0.2.x has been released! Check out the changelog to see what's new!

Jump To

  1. Installation Guide
  2. Quick Start Guide
  3. Previews

Installation Guide

The repository is hosted on Maven Central. You can add it to your project using the following code based on your build tool:

Maven

<dependency>
  <groupId>com.github.woojiahao</groupId>
  <artifactId>kMD2PDF</artifactId>
  <version>0.2.2</version>
</dependency>

Gradle

implementation 'com.github.woojiahao:kMD2PDF:0.2.2'

If you encounter errors with loading the library, visit the troubleshooting guide here.

Quick Start Guide

All examples are taken from the examples repository.

val document 
  get() = MarkdownDocument("resources/markdown-all-in-one.md")

Default styling

Example here.

fun main() {
  val converter = markdownConverter {
    document(document)
  }
  converter.convert()
}

Specifying PDF location

Example here.

fun main() {
  val converter = markdownConverter {
    document(document)
    targetLocation("${System.getProperty("user.home")}/Desktop/exported.pdf")
  }
  converter.convert()
}

Conversion success

Example here.

fun main() {
  val converter = markdownConverter {
    document(document)
  }
  val conversionResult = converter.convert()
  conversionResult.success {
    if (Desktop.isDesktopSupported()) Desktop.getDesktop().open(it)
  }
}

Conversion failure

Example here.

fun main() {
  val converter = markdownConverter {
    document(document)
  }
  val conversionStatus = converter.convert()
  conversionStatus.failure {
    if (it is FileNotFoundException) println("File is currently already open")
  }
}

Custom styling using style DSL

Example here.

More on this subject can be found on the documentation site here.

fun main() {
  val converter = markdownConverter {
    document(document)

    settings {
      fontSize = 16.0.px
      font = FontFamily("Roboto", "Lato")
      monospaceFont = FontFamily("Fira Code")
    }

    style {
      p {
        textColor = c("455A64")
      }

      ul {
        listStyleType = SQUARE
      }

      selector("tr:nth-child(even)") {
        "background-color" to "#f2f2f2"
      }
    }
  }
  converter.convert()
}

Exporting to HTML

Example here.

More on the subject here.

fun main() {
  val converter = markdownConverter {
    document(document)
    conversionTarget(MarkdownConverter.ConversionTarget.HTML)
  }
  converter.convert()
}

Previews

Sample document

Dark theme

Auto-generated table of contents