Giter VIP home page Giter VIP logo

htmltopdf-java's Introduction

Overview

This project is based on wkhtmltopdf, which converts HTML documents to PDF. Access to wkhtmltopdf is performed via JNA, exposed through a Java-friendly layer.

Get it

Gradle:

compile 'io.woo:htmltopdf:1.0.8'

Maven:

<dependency>
  <groupId>io.woo</groupId>
  <artifactId>htmltopdf</artifactId>
  <version>1.0.8</version>
  <scope>compile</scope>
</dependency>

Getting started

The following examples should be sufficient to get you started, however there are many more options discoverable by looking into the methods of HtmlToPdf and HtmlToPdfObject.

Saving HTML as a PDF file

boolean success = HtmlToPdf.create()
    .object(HtmlToPdfObject.forHtml("<p><em>Apples</em>, not oranges</p>"))
    .convert("/path/to/file.pdf");

Saving a webpage from URL as a PDF file

boolean success = HtmlToPdf.create()
    .object(HtmlToPdfObject.forUrl("https://github.com/wooio/htmltopdf-java"))
    .convert("/path/to/file.pdf");

Saving multiple objects as a PDF file

boolean success = HtmlToPdf.create()
    .object(HtmlToPdfObject.forUrl("https://github.com/wooio/htmltopdf-java"))
    .object(HtmlToPdfObject.forHtml("<p>This is the second object...</p>"))
    // ...
    .convert("/path/to/file.pdf");

Converting to InputStream (instead of saving as file)

Converting to an InputStream would be useful if you intend on returning the resulting PDF document as an HTTP response or adding it as an email attachment

HtmlToPdf htmlToPdf = HtmlToPdf.create()
    // ...
    .object(HtmlToPdfObject.forUrl("https://github.com/wooio/htmltopdf-java"));

try (InputStream in = htmlToPdf.convert()) {
    // "in" has PDF bytes loaded
} catch (HtmlToPdfException e) {
    // HtmlToPdfException is a RuntimeException, thus you are not required to
    // catch it in this scope. It is thrown when the conversion fails
    // for any reason.
}

Concurrency limitations

While the library is thread-safe, it unfortunately cannot perform conversions concurrently. Because wkhtmltopdf uses Qt behind the scenes to render webpages, there is a single thread which performs such rendering across a single process. Therefore, at this point, it is only possible to perform one conversion at the same time per process.

Troubleshooting

Missing native dependencies

If you get the following exception:

java.lang.UnsatisfiedLinkError: Unable to load library '/tmp/io.woo.htmltopdf/wkhtmltox/0.12.5/libwkhtmltox.so': Native library (tmp/io.woo.htmltopdf/wkhtmltox/0.12.5/libwkhtmltox.so) not found in resource path

Then that likely means that one of the native dependencies of wkhtmltopdf is not met. It might be worth checking that the following packages are installed:

  • libc6 (or glibc)
  • libx11
  • libxext
  • libxrender
  • libstdc++
  • libssl1.0
  • freetype
  • fontconfig

htmltopdf-java's People

Contributors

benbarkay avatar dariosilva 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.