Skip to main content

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

Convert a PDF file to ZPL using PHP

Below is a code sample that can be used to convert a PDF file to ZPL:

<?php
function callPdfToZplAPI($width, $height, $filename) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_URL, "https://html-to-zpl.p.rapidapi.com/pdf2zpl");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_MAXREDIRS, 10);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);

curl_setopt($curl, CURLOPT_HTTPHEADER, [
"X-RapidAPI-Host: html-to-zpl.p.rapidapi.com",
"X-RapidAPI-Key: API_KEY",
"content-type: application/json"
]);

$pdfRaw = file_get_contents($filename);
$pdfBase64 = base64_encode($pdfRaw);

$data = [
"width" => $width,
"height" => $height,
"pdfBase64" => $pdfBase64
];
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));

$result = curl_exec($curl);
curl_close($curl);

return $result;
}

$zpl = callPdfToZplAPI(4, 6, "test.pdf");
echo $zpl;

Convert HTML to ZPL using PHP

Sample code to convert HTML to ZPL using PHP:

<?php
function callHtmlToZplAPI($width, $height, $html)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_URL, "https://html-to-zpl.p.rapidapi.com/html2zpl");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_MAXREDIRS, 10);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);

curl_setopt($curl, CURLOPT_HTTPHEADER, [
"X-RapidAPI-Host: html-to-zpl.p.rapidapi.com",
"X-RapidAPI-Key: API_KEY",
"content-type: application/json"
]);

$data = [
"width" => $width,
"height" => $height,
"html" => $html
];
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));

$result = curl_exec($curl);
curl_close($curl);

return $result;
}

$html = "<div style='width: 4in; height: 6in; padding: 1in; box-sizing: border-box; text-align: center;'>";
$html .= " <h1>Hello World!<h1>";
$html .= " <h2>This label was generated by the htmltozpl.com API</h2>";
$html .= "</div>";

$zpl = callHtmlToZplAPI(4, 6, $html);
echo $zpl;