0

I am using barby gem to generate barcodes. I am doing it a batch at a time and then save all of them in a file to be viewed by the user. I am not trying to print one at a time.

  def generate_barcode
    number_of_instances = params[:times].to_i
    value = 12.times.map{rand(10)}.join

    barcodes = 10.times.collect { Barby::EAN13.new(value) } #collects ten barcodes in an array
    processed_barcodes = barcodes.map {|barcode| barcode.to_jpg_2(:height => 60)} #returns an array and each element is a string of jpeg file
    File.open('code.jpg', 'wb') do |f| 
      processed_barcodes.each {|barcode| f.write(barcode)} #stuck here
      #f.puts(processed_barcodes) #tried this
    end

    send_file('code.jpg',
      :type        => 'image/jpeg',
      :disposition => 'inline'
    )

  end

the view

<%= image_tag(url_for({:controller => 'business_partners', :action => 'generate_barcode', :format => 'jpg' })) %>

processed_barcodes.count came back with 10.

My code.jpg got processed but I only have 1 image.

each image in jpg string blob format looks like this

>> processed_barcodes.first
=> "\xFF\xD8\xFF\xE0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xFF\xDB\x00C\x00\x03\x02\x02\x02\x02\x02\x03\x02\x02\x02\x03\x03\x03\x03\x04\x06\x04\x04\x04\x04\x04\b\x06\x06\x05\x06\t\b\n\n\t\b\t\t\n\f\x0F\f\n\v\x0E\v\t\t\r\x11\r\x0E\x0F\x10\x10\x11\x10\n\f\x12\x13\x12\x10\x13\x0F\x10\x10\x10\xFF\xC0\x00\v\b\x00P\x00s\x01\x01\x11\x00\xFF\xC4\x00\e\x00\x01\x01\x01\x01\x01\x00\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\a\b\x06\x05\x02\x03\t\xFF\xC4\x009\x10\x00\x00\x05\x03\x01\x05\x05\x03\f\x03\x01\x00\x00\x00\x00\x00\x01\x02\x03\x04\x06\x00\x05\a\b\x11\x12\x13\x14w\"79\xB6\xB7\x158\x87\t\x16\x18!#3FI\x85\xB5\xC4\xC5\x17$B%\xFF\xDA\x00\b\x01\x01\x00\x00?\x00\xFDS\xA5)JR\x94\xA5)JR\x95\x8F\xF2\xA6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C3}?\x8F~\xDC\x85UiX\x038w\x8B\xA9~\x9F\xE4\x8F*\xC1j\xAB\xA2\xBF\xC3\xDD\x00\xC5\x9F\xDET\xABA\xBD\xE2\xC3\xBA~\xAF\x95q\xBDeX\xB7w_'\x17P/\x9EjgZ\xAB\r\xF8{\xE7n\x9F\xB3\xF4\xCE\xC3YW\r\xF8\x84gn\xA03\xF52\xC3Z\xABA\xBD\xE2\xC3\xBA~\xAF\x95q\xBD =\xDDA\xBA\x7F\xA7\x1F58\xA9V\xAF\xFF\x000_\x85\x1F\xC7\xA47\xC57\x10\xFCG\xF3\x1C\xCA\xAA\xB9\x93\xC3\xDF\x04\xF4\xFD\xE7\xA6w\xEA\x95i\xC7\xDD\xD7K\x1F\xA8\xFA\xA7\x15\xAD\xFF\x00\xA4\xEFu\x8C7\xD3\xF8\xF7\xED\xC8UV\x95\x803\x87x\xBA\x97\xE9\xFEH\xF2\xAC\x16\xAA\xBA+\xFC=\xD0\fY\xFD\xE5J\xB4\e\xDE,;\xA7\xEA\xF9W\e\xD6U\x8Bwu\xF2qu\x02\xF9\xE6\xA6u\xAA\xB0\xDF\x87\xBEv\xE9\xFB?L\xEC5\x95p\xDF\x88Fv\xEA\x03?S,5\xAA\xB4\e\xDE,;\xA7\xEA\xF9W\e\xD2\x03\xDD\xD4\e\xA7\xFAq\xF3S\x8A\x95j\xFF\x00\xF3\x05\xF8Q\xFCzC|Sq\x0F\xC4\x7F1\xCC\xAA\xAB\x99<=\xF0OO\xDEzg~\xA9V\x9C}\xDDt\xB1\xFA\x8F\xAAqZ\xDF\xFAN\xF7X\xC3}?
0

1 Answer 1

1

You open one file using File.open('code.jpg', 'wb') and write into it processed_barcodes times. If you want processed_barcodes files then you need to loop around File.open with unique names.

Instead of:

File.open('code.jpg', 'wb') do |f| 
  processed_barcodes.each {|barcode| f.write(barcode)} #stuck here
  #f.puts(processed_barcodes) #tried this
end

Use this untested code:

processed_barcodes.each_with_index do |barcode, i|
  File.open("code_#{i + 1}.jpg", 'wb') do |f| 
    f.write(barcode)
  end
end

You'll need to figure out how to modify:

send_file('code.jpg',
  :type        => 'image/jpeg',
  :disposition => 'inline'
)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.