https://github.com/fluidsonic/fluid-pdf
Easy PDF generation with HTML & CSS using Chromium or Google Chrome
https://github.com/fluidsonic/fluid-pdf
kotlin pdf pdf-converter pdf-generation
Last synced: about 2 months ago
JSON representation
Easy PDF generation with HTML & CSS using Chromium or Google Chrome
- Host: GitHub
- URL: https://github.com/fluidsonic/fluid-pdf
- Owner: fluidsonic
- License: apache-2.0
- Created: 2020-06-20T15:05:54.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2024-05-05T18:51:39.000Z (about 1 year ago)
- Last Synced: 2025-03-27T19:41:37.937Z (2 months ago)
- Topics: kotlin, pdf, pdf-converter, pdf-generation
- Language: Kotlin
- Homepage:
- Size: 472 KB
- Stars: 22
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
fluid-pdf
=========[](https://search.maven.org/artifact/io.fluidsonic.pdf/fluid-pdf)
[](https://github.com/JetBrains/kotlin/releases/v1.8.22)
[](https://kotlinlang.slack.com/messages/C7UDFSVT2/)Easy PDF generation with HTML & CSS using Chromium or Google Chrome
Installation
------------`build.gradle.kts`:
```kotlin
dependencies {
implementation("io.fluidsonic.pdf:fluid-pdf:0.20.0")
}
```Usage
-----## HTML file to PDF file
```kotlin
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()ChromiumPdfGenerator.launch(
binaryFile = Path.of("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome")
).use { generator ->
generator.generate(PdfGenerationSource.HtmlFile(sourceFile))
.writeTo(destinationFile)
}println("PDF has been generated at $destinationFile")
}
```- Use `ChromiumPdfGenerator.launch()` to launch a browser instance for PDF generation.
- Use `ChromiumPdfGenerator.lazy()` to launch the browser not immediately but automatically with the first PDF generation.
- Use `ChromiumPdfGenerator`'s `.close()` to shut down the browser when you are done generating PDFs.
- Use `PdfGenerator`'s `.generate()` to create any number of PDFs.
- Use `PdfGenerator` interface to hide implementation details (use of Chromium, `.close()`) as needed.## HTML string to PDF file
```kotlin
generator.generate(PdfGenerationSource.Html("Hello world!"))
.writeTo(PdfGenerationDestination.File(destinationFile))
```💡 Relative paths in HTML & CSS won't resolve. Using `` to specify the base path should help.
## HTML stream to PDF file
```kotlin
val sourceStream: InputStream = …generator.generate(PdfGenerationSource.HtmlStream(sourceStream))
.writeTo(PdfGenerationDestination.File(destinationFile))
```💡 Relative paths in HTML & CSS won't resolve. Using `` to specify the base path should help.
## PDF generation settings
```kotlin
generator.generate(
source = PdfGenerationSource.Html("Hello world!"),
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)
```## Output to stream
```kotlin
generator.generate(PdfGenerationSource.Html("Hello world!"))
.writeTo(outputStream)
```💡 Closing the output stream is the responsibility of the caller. It will not be closed automatically.
TO-DO
-----
Contributions welcome 🙏- Add unit tests.
- Add KDoc to all public API.
- Check if `.generate()` works well if used from multiple threads and document if that is the case.
- Add support for header & footer templates.License
-------Apache 2.0