HTML-to-PDF with Java


Code Examples

DocRaptor Java Native Client Library

This is a maven package for using DocRaptor API to convert HTML to PDF and XLSX.


To install the API client library to your local Maven repository, simply execute:

mvn install

To deploy it to a remote Maven repository instead, configure the settings of the repository and execute:

mvn deploy

After the client libarary is installed/deployed, you can use it in your Maven project by adding the following to your pom.xml:



See examples for runnable examples with file output, error handling, etc.

import com.docraptor.*;

public class Sync {
  public static void main(String[] args) throws Exception {
    DocApi docraptor = new DocApi();
    ApiClient client = docraptor.getApiClient();

    Doc doc = new Doc();
    doc.setTest(true);                                                   // test documents are free but watermarked
    doc.setDocumentContent("<html><body>Hello World</body></html>");     // supply content directly
    // doc.setDocumentUrl(""); // or use a url
    doc.setDocumentType(Doc.DocumentTypeEnum.PDF);                       // PDF or XLS or XLSX
    doc.setName("docraptor-java.pdf");                                   // help you find a document later
    doc.setJavascript(true);                                             // enable JavaScript processing
    // prince_options = new PrinceOptions();
    // doc.setPrinceOptions(prince_options);
    // prince_options.setMedia("screen");                                // use screen styles instead of print styles
    // prince_options.setBaseurl("")                     // pretend URL when using document_content

Docs created like this are limited to 60 seconds to render, check out the async example which allows 10 minutes.

We have guides for doing some of the common things:

Converting HTML into PDF and XLS documents with Java is fast and painless with DocRaptor. Just POST to our Docs API with your HTML content and some required parameters, either as a form serialized string or a JSON object.

Below you’ll find complete examples for Java, but our API Reference Guide has all the details about the complete workings of the Docs API.


With DocRaptor, your PDF content is HTML and its style is CSS - things you already know! Even still, it can be difficult to get your PDF to look just right. Check out our Style and Formatting Documentation for everything related getting a perfectly styled PDF!