html, pdf, java

 

We could use URLConnection to send data to our service but why reinvent the wheel when you can rely on the already proven experience of others. In this post we'll use The Apache HttpComponents™ to send data to HTML PDF API.

The Apache HttpComponents™ project is responsible for creating and maintaining a toolset of low level Java components focused on HTTP and associated protocols.

All examples are written for CLI but you can use these examples in different types of applications.

Before we move on, make sure you have Oracle Java JDK 8 installed, not Open JDK. If you're using Windows or Mac OSX you can easily download installer and follow the steps on screen. If you're using Ubuntu or Mint go to following page and follow the instructions.

After you've installed Java SDK, you can check version by running following command in your terminal:

$ java -version

You should get the similar output:

java version "1.8.0_66"
Java(TM) SE Runtime Environment (build 1.8.0_66-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.66-b17, mixed mode)

We'll use NetBeans IDE to create a new Project. We recommend you to install NetBeans bundle that has All supported technologies. If you don't have set up JAVA_HOME in your PATH you should get following warning.

To fix this press Exit button, open the terminal and type: 

update-java-alternatives -l | grep oracle

You will get list of all Java JDK installed

java-8-oracle 1072 /usr/lib/jvm/java-8-oracle

Copy path and edit <netbeans-IDE-installation>/etc/netbeans.conf and correct path to Java JDK:

netbeans_jdkhome="/usr/lib/jvm/java-8-oracle"

Now we are ready to create the new Project. Open NetBeans and select File - New Project - Maven - Java Application.

Netbeans Maven New Project

Click Next and  type Project name and click Finish.

Netbeas Maven Project name

We'll use Apache Maven to add dependencies and build our project. If you installed the NetBeans, as you might notice, Maven is already involved in its installation, so add following dependencies to your pom.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>hr.effectiva</groupId>
  <artifactId>HtmlPdfApiExamples</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>
  <dependencies>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpmime</artifactId>
        <version>4.3.1</version>
    </dependency>
  </dependencies>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>
  <name>HtmlPdfApiExamples</name>
</project>

To download dependencies right click on Dependencies in Netbeans project and choose Downlaod Declared Dependencies.

Maven download declared dependencies

How to use HTML to PDF API

You send data to HTML to PDF API via HTML POST request. To generate PDF, you can use one of the following methods:

  1. URL
  2. HTML file
  3. ZIP file
  4. HTML string

First you have to define parameters for the request. You can find the list of available parameters, as well as their description on HTML PDF API  documentation page.

You can download the Netbeans projects with all examples as a ZIP fileDon't forget to change the authentication token!!! 

Generate PDF from URL in Java

If you want to generate PDF file directly from URL, you must provide the 'url' parameter. In that case 'html' and 'file' parameters must not be set. In this example, you will send data by URL from remote HTML file and save result in PDF file.

Right click on Source Packages and add choose New - Java Class:

For Class Name type GeneratePdfByUrl and click Finish button.

Copy following code to GeneratePdfByUrl.java class:

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;

public class GeneratePdfByUrl {
    public static void main(String[] args) throws UnsupportedEncodingException, FileNotFoundException, IOException {
        // set data for request
        String apiUrl = "http://htmlpdfapi.com/api/v1/pdf";
        String url = "https://htmlpdfapi.com/examples/simple.html";
        
        // create HTTP POST
        HttpClient client = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost(apiUrl);
        
        // set HTTP header property
        post.setHeader("Authentication", "Token <your token>"); // Don't forget to change the token!!!
        
        // set HTTP POST parameters
        List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
        urlParameters.add(new BasicNameValuePair("url", url));
        post.setEntity(new UrlEncodedFormEntity(urlParameters));
        
        // get response stream and save to the file
        HttpResponse response = client.execute(post);
        if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
            InputStream responseStream = response.getEntity().getContent();
            FileOutputStream outputStream = new FileOutputStream("result.pdf");

            int bytesRead = -1;
            byte[] buffer = new byte[4096];
            while ((bytesRead = responseStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }

            // close streams
            outputStream.close();
            responseStream.close();
        }else{
            System.out.println("Error: " + response.getStatusLine());
        }
    }
}

We set custom header with the autentication token and url parameter on HttpPost object. Then we call execute method of HttpClient object to get HttpResponse. If response has status 200 we save PDF to file if not we print status line of response to Output console. If you have not set your token you will get the following error:

Error: HTTP/1.1 401 Unauthorized

To compile and run file right click on GeneratePdfByUrl.java and choose Run file or use shortcut Shift + F6:

You can find generated PDF in the root of your NetBeans project. If you don't know path to the root you can right click on your project and choose Properties.

In the Project Properties dialog click on Sources and you can see Project Folder.

Generate PDF from HTML file in Java

