View all our code samples

Using HttpClient in Java

HttpClient is a class provided by the Java standard library that facilitates making HTTP requests and receiving HTTP responses. It offers a simple and flexible API for performing tasks such as sending GET, POST, PUT, and DELETE requests, handling headers and content, managing cookies, and configuring timeouts and other request parameters.

We've implemented a code sample that you can re-use to convert your HTML documents to PDF, JPG, PNG or WEBP using PDFShift and Java:

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.nio.file.Files;
import java.nio.file.Paths;

public class PDFConvert {

    public byte[] convert(String apiKey, String params, String endpoint) throws Exception {
        if (!endpoint.equals("pdf") && !endpoint.equals("png") && !endpoint.equals("jpg") && !endpoint.equals("webp")) {
            throw new Exception("Invalid endpoint");
        }

        HttpClient httpClient = HttpClients.createDefault();

        HttpPost request = new HttpPost("https://api.pdfshift.io/v3/convert/" + endpoint);
        request.setHeader("Content-Type", "application/json");
        request.setHeader("Authorization", "Basic " + java.util.Base64.getEncoder().encodeToString(("api:" + apiKey).getBytes()));

        StringEntity entity = new StringEntity(params);
        request.setEntity(entity);

        HttpResponse response = httpClient.execute(request);

        if (response.getStatusLine().getStatusCode() != 200) {
            throw new Exception("Http request failed with status code: " + response.getStatusLine().getStatusCode());
        }

        byte[] binary = EntityUtils.toByteArray(response.getEntity());

        if (params.contains("\"filename\"") || params.contains("\"webhook\"")) {
            return EntityUtils.toString(response.getEntity()).getBytes();
        }

        return binary;
    }
}

Here's how you can use the above code:

public static void main(String[] args) {
    try {
        String apiKey = "sk_XXXXXXXXXXXXXX";
        String params = "{\"source\": \"https://en.wikipedia.org/wiki/REST\"}";

        PDFConvert pdfConvert = new PDFConvert();
        byte[] binary = pdfConvert.convert(apiKey, params, "pdf");

        Files.write(Paths.get("result.pdf"), binary);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

We've tested this code with the latest version of HttpClient and it's ready to be used in your project.

But if you were to encounter any bugs or issues while running it (or if you want to suggest changes to improve the code), please contact us and we'll be happy to help you out.