timrs2998 / pdf-builder

PDF builder written in Kotlin with a statically typed DSL
https://timrs2998.github.io/pdf-builder/
GNU General Public License v3.0
58 stars 16 forks source link
jvm kotlin kotlin-library pdf pdf-generation

License: GPL v3 Build Status

pdf-builder

PDF builder written in Kotlin with a statically typed DSL. Inspired by Craig's document-builder library. Under the hood, it relies on Apache PDFBox to create the pdf files.

Usage

Both pdf-build and Apache PDFBox are required dependencies. Include the following in your build.gradle.kts:

repositories {
    maven { url = uri("https://jitpack.io") }
}

dependencies {
    implementation("com.github.timrs2998:pdf-builder:2.1.0")
    implementation("org.apache.pdfbox:pdfbox:3.0.0")
}

and you can use the library in Kotlin with its DSL:

val pdDocument = document {
  text("Hello")
  text("Hello, color is red!") {
    fontColor = Color(1f, .1f, .1f)
  }
  table {
    row {
      text("r1 c1")
      text("r1 c2")
    }
    row {
      text("r2 c1")
      text("r2 c2")
    }
    border = Border(1f, 2f, 3f, 4f, Color.GREEN, Color.RED, Color.BLUE, Color.BLACK)
  }
}

pdDocument.use { pdDocument ->
  pdDocument.save("output.pdf")
}

or Java without a DSL:

Document document = new Document();
TextElement t1 = new TextElement("Hello");
TextElement t2 = new TextElement("Hello, color is red!");
t2.setFontColor(new Color(1f, .1f, .1f));
document.getChildren().add(t1);
document.getChildren().add(t2);

Development Notes

To build from source:

git clone git@github.com:timrs2998/pdf-builder.git
cd pdf-builder/
./gradlew build

To release a new version, use GitHub to create release tags in "v1.0.0" format matching build.gradle.kts. GitHub Actions will build and publish to the GitHub Packages registry.