ProductPromotion
Logo

Kotlin

made by https://0x3d.site

GitHub - fluidsonic/fluid-pdf: Easy PDF generation with HTML & CSS using Chromium or Google Chrome
Easy PDF generation with HTML & CSS using Chromium or Google Chrome - fluidsonic/fluid-pdf
Visit Site

GitHub - fluidsonic/fluid-pdf: Easy PDF generation with HTML & CSS using Chromium or Google Chrome

GitHub - fluidsonic/fluid-pdf: Easy PDF generation with HTML & CSS using Chromium or Google Chrome

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

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

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory