forked from phillip055/ktorio.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildpdf.kt
executable file
·34 lines (28 loc) · 893 Bytes
/
buildpdf.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env kscript
import java.net.*
// https://builds.wkhtmltopdf.org/0.12.6-dev/
fun main(args: Array<String>) {
val pages = downloadText("http://127.0.0.1:4000/pages.txt").lines().map { it.trim() }.filter { it.isNotBlank() }
val wargs = buildList<String> {
add("-l")
add("--disable-javascript")
add("--print-media-type")
add("toc")
for (page in pages) {
add(page)
}
add("ktor.pdf")
}
exec("wkhtmltopdf", *wargs.toTypedArray())
}
fun downloadText(url: String): String {
return URL(url).openStream().readBytes().toString(Charsets.UTF_8)
}
fun exec(process: String, vararg args: String) {
val pb = ProcessBuilder(process, *args).inheritIO()
val p = pb.start()
p.waitFor()
}
fun <T> buildList(callback: ArrayList<T>.() -> Unit): List<T> {
return arrayListOf<T>().apply(callback)
}