Popularity
0.5
Stable
Activity
3.1
Stable
0
1
0

Programming language: Kotlin
Tags: Misc    
Latest version: v0.9.0

fluid-pdf alternatives and similar libraries

Based on the "Misc" category

Do you think we are missing an alternative of fluid-pdf or a related project?

Add another 'Misc' Library

README

fluid-pdf

Maven Central JCenter Kotlin #fluid-libraries Slack Channel

Easy PDF generation with HTML & CSS using Chromium or Google Chrome

Installation

build.gradle.kts:

dependencies {
    implementation("io.fluidsonic.pdf:fluid-pdf:0.9.0")
}

Usage

HTML file to PDF file

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(
            source = PdfGenerationSource.HtmlFile(sourceFile),
            destination = PdfGenerationDestination.File(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

generator.generate(
    source = PdfGenerationSource.Html("<strong>Hello world!</strong>"),
    destination = PdfGenerationDestination.File(destinationFile)
)

💡 Relative paths in HTML & CSS won't resolve. Using <base href="…"> to specify the base path should help.

HTML stream to PDF file

val sourceStream: InputStream = …

generator.generate(
    source = PdfGenerationSource.HtmlStream(sourceStream),
    destination = PdfGenerationDestination.File(destinationFile)
)

💡 Relative paths in HTML & CSS won't resolve. Using <base href="…"> to specify the base path should help.

PDF generation settings

generator.generate(
    source = PdfGenerationSource.Html("<strong>Hello world!</strong>"),
    destination = PdfGenerationDestination.File(destinationFile),
    settings = PdfGenerationSettings.default.copy(
        includeBackgrounds = false,
        pageMargins = PdfMargins.cm(top = 2.0, right = 2.0, bottom = 1.0, left = 2.0),
        pageOrientation = PdfOrientation.landscape,
        pageSize = PdfSize.A5,
        preferCssPageSize = false
    )
)

Output to stream

generator.generate(
    source = PdfGenerationSource.Html("<strong>Hello world!</strong>"),
    destination = PdfGenerationDestination.File(outputStream)
)

💡 Closing the destination 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


*Note that all licence references and agreements mentioned in the fluid-pdf README section above are relevant to that project's source code only.