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

Overview

fluid-pdf

Maven Central 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.14.5")
}

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(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

generator.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.

HTML stream to PDF file

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.

PDF generation settings

generator.generate(
	source = PdfGenerationSource.Html("<strong>Hello world!</strong>"),
	settings = PdfGenerationSettings.default.copy(
		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

generator.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.

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

You might also like...
Location-history-viewer - Small compose-desktop app to view data from google's location history

Google Location History Takeout Viewer This application provides a minimalistic

AndroidArchitecture - An Implementation of Google Recommended New Android Architecture with Kotlin
AndroidArchitecture - An Implementation of Google Recommended New Android Architecture with Kotlin

Android Architecture An Implementation of Google Recommended New Android Archite

GBooks - A simple android app written in Kotlin to read books from the Google Book Api
GBooks - A simple android app written in Kotlin to read books from the Google Book Api

G-Books A simple android app written in Kotlin to read books from the Google Boo

Aurora Store: A Google Playstore Client
Aurora Store: A Google Playstore Client

Aurora Store: A Google Playstore Client Aurora Store is an unofficial, FOSS clie

A Kotlin multiplatform unit testing library inspired by / similar to Google Truth.

Truthish A testing API inspired by Google Truth but rewritten in Kotlin from the ground up, so it can be used in Kotlin multiplatform projects. For ex

A beautiful Fashion Store like Android App Mock built on Jetpack Compose with compose navigation, hilt, dark theme support and google's app architecture found on uplabs Here
A beautiful Fashion Store like Android App Mock built on Jetpack Compose with compose navigation, hilt, dark theme support and google's app architecture found on uplabs Here

A beautiful Fashion Store like Android App Mock built on Jetpack Compose with compose navigation, hilt, dark theme support and google's app architecture found on uplabs Here

:bouquet: An easy way to persist and run code block only as many times as necessary on Android.
:bouquet: An easy way to persist and run code block only as many times as necessary on Android.

Only 💐 An easy way to persist and run code block only as many times as necessary on Android. Download Gradle Add below codes to your root build.gradl

Easy Android camera integration, advanced features.

CameraViewEx This is an extended version of Google's cameraview library with better stability and many more features. CameraViewEx highly simplifies i

[Android Library] Get easy access to device information super fast, real quick
[Android Library] Get easy access to device information super fast, real quick

DeviceInfo-Sample Simple, single class wrapper to get device information from an android device. This library provides an easy way to access all the d

Releases(0.10.2)
  • 0.10.2(Dec 18, 2020)

    • Added PdfMetadata.documentId which allows specifying stable document IDs so that PDF output can be compared in unit tests
    • Kotlin 1.4.21
    • Kotlin Coroutines 1.4.2
    • Apache PDFBox 2.0.21
    Source code(tar.gz)
    Source code(zip)
  • 0.9.1(Jul 2, 2020)

    • Replaced PdfGenerationDestination parameter type with PdfGenerationOutput return type.
    • Allow specifying PDF metadata through PdfGenerationSettings.metadata.
    • ChromiumPdfGenerator.lazy is no longer suspending but truly lazy.
    • Wait for page load event before generating PDF.
    Source code(tar.gz)
    Source code(zip)
Owner
Marc Knaup
❤️ tech / startups / crypto / travel
Marc Knaup
Annotation based Android lint check generation

Intervention Annotation based Android lint check generation Generate custom Android lint checks and have lint warn you about code you may be dealing w

Fanis Veizis 25 Aug 18, 2022
Code generation of Kotlin DSL for AWS CDK

Code generation of Kotlin DSL for AWS CDK

Semantic Configuration 5 Dec 24, 2022
A CLI tool to convert multi-module Jetpack Compose compiler metrics into beautiful HTML reports

A CLI tool to convert multi-module Jetpack Compose compiler metrics into beautiful HTML reports 1. What are Jetpack Compose compiler metrics? The Comp

Jaya Surya Thotapalli 116 Jan 3, 2023
AdsManager - Easy way to implement Google Ads

AdsManager Easy way to implement Google Ads Implementaion: https://jitpack.io/#R

null 3 Jul 25, 2022
This program will read from your android application string.xml file and generate translated strings.xml files in your preferred languages using google sheet.

Localize your application content This program will read from your application string.xml file and generate translated strings.xml files in your prefe

DhiWise 4 Jul 29, 2022
UserLoc - A API call using Retrofit to obtain list of users details and show on UI in recycler view and google map

UserLoc This uses a API call using Retrofit to obtain list of users details and

Rohit Kumar 0 Jun 22, 2022
Easy lightweight SharedPreferences library for Android in Kotlin using delegated properties

Easy lightweight SharedPreferences library for Android in Kotlin using delegated properties Idea Delegated properties in Kotlin allow you to execute a

null 25 Dec 27, 2022
A simple library that can connect your autocomplete edittext to Google places api

Google Places AutoComplete EditText A simple library that can connect your autocomplete edittext to Google's places api Supporting Places AutoComplete

Mukesh Solanki 71 Dec 28, 2022
🔓 Kotlin version of the popular google/easypermissions wrapper library to simplify basic system permissions logic on Android M or higher.

EasyPermissions-ktx Kotlin version of the popular googlesample/easypermissions wrapper library to simplify basic system permissions logic on Android M

Madalin Valceleanu 326 Dec 23, 2022
Unofficial Actions on Google SDK for Kotlin and Java

Actions On Google Client Library This is a port of the official Node.js SDK to Kotlin. This can also be used from Java and any JVM language. Quick Fac

Ticketmaster® & Live Nation Entertainment® 118 Oct 3, 2022