Link Search Menu Expand Document

Java: HTML/CSS to Image

Generate Images and PDFs with Java. Renders exactly like Google Chrome.

Render PNG, JPG, or WebP images + PDFs with Java from HTML/CSS or reusable dynamic templates.

Live demo Get an API Key


Generating images with Java

  1. The API takes your HTML/CSS and runs it inside a real instance of Google Chrome to convert your HTML into an image.
  2. Use Java to send the API your HTML/CSS.
  3. You’ll get back JSON with the URL to your generated image.

For more details on how this works, see Creating an image.

Example API response:

{
  "url": "https://hcti.io/v1/image/be4c5118-fe19-462b-a49e-48cf72697a9d",
  "id": "be4c5118-fe19-462b-a49e-48cf72697a9d"
}

Image generated with Java. Convert HTML to an image using Java.

Authentication with Java

The API uses HTTP Basic authentication.

Your username is your User ID and your password is your API Key. Both of these are available from the dashboard. The Java code sample demonstrates how to authenticate your request.

You can sign up for a free API key to get started.

Free API Key for Java


Java example code

This Java code example sends an HTTP POST to the https://hcti.io/v1/image API to convert your HTML/CSS to an image.

Java includes an HTTP client in the standard library, but not a JSON encoder. This example uses the built-in Java 11+ HttpClient with Jackson to encode the JSON request body.

Add Jackson to your project:

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.17.2</version>
</dependency>
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Map;

public class HctiExample {
    private static final String USER_ID = "your-user-id";
    private static final String API_KEY = "your-api-key";

    public static void main(String[] args) throws IOException, InterruptedException {
        Map<String, String> payload = Map.of(
            "html", "<div class='box'>Java ✅</div>",
            "css", ".box { border: 4px solid #03B875; padding: 20px; font-family: Roboto, sans-serif; }",
            "google_fonts", "Roboto"
        );
        String body = new ObjectMapper().writeValueAsString(payload);

        String auth = Base64.getEncoder().encodeToString(
            (USER_ID + ":" + API_KEY).getBytes(StandardCharsets.UTF_8)
        );

        HttpRequest request = HttpRequest.newBuilder()
            .uri(URI.create("https://hcti.io/v1/image"))
            .header("Authorization", "Basic " + auth)
            .header("Content-Type", "application/json")
            .POST(HttpRequest.BodyPublishers.ofString(body))
            .build();

        HttpClient client = HttpClient.newHttpClient();
        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

        if (response.statusCode() < 200 || response.statusCode() >= 300) {
            throw new RuntimeException("Request failed: " + response.statusCode() + " " + response.body());
        }

        System.out.println(response.body());
        // {"url":"https://hcti.io/v1/image/1113184e-419f-49f1-b231-2069942a186f"}
    }
}

Need help?

Talk to a human. Please email us support@htmlcsstoimage.com with any questions and we’ll gladly help you get started.


Back to top

Built with extensive integration tests and serious care for developer happiness.
© 2018-2026 Code Happy, LLC.

Page last modified: Jul 3 2026 at 01:46 PM.

Edit this page on GitHub.