对 wkhtmltopdf 的调用功能进行包装,方便简化使用

rsanders-relias e5cb99b4d8 Fix for issue where wkhtmltopdf returns 1 for cases where the PDF is generated by some assets are missing. For some users that case is acceptable. 6 éve
deploy 11f5d87bcb setting up travis to deploy only on master 6 éve
src e5cb99b4d8 Fix for issue where wkhtmltopdf returns 1 for cases where the PDF is generated by some assets are missing. For some users that case is acceptable. 6 éve
.gitignore 4e2e512b5a Fix compile on current java 10 éve
.travis.yml d5498943b5 new release with travis-ci and mvn central 6 éve
LICENSE 5c32296c22 Initial commit 11 éve
README.md e5cb99b4d8 Fix for issue where wkhtmltopdf returns 1 for cases where the PDF is generated by some assets are missing. For some users that case is acceptable. 6 éve
pom.xml 32cc963d8b new release 6 éve

README.md

Java WkHtmlToPdf Wrapper Build Status

A Java based wrapper for the wkhtmltopdf command line tool. As the name implies, it uses WebKit to convert HTML documents to PDFs.

Requirements

wkhtmltopdf must be installed and working on your system.

Wrapper project dependency

Make sure you have Java Wrapper dependency added to your project.

If you are using Gradle/Maven, see example below:

Gradle

In your build.gradle:

	dependencies {
		compile 'com.github.jhonnymertz:java-wkhtmltopdf-wrapper:1.1.8-RELEASE'
	}
Maven

In your pom.xml:

	<dependency>
    <groupId>com.github.jhonnymertz</groupId>
    <artifactId>java-wkhtmltopdf-wrapper</artifactId>
    <version>1.1.8-RELEASE</version>
  </dependency>

Usage

Pdf pdf = new Pdf();

pdf.addPageFromString("<html><head><meta charset=\"utf-8\"></head><h1>Müller</h1></html>");
pdf.addPageFromUrl("http://www.google.com");

// Add a Table of Contents
pdf.addToc();

// The `wkhtmltopdf` shell command accepts different types of options such as global, page, headers and footers, and toc. Please see `wkhtmltopdf -H` for a full explanation.
// All options are passed as array, for example:
pdf.addParam(new Param("--no-footer-line"), new Param("--header-html", "file:///header.html"));
pdf.addParam(new Param("--enable-javascript"));

// Add styling for Table of Contents
pdf.addTocParam(new Param("--xsl-style-sheet", "my_toc.xsl"));

// Save the PDF
pdf.saveAs("output.pdf");

Xvfb Support

XvfbConfig xc = new XvfbConfig();
xc.addParams(new Param("--auto-servernum"), new Param("--server-num=1"));

WrapperConfig wc = new WrapperConfig();
wc.setXvfbConfig(xc);

Pdf pdf = new Pdf(wc);
pdf.addPageFromUrl("http://www.google.com");

pdf.saveAs("output.pdf");

wkhtmltopdf exit codes

wkhtmltopdf may return non-zero exit codes to denote warnings, you can now set the Pdf object to allow this:


Pdf pdf = new Pdf(wc);
pdf.addPageFromUrl("http://www.google.com");

pdf.setAllowMissingAssets();
// or:  
pdf.setSuccessValues(Arrays.asList(0, 1));

pdf.saveAs("output.pdf");

License

This project is available under MIT Licence.