fun-pdf

Kotlin wkhtmltopdf wrapper

License

License

Categories

Categories

PDF Data
GroupId

GroupId

id.jasoet
ArtifactId

ArtifactId

fun-pdf
Last Version

Last Version

1.0.3
Release Date

Release Date

Type

Type

jar
Description

Description

fun-pdf
Kotlin wkhtmltopdf wrapper
Project URL

Project URL

https://github.com/jasoet/fun-pdf
Source Code Management

Source Code Management

https://github.com/jasoet/fun-pdf

Download fun-pdf

How to add to project

<!-- https://jarcasting.com/artifacts/id.jasoet/fun-pdf/ -->
<dependency>
    <groupId>id.jasoet</groupId>
    <artifactId>fun-pdf</artifactId>
    <version>1.0.3</version>
</dependency>
// https://jarcasting.com/artifacts/id.jasoet/fun-pdf/
implementation 'id.jasoet:fun-pdf:1.0.3'
// https://jarcasting.com/artifacts/id.jasoet/fun-pdf/
implementation ("id.jasoet:fun-pdf:1.0.3")
'id.jasoet:fun-pdf:jar:1.0.3'
<dependency org="id.jasoet" name="fun-pdf" rev="1.0.3">
  <artifact name="fun-pdf" type="jar" />
</dependency>
@Grapes(
@Grab(group='id.jasoet', module='fun-pdf', version='1.0.3')
)
libraryDependencies += "id.jasoet" % "fun-pdf" % "1.0.3"
[id.jasoet/fun-pdf "1.0.3"]

Dependencies

compile (8)

Group / Artifact Type Version
org.jetbrains.kotlin : kotlin-stdlib-jdk8 jar 1.2.60
org.jetbrains.kotlinx : kotlinx-coroutines-core jar 0.24.0
commons-io : commons-io jar 2.5
ch.qos.logback : logback-core jar 1.2.3
ch.qos.logback : logback-classic jar 1.2.3
io.arrow-kt : arrow-core jar 0.7.2
org.jetbrains.kotlin : kotlin-reflect jar 1.2.60
id.jasoet : fun-kommand jar 1.0.3

Project Modules

There are no modules declared in this project.

Html to Pdf Converter

Build Status codecov Download

Library to convert Html to Pdf using wkhtmltopdf library. This library only tested on Linux and macOS

Features

  • Define custom executable location, if not found will find executable by running which wkthtmltopdf
  • Accept File, String, URL or InputStream as standard input.
  • Redirect standard output to File or OutputStream (including System.out).
  • Accept Map as environment variable.
  • Return BufferedInputStream if output is not redirected, null if otherwise.
  • InputStream from a command will be easily to be piped (as input) to other command.
  • Available on jcenter()

Install wkhtmltopdf

wkhtmltopdf library can be downloaded from here

Linux

sudo apt-get update -q
sudo apt-get install -yq wget xfonts-base xfonts-75dpi curl
curl -sLO https://downloads.wkhtmltopdf.org/0.12/0.12.5/wkhtmltox_0.12.5-1.xenial_amd64.deb
sudo dpkg -i wkhtmltox_0.12.5-1.xenial_amd64.deb

macOS

brew cask install wkhtmltopdf

Gradle

Add maven repository url

repositories {
    jcenter()
}

Add dependency

compile 'id.jasoet:fun-pdf:<version>'

Usage

Configurable and Cacheable

val pdf by lazy {
        HtmlToPdf(executable = "/usr/bin/wkhtmltopdf") {
            orientation(PageOrientation.LANDSCAPE)
            pageSize("Letter")
            marginTop("1in")
            marginBottom("1in")
            marginLeft("1in")
            marginRight("1in")
        }
}

Html String as input

val htmlString = "<html><body><h1>Hello World</h1></body></html>"

// convert and save if to file
val outputFile = Paths.get("/home/jasoet/document/destination.pdf").toFile()
val inputStream = pdf.convert(input = htmlString,output = outputFile) // will always return null if output is redirected

// Convert and return InputStream
val inputStream = pdf.convert(input = htmlString) // InputStream is open and ready to use

File as input

val file = File("/home/jasoet/source.html")

// convert and save if to file
val outputFile = Paths.get("/home/jasoet/document/destination.pdf").toFile()
val inputStream = pdf.convert(input = file,output = outputFile) // will always return null if output is redirected

// Convert and return InputStream
val inputStream = pdf.convert(input = file) // InputStream is open and ready to use

Url as input

val url = URL("https://www.google.com")

// convert and save if to file
val outputFile = Paths.get("/home/jasoet/document/destination.pdf").toFile()
val inputStream = pdf.convert(input = url,output = outputFile) // will always return null if output is redirected

// Convert and return InputStream
val inputStream = pdf.convert(input = url) // InputStream is open and ready to use

InputStream as Input

// get input stream from other executable
val inputStream = listOf("ssh", "[email protected]","\"cat /home/jasoet/remotefile.html\"").execute()

// Save it to file
val outputFile = Paths.get("/home/jasoet/document/destination.pdf").toFile()
val inputStream = pdf.convert(input = url,output = outputFile) // will always return null if output is redirected

Using extensions

String extension

val outputFile = Paths.get("/home/jasoet/document/destination.pdf").toFile()
val inputStream = "<html><body><h1>Hello World</h1></body></html>".convertToPdf(output = outputFile) // will always return null if output is redirected

// Convert and return InputStream
val inputStream = "<html><body><h1>Hello World</h1></body></html>".convertToPdf {
                    orientation(PageOrientation.LANDSCAPE)
                    pageSize("Letter")
                    marginTop("1in")
                    marginBottom("1in")
                    marginLeft("1in")
                    marginRight("1in")
               }
// InputStream is open and ready to use

File extension

val outputFile = Paths.get("/home/jasoet/document/destination.pdf").toFile()
val inputStream = File("/home/jasoet/source.html").convertToPdf(output = outputFile) // will always return null if output is redirected

// Convert and return InputStream
val inputStream = File("/home/jasoet/source.html").convertToPdf {
                    orientation(PageOrientation.LANDSCAPE)
                    pageSize("Letter")
                    marginTop("1in")
                    marginBottom("1in")
                    marginLeft("1in")
                    marginRight("1in")
               }
// InputStream is open and ready to use

Url extension

// convert and save if to file
val outputFile = Paths.get("/home/jasoet/document/destination.pdf").toFile()
val inputStream = URL("https://www.google.com").convertToPdf(output = outputFile) // will always return null if output is redirected

// Convert and return InputStream
val inputStream = URL("https://www.google.com").convertToPdf {
                    orientation(PageOrientation.LANDSCAPE)
                    pageSize("Letter")
                    marginTop("1in")
                    marginBottom("1in")
                    marginLeft("1in")
                    marginRight("1in")
               } 
// InputStream is open and ready to use

InputStream as Input

val outputFile = Paths.get("/home/jasoet/document/destination.pdf").toFile()

// get input stream from other executable and save if to file
val inputStream = listOf("ssh", "[email protected]","\"cat /home/jasoet/remotefile.html\"").execute()
                  .convertToPdf(output = outputFile) {
                    orientation(PageOrientation.LANDSCAPE)
                    pageSize("Letter")
                    marginTop("1in")
                    marginBottom("1in")
                    marginLeft("1in")
                    marginRight("1in")
               } 

See unit test files.

Versions

Version
1.0.3