Skip to content

Commit

Permalink
V0.2.2: Add pdf rendering with playwright (alfa)
Browse files Browse the repository at this point in the history
  • Loading branch information
LimeBeck committed Feb 18, 2024
1 parent f26e461 commit 52b86e8
Show file tree
Hide file tree
Showing 13 changed files with 234 additions and 76 deletions.
24 changes: 22 additions & 2 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,31 @@ Run server with presentation:
jbang [email protected] run ./MyAwesomePresentation.reveal.kts
```

Render presentation to static html site:
Bundle presentation to static html site:
```bash
jbang [email protected] render ./MyAwesomePresentation.reveal.kts
jbang [email protected] bundle ./MyAwesomePresentation.reveal.kts
```

Render presentation to pdf via playwright:
```bash
jbang [email protected] pdf ./MyAwesomePresentation.reveal.kts -o myPresentation.pdf
```

[!CAUTION]
For now playwright requires to download chromium at first run via npm

Playwright documentation: https://playwright.dev/java/docs/browsers

```bash
jbang [email protected] pdf install-chrome
```

Uninstall chrome
```bash
jbang [email protected] pdf uninstall-chrome
```


Create new presentation from template
```bash
jbang [email protected] init my-awesome-presentation
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
plugins {
kotlin("multiplatform")
id("com.github.ben-manes.versions") version "0.47.0"
alias(libs.plugins.versions)
}

