Converting .pdf to .zpl

19,088

I had the some problem to solve: take a PDF file, convert it into ZPL code somehow and print it using a Zebra printer.

Thanks to stackoverflow and the ZPL Programming Guide, I learned about embedding bitmaps with the Graphic Field command (^GF).

Basically, you have to do these steps:

  1. Render a PDF file as a bitmap
  2. Make it monochrome
  3. Convert the bitmap into ASCII hexadecimal (as defined by ZPL)
  4. Compress the ASCII hexadecimal (otherwise our printers struggled with megabytes of bitmap data)
  5. Put the data into this code template ^XA^GFA{some parameters and tons of bitmap data}^XZ

Our services were running on ASP.NET so I wrote a C# library to do just that. It wraps the native calls to Google's PDFium for rendering and returns a string with valid ZPL code for printing.

You could convert base64 encoded PDF files like that:

public static string GetZplCode(string pdfBase64, int page = 0, int dpi = 203)
{
    return PDFtoZPL.Conversion.ConvertPdfPage(pdfBase64, page: page, dpi: dpi);
}

I hope this nuget package will prevent others from spending weeks in searching on how to make this ^GF command work.

Share:
19,088
GotBatteries
Author by

GotBatteries

Always working on some code in some company for some reasons.

Updated on October 03, 2022

Comments

  • GotBatteries
    GotBatteries over 1 year

    I need to convert .pdf -file to .zpl -label file for printing with zebra printers, but is this even possible?

    The PDF comes in as a base64 encoded string, and somehow I need to output that as a .zpl -file.

    I use PHP in my project, and I prefer the method in PHP, but basically any programming language is fine, as long as it gets the job done.

    I was thinking about converting the PDF to image(which seems to be possible by quick googling), and then from image(PNG, JPG, etc.) to ZPL(which also seems to be ok by quick googling), but does anyone have any knowledge about this kind of operation or any insights before I start to do this? I'm on a tight schedule here, and I cannot afford any fruitless work.

    Update 4.8.2016

    I went the other way and created the ZPL from the scratch because it keeps our service faster than doing some conversions. So I don't have any more info on this than what the google already offers, if someone comes wondering about this same thing. PS. ZPL isn't that hard of a language. ;)

  • AndreB
    AndreB over 2 years
    Works perfectly!