Inscreva-se no plano básico para obter uma chave de API de desenvolvimento gratuita.
Por favor, substitua YOUR_API_KEY
no exemplo de código pela sua própria chave de API.
Converter PDF para ZPL usando 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;
}
}
}