--- layout: page title: Kotlin - HTML to Image Example nav_title: Kotlin parent: Example code permalink: /example-code/kotlin/ description: >- Convert HTML to an image (png, jpg or webp) with Kotlin + the HTML/CSS to Image API. Renders exactly like Google Chrome. --- {% include intro.md language="Kotlin" %} This example uses the built-in Java 11+ `HttpClient` from Kotlin and [`kotlinx.serialization`](https://github.com/Kotlin/kotlinx.serialization) to encode the JSON request body. Add the JSON serialization dependency: ```kotlin dependencies { implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.3") } ``` ```kotlin 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 "