Skip to main content

Java

Example of image generation using Java.

The Canvasflare API allows the creation of images from HTML and CSS. Below is an example of how to use the API in Java to generate an image.

Rendering an Image

To render an image, send a POST request to https://api.canvasflare.com/render with the necessary parameters.

Parameters

  • html - The HTML content you wish to render, e.g., <div>Your content</div>.
  • css - The CSS styling for your rendered image, e.g., .h1 { color: red }.
  • device_scale - Modifies the pixel density of the screenshot.
  • viewport_width - The width of the Chrome viewport.
  • viewport_height - The height of the Chrome viewport.
  • viewport_device - Emulates a given device's metrics and user agent.
  • viewport_has_touch - Indicates whether the viewport supports touch events.
  • viewport_landscape - Indicates whether the viewport is in landscape mode.
  • viewport_mobile - Specifies whether the meta viewport tag is considered.
  • full_page - If set to true screenshots entire webpage.
  • selector - A CSS selector corresponding to an element in the HTML.
  • ms_delay - Additional delay to ensure completion of JavaScript execution.
  • user_agent - An user agent for the request.

Example Request in Java

To make a request, you can use Java's HttpClient and HttpRequest (introduced in Java 11) to send HTTP requests:

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpHeaders;
import java.util.HashMap;
import java.util.Map;
import com.google.gson.Gson; // Gson library is used for JSON serialization

public class CanvasflareApiExample {

public static void main(String[] args) {
var client = HttpClient.newHttpClient();
var gson = new Gson();

Map<String, Object> data = new HashMap<>();
data.put("html", "<div>This is something</div>");
data.put("css", ".h1 { color: red }");
data.put("device_scale", 2);
data.put("viewport_width", 640);
data.put("viewport_height", 480);
data.put("ms_delay", 500);

var request = HttpRequest.newBuilder()
.uri(URI.create("https://api.canvasflare.com/render?api_key=YOUR_API_KEY"))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(gson.toJson(data)))
.build();

try {
var response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println("Image created: " + response.body());
} catch (Exception e) {
e.printStackTrace();
}
}
}

Response

The API response will return a URL to the generated image that you can download or display.

Make Your Image URL Downloadable

You can add the parameter ?dl=true to the image URL to make the browser download the image automatically. For example:

https://api.canvasflare.com/image/8d063c49-3f15-46c1-91c7-9cf61e6758c6?dl=true

Java Version

Ensure you have Java 11 or later installed, as this example uses java.net.http.HttpClient and java.net.http.HttpRequest, which are available from Java 11 onwards. Also, include the Gson library in your project dependencies for JSON handling. You can add Gson to your project using Maven or Gradle dependency management.