repositories {
Expand Down
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ yarn.ignoreScripts = false

kotlinVersion=1.9.20

revealKtVersion=0.2.1
revealKtVersion=0.2.2

kotlinCoroutinesVersion=1.7.3
kotlinCoroutinesVersion=1.8.0
ktorVersion=2.1.3
dokkaVersion=1.8.20
dokkaVersion=1.9.10


repo.uri=https://maven.pkg.github.com/LimeBeck/reveal-kt
11 changes: 6 additions & 5 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
[versions]

kotlin = "1.9.0"
serialization = "1.4.1"
serialization = "1.6.3"
ktor = "2.1.3"
kxhtml = "0.9.1"
kxhtml = "0.11.0"

[libraries]
slf4j = { module = "org.slf4j:slf4j-api", version = "2.0.7" }
logback = { module = "ch.qos.logback:logback-classic", version = "1.4.14"}
clikt = { module = "com.github.ajalt.clikt:clikt", version = "4.2.1" }
logback = { module = "ch.qos.logback:logback-classic", version = "1.5.0"}
clikt = { module = "com.github.ajalt.clikt:clikt", version = "4.2.2" }
kxhtml-jvm = { module = "org.jetbrains.kotlinx:kotlinx-html-jvm", version.ref = "kxhtml" }
kxhtml = { module = "org.jetbrains.kotlinx:kotlinx-html", version.ref = "kxhtml" }
uuid = { module = "com.benasher44:uuid", version = "0.8.2" }
qrcode = { module = "io.github.g0dkar:qrcode-kotlin", version = "4.1.1" }
playwright = { module = "com.microsoft.playwright:playwright", version = "1.41.2" }

[plugins]

versions = { id = "com.github.ben-manes.versions", version = "0.50.0" }
versions = { id = "com.github.ben-manes.versions", version = "0.51.0" }
dokka = { id = "org.jetbrains.dokka:dokka-gradle-plugin", version.ref = "kotlin" }
build-config = { id = "dev.limebeck.build-time-config", version = "2.2.0"}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
3 changes: 2 additions & 1 deletion reveal-kt/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ buildTimeConfig {
kotlin {
jvm {
java {
withSourcesJar()
// withSourcesJar()
}
withJava()
testRuns["test"].executionTask.configure {
Expand Down Expand Up @@ -76,6 +76,7 @@ kotlin {
implementation(libs.logback)
implementation(libs.slf4j)
implementation(libs.clikt)
implementation(libs.playwright)
}
}
val jvmTest by getting
Expand Down
23 changes: 21 additions & 2 deletions reveal-kt/app/src/jvmMain/kotlin/Application.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
package dev.limebeck.application

import com.github.ajalt.clikt.completion.completionOption
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.core.subcommands
import dev.limebeck.application.commands.*
import dev.limebeck.revealkt.RevealkConfig

fun main(args: Array<String>) = RevealKtApplication()
.subcommands(RunServer(), InitTemplate(), RenderToFile())
fun main(args: Array<String>) = RevealKtCliApplication()
.subcommands(
RunServer(),
InitTemplate(),
BundleToStatic(),
RenderPdf().subcommands(InstallChrome(), UninstallChrome())
)
.completionOption()
.main(args)

class RevealKtCliApplication : CliktCommand(
printHelpOnEmptyArgs = true,
help = """
RevealKt CLI
Application version ${RevealkConfig.version}
""".trimIndent()
) {
override fun run() = Unit
}
Original file line number Diff line number Diff line change
@@ -1,62 +1,22 @@
package dev.limebeck.application
package dev.limebeck.application.commands

import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.core.context
import com.github.ajalt.clikt.output.MordantHelpFormatter
import com.github.ajalt.clikt.parameters.arguments.argument
import com.github.ajalt.clikt.parameters.options.default
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.types.file
import com.github.ajalt.clikt.parameters.types.int
import com.github.ajalt.clikt.parameters.types.path
import dev.limebeck.application.server.*
import dev.limebeck.application.templates.generatePresentationTemplate
import dev.limebeck.revealkt.RevealkConfig
import dev.limebeck.application.getResourcesList
import dev.limebeck.application.isFont
import dev.limebeck.application.server.logger
import dev.limebeck.application.server.renderLoadResult
import dev.limebeck.revealkt.scripts.RevealKtScriptLoader
import java.io.File
import java.nio.file.Path
import kotlin.io.path.*

class RevealKtApplication : CliktCommand(
printHelpOnEmptyArgs = true,
help = """
RevealKt CLI
Application version ${RevealkConfig.version}
""".trimIndent()
) {
override fun run() = Unit
}

class RunServer : CliktCommand(name = "run", help = "Serve presentation with live-reload") {
val port: Int by option(help = "Port").int().default(8080)
val host: String by option(help = "Host").default("0.0.0.0")
val basePath: Path? by option(help = "Script dir").path()
val script: File by argument(help = "Script file").file(canBeDir = false, mustBeReadable = true)

init {
context {
helpFormatter = {
MordantHelpFormatter(
showDefaultValues = true,
context = it
)
}
}
}

override fun run() {
runServer(
Config(
server = ServerConfig(host, port),
basePath = basePath?.pathString ?: script.parent,
script = script
)
)
}
}

class RenderToFile : CliktCommand(name = "render", help = "Render to file") {
class BundleToStatic : CliktCommand(name = "bundle", help = "Bundle to static html file") {
val outputDir: Path? by option(help = "Output dir").path(
mustBeWritable = true,
canBeDir = true,
Expand Down Expand Up @@ -112,16 +72,4 @@ class RenderToFile : CliktCommand(name = "render", help = "Render to file") {
}
}
}
}

class InitTemplate : CliktCommand(name = "init", help = "Create presentation template") {
val name: String by argument(help = "Presentation name")
val basePath: Path by option(help = "Template dir")
.path(canBeDir = true, canBeFile = false)
.default(Path.of("."))
val dirname: String? by option()

override fun run() {
generatePresentationTemplate(name, basePath / (dirname ?: name))
}
}
22 changes: 22 additions & 0 deletions reveal-kt/app/src/jvmMain/kotlin/commands/InitTemplate.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package dev.limebeck.application.commands

import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.parameters.arguments.argument
import com.github.ajalt.clikt.parameters.options.default
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.types.path
import dev.limebeck.application.templates.generatePresentationTemplate
import java.nio.file.Path
import kotlin.io.path.div

class InitTemplate : CliktCommand(name = "init", help = "Create new presentation from template") {
val name: String by argument(help = "Presentation name")
val basePath: Path by option(help = "Template dir")
.path(canBeDir = true, canBeFile = false)
.default(Path.of("."))
val dirname: String? by option()

override fun run() {
generatePresentationTemplate(name, basePath / (dirname ?: name))
}
}
72 changes: 72 additions & 0 deletions reveal-kt/app/src/jvmMain/kotlin/commands/RenderPdf.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package dev.limebeck.application.commands

import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.core.context
import com.github.ajalt.clikt.output.MordantHelpFormatter
import com.github.ajalt.clikt.parameters.arguments.argument
import com.github.ajalt.clikt.parameters.options.default
import com.github.ajalt.clikt.parameters.options.defaultLazy
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.types.file
import com.github.ajalt.clikt.parameters.types.int
import com.github.ajalt.clikt.parameters.types.path
import com.microsoft.playwright.CLI
import dev.limebeck.application.pdf.PdfRenderer
import dev.limebeck.application.server.Config
import dev.limebeck.application.server.ServerConfig
import dev.limebeck.application.server.runServer
import kotlinx.coroutines.runBlocking
import java.io.File
import java.nio.file.Path
import kotlin.concurrent.thread
import kotlin.io.path.pathString

class RenderPdf : CliktCommand(name = "pdf", help = "Render pdf from presentation") {
val port: Int by option("-p", "--port", help = "Port").int().default(8080)
val host: String by option("-h", "--host", help = "Host").default("0.0.0.0")
val basePath: Path? by option("-b", help = "Script dir").path()
val script: File by argument(help = "Script file").file(canBeDir = false, mustBeReadable = true)
val output: File by option("-o", "--output", help = "Output file")
.file(canBeDir = false, mustBeWritable = true)
.defaultLazy { File("./output.pdf") }

init {
context {
helpFormatter = {
MordantHelpFormatter(
showDefaultValues = true,
context = it
)
}
}
}

val renderer = PdfRenderer()

override fun run() {
runServer(
Config(
server = ServerConfig(host, port),
basePath = basePath?.pathString ?: script.parent,
script = script
),
background = false
)
runBlocking {
val outputData = renderer.render("http://$host:$port/?print-pdf")
output.writeBytes(outputData)
}
}
}

class InstallChrome : CliktCommand(name = "install-chrome", help = "Install chrome") {
override fun run() {
CLI.main(arrayOf("install", "chromium"))
}
}

class UninstallChrome : CliktCommand(name = "uninstall-chrome", help = "Uninstall chrome") {
override fun run() {
CLI.main(arrayOf("uninstall"))
}
}
45 changes: 45 additions & 0 deletions reveal-kt/app/src/jvmMain/kotlin/commands/RunServer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package dev.limebeck.application.commands

import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.core.context
import com.github.ajalt.clikt.output.MordantHelpFormatter
import com.github.ajalt.clikt.parameters.arguments.argument
import com.github.ajalt.clikt.parameters.options.default
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.types.file
import com.github.ajalt.clikt.parameters.types.int
import com.github.ajalt.clikt.parameters.types.path
import dev.limebeck.application.server.Config
import dev.limebeck.application.server.ServerConfig
import dev.limebeck.application.server.runServer
import java.io.File
import java.nio.file.Path
import kotlin.io.path.pathString

class RunServer : CliktCommand(name = "run", help = "Serve presentation with live-reload") {
val port: Int by option(help = "Port").int().default(8080)
val host: String by option(help = "Host").default("0.0.0.0")
val basePath: Path? by option(help = "Script dir").path()
val script: File by argument(help = "Script file").file(canBeDir = false, mustBeReadable = true)

init {
context {
helpFormatter = {
MordantHelpFormatter(
showDefaultValues = true,
context = it
)
}
}
}

override fun run() {
runServer(
Config(
server = ServerConfig(host, port),
basePath = basePath?.pathString ?: script.parent,
script = script
)
)
}
}
Loading

0 comments on commit 52b86e8

Please sign in to comment.