Skip to main content

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

Convert PDF to ZPL using C#

using System;
using System.IO;
using System.Net;
using System.Text;

class Program
{
static void Main(string[] args)
{
string filePath = "test.pdf";

try
{
string result = ConvertFileToZpl(filePath);
Console.WriteLine("Response from server: " + result);
}
catch (Exception ex)
{
Console.WriteLine("An error occurred: " + ex.Message);
}

Console.Write("Press any key to exit...");
Console.ReadKey();
}

public static string ConvertFileToZpl(string filePath)
{
// Read the file and convert it to Base64
byte[] fileBytes = File.ReadAllBytes(filePath);
string fileBase64 = Convert.ToBase64String(fileBytes);

// Prepare the JSON data
string json = $@"
{{
""width"": 4,
""height"": 6,
""pdfBase64"": ""{fileBase64}""
}}
";
Console.WriteLine(json);

// Prepare the HTTP request
var request = (HttpWebRequest)WebRequest.Create("https://html-to-zpl.p.rapidapi.com/pdf2zpl");
request.Method = "POST";
request.Headers.Add("x-rapidapi-key", YOUR_API_KEY);
request.ContentType = "application/json";
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
streamWriter.Write(json);
}

// Send the HTTP request and get the response
var response = (HttpWebResponse)request.GetResponse();
using (var streamReader = new StreamReader(response.GetResponseStream()))
{
// Read the response content
string responseContent = streamReader.ReadToEnd();

// Return the response content
return responseContent;
}
}
}