Giter VIP home page Giter VIP logo

fun-pdf's Introduction

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.

fun-pdf's People

Contributors

jasoet avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.