rjocoleman / labelary

Ruby Gem to interact with the Labelary.com ZPL Web Service
MIT License
42 stars 14 forks source link

Render png barcode generated #9

Open ademir10 opened 1 year ago

ademir10 commented 1 year ago

Hello guys! I'm trying to use this gem to generate a barcode based in a string value, but i don't know how to do it, do you have some example?

this is my code based in your example: @barcode = Labelary::Label.render zpl: '^XA^FDHello World^FS^XZ', content_type: 'image/png', dpmm: 8, width: 6, height: 4

I'd like to render this @barcode in my view, is it possible?

when i try to inspect this variable i receive this message: "ERROR: HTTP 404 Not Found"

currently i've been used this other gem: Barby, but i need to print the barcode and also string value:

  code = Barby::Code39.new(id_invoice).to_png(margin:5 , height: 50)
  @image = Base64.encode64(code.to_s).gsub(/\s+/, "")

in my view: <%= image_tag "data:@barcode/png;base64,#{Rack::Utils.escape(@barcode)}" %>

Is it possible with your gem? thank you so much guys!

rjocoleman commented 1 year ago

This gem specifically abstracts the http://labelary.com/ webservice to use it to render Zebra printer language (ZPL) as images. It doesn't generate barcodes.. it's really only useful if you're working with Zebra printer language from a Ruby app.

@barcode = Labelary::Label.render zpl: '^XA^FDHello World^FS^XZ', content_type: 'image/png', dpmm: 8, width: 6, height: 4 return a PNG from the webserivce.

label

I don't think this is what you want.

If you want Code39 barcode as a data url, with human readable barcode string, and nothing to do with ZPL:

Maybe something like this (I didn't run the code and haven't used barby for a little bit, but it should give you a starting point)

module ApplicationHelper
  require 'barby'
  require 'barby/barcode/code_39'
  require 'barby/outputter/png_outputter'
  require 'base64'

  def generate_barcode_data(id_invoice)
    # Create a new Code39 barcode
    barcode = Barby::Code39.new(id_invoice)

    # Use the PNG outputter to get the blob
    blob = barcode.to_png(margin: 5, height: 50)

    # Encode the blob in Base64
    base64_barcode = Base64.encode64(blob).gsub("\n", '')

    {
      image: "data:image/png;base64,#{base64_barcode}",
      text: barcode.to_s
    }
  end
end
<% barcode_data = generate_barcode_data(id_invoice) %>
<%= image_tag barcode_data[:image], alt: 'Barcode', title: id_invoice %>
<p>Invoice ID: <%= barcode_data[:text] %></p>