silkwormy
silkwormy

Reputation: 677

Rails generate barcodes from strings

What is the best way to generate barcodes from strings in a rails app? After displaying the codes in a view I also would like to print them to a pdf file.

Thank you

Upvotes: 1

Views: 3562

Answers (2)

toretore
toretore

Reputation: 91

I wanted to write this as a comment to idlefingers' answer, but apparently I don't have enough reputation to do that, so I'll write it here instead.

If you're creating PDFs you can use Barby to render the barcode directly into the PDF file instead of creating an image first:

require 'barby/outputter/prawn_outputter'
#aPdf is a Prawn::Document, aBarcode is a Barby::Barcode
aBarcode.annotate_pdf(aPdf, :x => 100, :y => 100)

There's also a PdfWriterOutputter.

Upvotes: 9

idlefingers
idlefingers

Reputation: 32067

When tasked with the same problem, I used barby to save the barcode as a png, then just loaded that into a pdf (using prawn). I don't know if it's the best way, but it works. :)

Upvotes: 1

Related Questions