Easy PDF generation with HTML & CSS using Chromium or Google Chrome
build.gradle.kts:
dependencies {
implementation("io.fluidsonic.pdf:fluid-pdf:0.30.0")
}import io.fluidsonic.pdf.*
import java.nio.file.*
suspend fun main() {
// TODO Change the binary file path to your local Chromium or Google Chrome installation.
val sourceFile = Path.of("input.html").toAbsolutePath()
val destinationFile = Path.of("output.pdf").toAbsolutePath()
coroutineScope {
val service = PdfGeneratorService.chromiumLauncher(
path = Path.of("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome")
)
service.startIn(this)
try {
service.generate(PdfGenerationSource.HtmlFile(sourceFile))
.writeTo(destinationFile)
}
finally {
service.stop()
}
}
println("PDF has been generated at $destinationFile")
}- Use
PdfGeneratorService.chromiumLauncher()to launch a browser instance. - Use
PdfGeneratorService.devTools()to connect to an existing or a remote browser instance. - Use
service.startIn(CoroutineScope)andservice.stop()to manage the service lifecycle. - Use
PdfGeneratorService.generate()to create any number of PDFs. - Use
PdfGeneratorinterface to hide service details, e.g. in dependency injection.
service.generate(PdfGenerationSource.Html("<strong>Hello world!</strong>"))
.writeTo(PdfGenerationDestination.File(destinationFile))💡 Relative paths in HTML & CSS won't resolve. Using <base href="…"> to specify the base path should help.
Paths in HTML & CSS only work with a Chromium instance on the same machine.
val sourceStream: InputStream = …
generator.generate(PdfGenerationSource.HtmlStream(sourceStream))
.writeTo(PdfGenerationDestination.File(destinationFile))💡 Relative paths in HTML & CSS won't resolve. Using <base href="…"> to specify the base path should help.
Paths in HTML & CSS only work with a Chromium instance on the same machine.
service.generate(
source = PdfGenerationSource.Html("<strong>Hello world!</strong>"),
settings = PdfGenerationSettings.default.copy(
encryption = PdfEncryption(
ownerPassword = "secret",
permissions = PdfPermissions.none.copy(
contentExtractionAllowed = true,
contentExtractionForAccessibilityAllowed = true,
printQuality = PdfPermissions.PrintQuality.high,
),
),
includeBackgrounds = false,
metadata = PdfMetadata(
title = "My PDF"
),
pageMargins = PdfMargins.cm(top = 2.0, right = 2.0, bottom = 1.0, left = 2.0),
pageOrientation = PdfOrientation.landscape,
pageSize = PdfSize.A5,
preferCssPageSize = false
)
)
.writeTo(destinationFile)service.generate(PdfGenerationSource.Html("<strong>Hello world!</strong>"))
.writeTo(outputStream)💡 Closing the output stream is the responsibility of the caller. It will not be closed automatically.
Contributions welcome 🙏
- Add unit tests.
- Add KDoc to all public API.
- Write Chromium launcher & DevTools WebSocket client with Kotlin & Ktor, then remove dependency on
com.github.kklisura.cdt. - Test if
.generate()works well with significant parallelism.
Apache 2.0