fluid-pdf
0.32.2indexedFacilitates PDF generation from HTML & CSS using Chromium or Google Chrome, supporting various input methods and customizable settings for encryption, metadata, page orientation, and size.
Facilitates PDF generation from HTML & CSS using Chromium or Google Chrome, supporting various input methods and customizable settings for encryption, metadata, page orientation, and size.
Easy PDF generation with HTML & CSS using Chromium or Google Chrome
build.gradle.kts:
dependencies {
implementation("io.fluidsonic.pdf:fluid-pdf:0.32.2")
}
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.
sourceFile = Path.of().toAbsolutePath()
destinationFile = Path.of().toAbsolutePath()
coroutineScope {
service = PdfGeneratorService.chromiumLauncher(
path = Path.of()
)
service.startIn()
{
service.generate(PdfGenerationSource.HtmlFile(sourceFile))
.writeTo(destinationFile)
}
{
service.stop()
}
}
println()
}
PdfGeneratorService.chromiumLauncher() to launch a browser instance.PdfGeneratorService.devTools() to connect to an existing or a remote browser instance.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 =
)
)
.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 🙏
com.github.kklisura.cdt..generate() works well with significant parallelism.Apache 2.0
service.startIn(CoroutineScope) and service.stop() to manage the service lifecycle.PdfGeneratorService.generate() to create any number of PDFs.PdfGenerator interface to hide service details, e.g. in dependency injection.Surfaced from shared tags and platforms — no rankings paid for.