Skip to content
HTML/CSS to ImageDocs

Generate PNG, JPG, WebP, and PDF output with Kotlin, using HTML/CSS or reusable templates rendered in Google Chrome.

Generating images with Kotlin

  1. Send your HTML/CSS to the API.
  2. The API renders it in Google Chrome.
  3. Read the generated image URL from the JSON response.

See Creating an image for request parameters. A response includes the image's url and id.

Example image generated from HTML with Kotlin

Authentication with Kotlin

Use HTTP Basic authentication with your API ID as the username and API key as the password. Find both in the dashboard. Keep credentials in server-side configuration or environment variables.

Kotlin example code

The example sends a POST request to https://hcti.io/v1/image. Image creation requires images:create. See authentication and API keys for scoped credentials and access errors.

This example uses the built-in Java 11+ HttpClient from Kotlin and kotlinx.serialization to encode the JSON request body.

Add the JSON serialization dependency:

dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.3")
}
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
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
fun main() {
val userId = "your-user-id"
val apiKey = "your-api-key"
val payload = mapOf(
"html" to "<div class='box'>Kotlin ✅</div>",
"css" to ".box { border: 4px solid #03B875; padding: 20px; font-family: Roboto, sans-serif; }",
"google_fonts" to "Roboto"
)
val body = Json.encodeToString(payload)
val auth = Base64.getEncoder().encodeToString(
"$userId:$apiKey".toByteArray(StandardCharsets.UTF_8)
)
val 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()
val response = HttpClient.newHttpClient()
.send(request, HttpResponse.BodyHandlers.ofString())
if (response.statusCode() !in 200..299) {
error("Request failed: ${response.statusCode()} ${response.body()}")
}
println(response.body())
// {"url":"https://hcti.io/v1/image/1113184e-419f-49f1-b231-2069942a186f"}
}

Need help?

Talk to a human. Email support@htmlcsstoimage.com and we’ll help you get started.