Skip to main content

Java Example

Sign up for the basic plan for a free development API key. Please replace API_KEY in the code example with your own API key.

import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import java.net.URI;
import java.net.URLEncoder;
import java.io.BufferedWriter;
import java.io.FileWriter;

class HTMLtoZPLJava {

public static void main(String[] args) throws Exception {

// String API_KEY = "API_KEY"; /* Get from: https://rapidapi.com/dablabs/api/html-to-zpl */

String width = "4"; // inches
String height = "2"; // inches
String html = "<h1 style=\"font-size:40pt;margin:0.5in\">Hello World!<h1>";
html +="<p style=\"font-size:25pt;margin:0.8in\">ZPL label generated by <u>htmltozpl.com</u></p>";

String urlEncodedHtml = URLEncoder.encode(html, "UTF-8");

String data = "width=" + width + "&height=" + height + "&html=" + urlEncodedHtml;

HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder(
URI.create("https://html-to-zpl.p.rapidapi.com/html2zpl"))
.header("x-rapidapi-host", "html-to-zpl.p.rapidapi.com")
.header("x-rapidapi-key", API_KEY)
.header("content-type", "application/x-www-form-urlencoded")
.POST(BodyPublishers.ofString(data))
.build();

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

String zpl = response.body().toString();

// Write to Conosle
System.out.println(zpl);

// Write to file
BufferedWriter writer = new BufferedWriter(new FileWriter("java-html-to-zpl-example.zpl"));
writer.write(zpl.toCharArray());
writer.close();
}

}