If you want to generate PDF file from file, you must provide the 'file' parameter. In that case 'html' and 'url' parameters must not be set. Repeat the same steps as in the previous example but for Class Name type GeneratePdfByHtml. You will need a example.html file in the root path of your project.

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.HttpClientBuilder;

public class GeneratePdfByFile {
    public static void main(String[] args) throws UnsupportedEncodingException, FileNotFoundException, IOException {
        // set data for request
        String apiUrl = "http://htmlpdfapi.com/api/v1/pdf";
        String fileName = "example.html";
        
        // create HTTP POST
        HttpClient client = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost(apiUrl);
        
        // set HTTP header property
        post.setHeader("Authentication", "Token <your token>"); // Don't forget to change the token!!!
        
        // set HTTP POST parameters
        File file = new File(fileName);
        MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
        entityBuilder.addPart("file", new FileBody(file));
        post.setEntity(entityBuilder.build());
        
        // get response stream and save to the file
        HttpResponse response = client.execute(post);
        if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
            InputStream responseStream = response.getEntity().getContent();
            FileOutputStream outputStream = new FileOutputStream("result.pdf");

            int bytesRead = -1;
            byte[] buffer = new byte[4096];
            while ((bytesRead = responseStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }

            // close streams
            outputStream.close();
            responseStream.close();
        }else{
            System.out.println("Error: " + response.getStatusLine());
        }
    }
}

Generate PDF from ZIP file in Java

If you want to generate PDF file from ZIP file, you must provide the 'file' parameter. In that case 'html' and 'url' parameters must not be set. Repeat the same steps as in the first example but for Class Name type GeneratePdfByZip​. You will need a example.zip file in the root path of your project.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.HttpClientBuilder;

public class GeneratePdfByZip {
    public static void main(String[] args) throws UnsupportedEncodingException, FileNotFoundException, IOException {
        // set data for request
        String apiUrl = "http://htmlpdfapi.com/api/v1/pdf";
        String fileName = "example.zip";
        
        // create input stream
        InputStream inputStream = new FileInputStream(fileName);
            
        // create HTTP POST
        HttpClient client = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost(apiUrl);
        
        // set HTTP header property
        post.setHeader("Authentication", "Token <your token>"); // Don't forget to change the token!!!
        
        // set HTTP POST parameters
        File file = new File(fileName);
        MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
        entityBuilder.addPart("file", new FileBody(file));
        post.setEntity(entityBuilder.build());
        
        // get response stream and save to the file
        HttpResponse response = client.execute(post);
        if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
            InputStream responseStream = response.getEntity().getContent();
            FileOutputStream outputStream = new FileOutputStream("result.pdf");

            int bytesRead = -1;
            byte[] buffer = new byte[4096];
            while ((bytesRead = responseStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }

            // close streams
            outputStream.close();
            responseStream.close();
        }else{
            System.out.println("Error: " + response.getStatusLine());
        }
    }
}

Generate PDF from string in Java

If you want to generate PDF file from HTML string, you must provide the 'html' parameter. In that case 'file' and 'url' parameters must not be set. Repeat the same steps as in the first example but for Class Name type GeneratePdfByHtml​.

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;

public class GeneratePdfByHtml {
    public static void main(String[] args) throws UnsupportedEncodingException, FileNotFoundException, IOException {
        // set data for request
        String apiUrl = "http://htmlpdfapi.com/api/v1/pdf";
        String html = "<!DOCTYPE html><html><head><meta charset=\"UTF-8\"></head><body>Hello world!!!</body></html>";
        
        // create HTTP POST
        HttpClient client = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost(apiUrl);
        
        // set HTTP header property
        post.setHeader("Authentication", "Token <your token>"); // Don't forget to change the token!!!
        
        // set HTTP POST parameters
        List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
        urlParameters.add(new BasicNameValuePair("html", html));
        post.setEntity(new UrlEncodedFormEntity(urlParameters));
        
        // get response stream and save to the file
        HttpResponse response = client.execute(post);
        if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
            InputStream responseStream = response.getEntity().getContent();
            FileOutputStream outputStream = new FileOutputStream("result.pdf");

            int bytesRead = -1;
            byte[] buffer = new byte[4096];
            while ((bytesRead = responseStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }

            // close streams
            outputStream.close();
            responseStream.close();
        }else{
            System.out.println("Error: " + response.getStatusLine());
        }
    }
}

Sending additional parameters

In all examples you can send additional parameters.

For URL and HTML string requests, you can send parameters:

urlParameters.add(new BasicNameValuePair("page_size", "A5"));
urlParameters.add(new BasicNameValuePair("margin_top", "20"));

For file and ZIP file requests you can send parameters:

entityBuilder.addTextBody("page_size", "A5");
entityBuilder.addTextBody("margin_top", "20");

As we said, you can find the list of available parameters on HTML PDF API documentation page.

That's it. You're done!

Please feel free to leave your comments below.

Download - Java examples for HTML to PDF API (ZIP)