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.

Convert PDF to ZPL using Java

This example loads the file label.pdf in the current directory, converts it to Base64, and then sends it to the API server.

The API returns the ZPL label in the response body. It is then written to the console and also saved to the local file java-html-to-zpl-example.zpl.

import java.net.HttpURLConnection;
import java.net.URL;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.ByteArrayOutputStream;
import java.util.Base64;
import java.nio.file.Files;
import java.nio.file.Paths;

public class PDFtoZPLJava {

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

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

// Load label.pdf and convert to base64
byte[] pdfBytes = Files.readAllBytes(Paths.get("label.pdf"));
String pdfBase64 = Base64.getEncoder().encodeToString(pdfBytes);

// Raw JSON string:
// In production code you should use a JSON library
String json = "{\"width\":4, \"height\":6, \"pages\":\"all\", \"pdfBase64\":\"" + pdfBase64 + "\"}";

URL url = new URL("https://html-to-zpl.p.rapidapi.com/pdf2zpl");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("x-rapidapi-key", API_KEY);
conn.setRequestProperty("content-type", "application/json");
conn.setDoOutput(true);

// Send JSON body
try (OutputStream os = conn.getOutputStream()) {
byte[] input = json.getBytes("UTF-8");
os.write(input);
}

// Read response body
int responseCode = conn.getResponseCode();
InputStream responseStream = (responseCode >= 200 && responseCode < 300)
? conn.getInputStream()
: conn.getErrorStream();

String str = "";
if (responseStream != null) {
try (InputStream is = responseStream;
ByteArrayOutputStream baos = new ByteArrayOutputStream()) {

byte[] buffer = new byte[8192];
int len;
while ((len = is.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
str = new String(baos.toByteArray(), "UTF-8");
}
}

System.out.println(str);

System.out.println("Writing ZPL to java-html-to-zpl-example.zpl");
BufferedWriter writer = new BufferedWriter(new FileWriter("java-html-to-zpl-example.zpl"));
writer.write(str.toCharArray());
writer.close();

conn.disconnect();
}
}

Convert HTML to ZPL using Java

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-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();
}

}