Ruby quickstart
This page shows how to convert labels from Ruby with the LabelZoom Ruby SDK.
Before you begin
Section titled “Before you begin”- Install Ruby 3.1 or later.
- Optional: Get an API key. Without one, output has a watermark. See Authentication.
Install the SDK
Section titled “Install the SDK”Install the labelzoom gem from RubyGems:
gem install labelzoomIf you use Bundler, add the gem to your Gemfile instead:
gem "labelzoom", "~> 2.0"Convert ZPL to PDF
Section titled “Convert ZPL to PDF”Save the following program as convert.rb:
require "labelzoom"
client = LabelZoom::Client.new # reads LABELZOOM_API_KEY
result = client.convert( :zpl, :pdf, "^XA^FO50,50^ADN,36,20^FDLabelZoom^FS^XZ", label: { width: 4, height: 6 } # inches)
result.save("label.pdf")The SDK reads your API key from the LABELZOOM_API_KEY environment variable. To pass a key
explicitly, use LabelZoom::Client.new(api_key: "API_KEY").
Options are keyword arguments, and grouped options are nested hashes. Source options, such as
label: and pdf:, set how the document is read, and target options, such as target_dpi:, set
what is written. A PDF target takes no target_dpi:. A misspelled key inside a nested hash raises
LabelZoom::ValidationError.
Run the program with ruby convert.rb. It writes label.pdf.
Convert a PDF to ZPL
Section titled “Convert a PDF to ZPL”result = client.convert( :pdf, :zpl, File.binread("shipping-label.pdf"), pdf: { page_number: 0 }, # source: 0-based; omit to convert every page target_dpi: 203 # target: the printer's resolution)
puts result.textWrite PDF, image, and printer-language (EPL, IPL, TSPL, DPL, SBPL) output as bytes, with result.save or File.binwrite rather than
File.write; see Binary output.
Fill variable fields
Section titled “Fill variable fields”template = "^XA^FO50,50^A0N,30,30^FD{name}^FS^FO50,100^A0N,30,30^FD{sku}^FS^XZ"
result = client.convert( :zpl, :zpl, template, data: [ # one label per record { name: "ACME Corp", sku: "12345" }, { name: "Globex", sku: "67890" } ])
puts result.textEach placeholder, such as {name}, takes its value from the matching key in each record; see
Merge fields for the placeholder syntax.
Handle errors
Section titled “Handle errors”begin client.convert(:zpl, :pdf, "^XA^FO50,50^ADN,36,20^FDLabelZoom^FS^XZ")rescue LabelZoom::APIError => e warn "HTTP #{e.status} (#{e.code}), request ID #{e.request_id}: #{e.message}"endEach API error is a subclass of LabelZoom::APIError, such as LabelZoom::BadRequestError, and
carries status, code, and request_id. The SDK retries 429 and 5xx responses automatically
before it raises. For status codes and error codes, see Errors.
Call the API without the SDK
Section titled “Call the API without the SDK”To call the API with net/http instead of the SDK, send the label as the request body:
require "json"require "net/http"require "uri"
zpl = "^XA^FO50,50^ADN,36,20^FDLabelZoom^FS^XZ"
uri = URI("https://api.labelzoom.com/api/v2/convert/zpl/to/pdf")uri.query = URI.encode_www_form( "params" => JSON.generate({ "label" => { "width" => 4, "height" => 6 } }))
request = Net::HTTP::Post.new(uri)request["Content-Type"] = "text/plain"request["Accept"] = "*/*"# The API key is optional. Omit the Authorization header if it isn't set.api_key = ENV["LABELZOOM_API_KEY"]request["Authorization"] = "Bearer #{api_key}" if api_key && !api_key.empty?request.body = zpl
response = Net::HTTP.start(uri.host, uri.port, use_ssl: true) { |http| http.request(request) }raise "LabelZoom returned #{response.code}: #{response.body}" unless response.code == "200"
File.binwrite("label.pdf", response.body)Write the PDF with File.binwrite, not File.write. To convert a PDF to ZPL, post the file’s bytes
to /convert/pdf/to/zpl with Content-Type: application/pdf, and read response.body as text.
To set options, send one params query parameter that holds JSON, or use dot notation such as
label.width=4. Accept can be */*, the target’s media type, or a wildcard such as image/*.
See Conversion parameters.