Encoding::UndefinedConversionError: "\x89" from ASCII-8BIT to UTF-8
ziyan-junaideen opened this issue · comments
Abstract
Error recorded in production server trying to create a bar-code. Is this a bug in the lib or some thing we are doing wrong? Thanks in advance.
Details
The bar-code creation process works fine, but last night Rollbar had recorded an error. We are using v0.6.3 and the error received is as follows.
- Version 0.6.3
- Order ID = 1145
require 'barby'
require 'barby/barcode/code_128'
require 'barby/outputter/ascii_outputter'
require 'barby/outputter/png_outputter'
class Admin::OrdersController < AdminController
def print
@order = Order.find(params[:id])
@order_id = sprintf '%010d', @order.id
@barcode = Barby::Code128B.new(@order_id)
bar_code_image_path = Rails.root.join('public/bar_codes',"#{@order.id}.png" )
File.open(bar_code_image_path , 'w'){|f|
f.write @barcode.to_png(:height => 50, :margin => 15) # The line which the error happens
}
render layout: 'print'
end
Resulting error
Encoding::UndefinedConversionError: "\x89" from ASCII-8BIT to UTF-8
1 File "/home/rails/production/releases/20160506183304/app/controllers/admin/orders_controller.rb" line 79 in write
2 File "/home/rails/production/releases/20160506183304/app/controllers/admin/orders_controller.rb" line 79 in block in print
3 File "/home/rails/production/releases/20160506183304/app/controllers/admin/orders_controller.rb" line 78 in open
4 File "/home/rails/production/releases/20160506183304/app/controllers/admin/orders_controller.rb" line 78 in print
This is one of two things:
- The data you're giving to Code128B is not marked as being binary "encoded".
- The file you're writing to has not been opened as binary.
- Code128B assumes data given to it is in Encoding::BINARY/Encoding::ASCII_8BIT and marked as such. It must be.
- The file being opened must also use this encoding as it will write binary data.