Ruby-Rails Spreadsheet "corrupted" under Microsoft Excel 2010 - ruby-on-rails

I am using Spreadsheet gem to allow users to export data with .xls format. The code I am showing is allowing users to open the file in Open Office and MS Excel for MAC, but not for Windows, which I get a "file is corrupted" error.
I have a method that do the XLS export like this:
book = Spreadsheet::Workbook.new
sheet1 = book.create_worksheet(:name => "Expired Activity")
# Title
title_format = Spreadsheet::Format.new(:size => 12, :pattern => 1, :pattern_fg_color => :blue, :align => :merge)
for i in 0..5
sheet1.row(0).set_format(i, title_format)
end
sheet1.row(0).push "Activity"
# Header row
header_format = Spreadsheet::Format.new(:size => 11, :pattern => 1, :pattern_fg_color => :yellow, :align => :merge)
index_row = sheet1.row(1)
index_row.push("Date")
index_row.push("First Name")
index_row.push("Last Name")
for i in 0..2
index_row.set_format(i, header_format)
end
# Row values
row_iterator = 2
#expired.each do |expired|
sheet1.row(row_iterator).push(expired.earnedOn, expired.firstName, expired.lastName)
row_iterator = row_iterator + 1
end
file_io = StringIO.new
book.write file_io
file_io.string
And this method is being called like this:
format.xls {#expired = current_user.recent_activity(unlimited_size,params[:start],nil);
send_data(build_excel_expired_activity, :filename => "#{Time.now.strftime('%Y-%m-%d')}_Expired_Activity.xls", :type => "application/xls", :disposition => 'attachment')
}
Any thoughts, or ideas?

You might want to have a look at the axlsx gem and its rails counterpart acts_as_xlsx. It supports full schema validation so you don't have to fight with this kind of thing.
https://www.ruby-toolbox.com/projects/axlsx

Related

batch jpg images in string blob not all written to file with ruby file open write barby gem rmagick

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{\xCB\x11\xB9\xA6q\xB4\xD9e|\xBBH|>mt\xB2\xA7\xC8\xB6?*\xE9\x85\x82&\xED\xA2\x9BL\x98\x89\xF7\x17\xBA\xBF>\xC3\x89\x8An>\xC3\x01\x8AB\x01h\x1A`\xCAS\xBC\x89\xECo\x9E7\xDFhsx~\x01)[\xFDTR\xDE\xBA\\\xBD\xAB\xCE\xAF\xF6d.\xCE'*\x87`;\x05\xDC\xEC\x14\xBBM\xB6\x7F\xA4\xAC\xF7\x962l\xD25i\x9CJ\xF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\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}?
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'
)

Inconsistent display of checkboxes on PDF form filled with PDFtk

I am filling PDF forms in my rails app with the pdf-forms (https://github.com/jkraemer/pdf-forms) gem, based on PDFtk. Text fields work as I would expect, but my checkbox fields do not. The boxes display well in Chrome, but in Preview and Mail the checkbox fields appear empty.
class FormsController < ApplicationController
require 'pdf_forms'
def acord25
#policy = Policy.find(params[:id])
pdftk = PdfForms.new('/usr/local/bin/pdftk')
# find out the field names that are present in form.pdf
pdftk.get_field_names 'lib/pdfs/acord25.pdf'
# take form.pdf, set the 'foo' field to 'bar' and save the document to myform.pdf
pdftk.fill_form '/lib/pdfs/acord25.pdf', "acord25.pdf",
"F[0].P1[0].Form_CompletionDate_A[0]" => #policy.dateIssued,
"F[0].P1[0].Producer_FullName_A[0]" => #policy.client.broker.name,
"F[0].P1[0].Producer_MailingAddress_LineOne_A[0]" => #policy.client.broker.company,
"F[0].P1[0].Producer_MailingAddress_LineTwo_A[0]" => #policy.client.broker.address,
"F[0].P1[0].Producer_ContactPerson_FullName_A[0]" => #policy.legalVesting,
"F[0].P1[0].Producer_ContactPerson_PhoneNumber_A[0]" => #policy.client.broker.phone,
"F[0].P1[0].Producer_FaxNumber_A[0]" => #policy.client.broker.fax,
"F[0].P1[0].Producer_ContactPerson_EmailAddress_A[0]" => #policy.client.broker.email,
"F[0].P1[0].NamedInsured_FullName_A[0]" => #policy.client.name,
"F[0].P1[0].NamedInsured_MailingAddress_LineOne_A[0]" => #policy.client.address.titlecase,
"F[0].P1[0].GeneralLiability_CoverageIndicator_A[0]" => 1,
"F[0].P1[0].GeneralLiability_OccurrenceIndicator_A[0]" => 1,
"F[0].P1[0].GeneralLiability_GeneralAggregate_LimitAppliesPerLocationIndicator_A[0]" => 1,
"F[0].P1[0].Policy_PolicyNumberIdentifier_A[0]" => #policy.policyNumber,
"F[0].P1[0].Policy_EffectiveDate_A[0]" => #policy.dateEffective,
"F[0].P1[0].PolicyExpirationGeneral[0]" => #policy.term.dayEnd,
"F[0].P1[0].Insurer_FullName_A[0]" => "Lexington Insurance Company",
"F[0].P1[0].Insurer_NAICCode_A[0]" => 19437,
"F[0].P1[0].Insurer_FullName_B[0]" => "Commerce & Industry Insurance Company",
"F[0].P1[0].Insurer_NAICCode_B[0]" => 19410,
"F[0].P1[0].Insurer_FullName_C[0]" => "Great American Insurance Company",
"F[0].P1[0].Insurer_NAICCode_C[0]" => 37532,
"F[0].P1[0].Insurer_FullName_D[0]" => "Admiral Insurance Company",
"F[0].P1[0].Insurer_NAICCode_D[0]" => 24856,
"F[0].P1[0].GeneralLiability_InsurerLetterCode_A[0]" => "A",
"F[0].P1[0].GeneralLiability_EachOccurrence_LimitAmount_A[0]" => 1000000,
"F[0].P1[0].GeneralLiability_FireDamageRentedPremises_EachOccurrenceLimitAmount_A[0]" => 50000,
"F[0].P1[0].GeneralLiability_MedicalExpense_EachPersonLimitAmount_A[0]" => "Excluded",
"F[0].P1[0].GeneralLiability_PersonalAndAdvertisingInjury_LimitAmount_A[0]" => 1000000,
"F[0].P1[0].GeneralLiability_GeneralAggregate_LimitAmount_A[0]" => 2000000,
"F[0].P1[0].GeneralLiability_ProductsAndCompletedOperations_AggregateLimitAmount_A[0]" => 2000000,
"F[0].P1[0].Vehicle_InsurerLetterCode_A[0]" => "A",
"F[0].P1[0].Vehicle_HiredAutosIndicator_A[0]" => 1,
"F[0].P1[0].Vehicle_NonOwnedAutosIndicator_A[0]" => 1,
"F[0].P1[0].Policy_PolicyNumberIdentifier_B[0]" => #policy.policyNumber,
"F[0].P1[0].Policy_EffectiveDate_B[0]" => #policy.dateEffective,
"F[0].P1[0].Policy_ExpirationDate_B[0]" => #policy.term.dayEnd,
"F[0].P1[0].Vehicle_CombinedSingleLimit_EachAccidentAmount_A[0]" => 1000000,
"F[0].P1[0].ExcessUmbrella_InsurerLetterCode_A[0]" => "B",
"F[0].P1[0].ExcessUmbrella_OccurrenceIndicator_A[0]" => 1,
"F[0].P1[0].ExcessUmbrella_DeductibleIndicator_A[0]" => 1,
"F[0].P1[0].ExcessUmbrella_Umbrella_DeductibleOrRetentionAmount_A[0]" => #policy.coverages.first.deductibleOcc,
"F[0].P1[0].Policy_PolicyNumberIdentifier_D[0]" => #policy.policyNumber,
"F[0].P1[0].Policy_EffectiveDate_D[0]" => #policy.dateEffective,
"F[0].P1[0].Policy_ExpirationDate_D[0]" => #policy.term.dayEnd,
"F[0].P1[0].ExcessUmbrella_Umbrella_EachOccurrenceAmount_A[0]" => 10000000,
"F[0].P1[0].ExcessUmbrella_Umbrella_AggregateAmount_A[0]" => 10000000
send_file("#{Rails.root}/acord25.pdf", filename: "#{#policy.client.name} - #{#policy.carrier.name} - #{#policy.policyNumber} (#{Time.now}).pdf", type: "application/vnd.ms-excel")
end
end
TL;DR: Checked boxes display in Chrome, but not in Preview or Mail.
I appreciate any help or leads on what the problem might be. Thank you!
Turns out the PDF form was corrupted. Had to recreate form, not a PDFTK issue.

'Encoding::UndefinedConversionError "\xE9" from ASCII-8BIT to UTF-8' when using some ruby gems that using file.open in ruby on rails

What's I want?
I want to generate .docx file or .odt file from template file in Rails 3.2
I want to use Japanese in it.
In ubuntu server 12.04 & ruby 1.9.3p194 & rails 3.2.8
What's happen?
I tried gems 'docx-templater' and 'serenity'
ruby-docx-templater
https://github.com/jawspeak/ruby-docx-templater
1 sample works good
2 try to do the same in my rails app
in controller as is sample
def gen_docx
input_file = './app/template/ExampleTemplate.docx'
data = {
:teacher => "Priya Vora",
:building => "Building #14",
:classroom => :'Rm 202',
:district => "Washington County Public Schools",
:senority => 12.25,
:roster => [
{:name => 'Sally', :age => 12, :attendence => '100%'},
{:name => :Xiao, :age => 10, :attendence => '94%'},
{:name => 'Bryan', :age => 13, :attendence => '100%'},
{:name => 'Larry', :age => 11, :attendence => '90%'},
{:name => 'Kumar', :age => 12, :attendence => '76%'},
{:name => 'Amber', :age => 11, :attendence => '100%'},
{:name => 'Isaiah', :age => 12, :attendence => '89%'},
{:name => 'Omar', :age => 12, :attendence => '99%'},
{:name => 'Xi', :age => 11, :attendence => '20%'},
{:name => 'Noushin', :age => 12, :attendence => '100%'}
],
:event_reports => [
{:name => 'Science Museum Field Trip', :notes => 'PTA sponsored event. Spoke to Astronaut with HAM radio.'},
{:name => 'Wilderness Center Retreat', :notes => '2 days hiking for charity:water fundraiser, $10,200 raised.'}
],
:created_at => "11-12-03 02:01"
}
DocxTemplater::DocxCreator.new(input_file, data).generate_docx_file()
end
3 but the error raised
the error raised at following point in gem (docx_templater.rb 22)
File.open(file_name, 'w') { |f| f.write(buffer) }
serenity
https://github.com/kremso/serenity
1 sample works well
2 do the same in my rails app and works well
like this
#encoding: utf-8
require 'serenity'
class Showcase
include Serenity::Generator
Person = Struct.new(:name, :items)
Item = Struct.new(:name, :usage)
def generate_showcase
#title = 'Serenity inventory'
mals_items = [Item.new('Moses Brothers Self-Defense Engine Frontier Model B', 'Lock and load')]
mal = Person.new('Malcolm Reynolds', mals_items)
jaynes_items = [Item.new('Vera', 'Callahan full-bore auto-lock with a customized trigger, double cartridge and thorough gauge'),
Item.new('Lux', 'Ratatata'),
Item.new('Knife', 'Cut-throat')]
jayne = Person.new('Jayne Cobb', jaynes_items)
#crew = [mal, jayne]
render_odt 'app/template/showcase.odt'
end
end
3 I tried my template including Japanese but the error raised.
the error raised at following point in gem(template.rb 22)
def process context
tmpfiles = []
Zip::ZipFile.open(#template) do |zipfile|
%w(content.xml styles.xml).each do |xml_file|
content = zipfile.read(xml_file)
odteruby = OdtEruby.new(XmlReader.new(content))
out = odteruby.evaluate(context)
tmpfiles << (file = Tempfile.new("serenity"))
file << out #!!!! HERE !!!!
file.close
zipfile.replace(xml_file, file.path)
end
end
end
What I did?
I found that 'serenity' have rspec test for Greek (UTF-8) in gem.I tried Japanese the same way. and the test passed!. so I thought the problem is not in gems in rails setting.
add magic comment "#encoding: utf-8" to my controller or lib file
confirm 'config.encoding = "utf-8"' in config/application.rb
add following to config/enviroment.rb above "initialize!"
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
I don't use any database in my rails app.
But all is nothing to do with my case... any idea?
I may not know the basic things ...
This is a little old, but I just ran into this same situation. In my case, it was a matter of setting the tempfile to binary mode prior to writing to it:
tmpfiles << (file = Tempfile.new("serenity"))
file.binmode
file << out
file.close
Hope this helps

Convert CSV to Array?

Any ideas on how to convert this CSV into a ruby array using vim?
Starting CSV:
Year,Make,Model
1997,Ford,E350
2000,Mercury,Cougar
Desired Array:
car_info = [
{'Year' => '1997', 'Make' => 'Ford', 'Model' => 'E350'},
{'Year' => '2000', 'Make' => 'Mercury', 'Model' => 'Cougar'},
]
I have > 2000 entries like the CSV above, and I'd love a way to quickly re-format it for use in my Rails app. I'd like to use vim, but I'm open to other options too.
FasterCSV.read("path/to/file.csv", :headers => true).map do |row|
{ "Year" => row[0], "Make" => row[1], "Model" => row[2] }
end
PS: Install faster_csv gem
In vim, you can use global search and replace with a regular expression:
:g/\(.*\),\(.*\),\(.*\)/s//{'Year' => '\1', 'Make' => '\2', 'Model' => '\3'}/g
Then edit the first and last lines of the resulting file accordingly.

Difficulty with send_data in Ruby on Rails in conjunction with Spreadsheet plug-in

I have a function in a controller that takes in some specifications and generates a report on them. This function user_report is called in a view:
< %= submit_to_remote 'submit-button', "Export Report to Excel", :url => { :controller => :reports, :action => :user_report, :print_state => 'print'} % >
In reports_controller I use the Spreadsheet plugin to generate an Excel file within the user_report function. I want to use send_data to stream the file to the user without creating it on the server first. The research I've done shows that using StringIO is the way to go, as shown below. Frustratingly, nothing happens when I call send_data. The plugin seems to work well creating a file and saving it on the server, but does nothing when I use send_file, suggesting that the problem doesn't lie in the plugin. But then what am I doing wrong with send_file/send_data?
The function itself looks like this:
def user_report
if request.post?
unless params[:reports][:userid].blank?
#userid=params[:reports][:userid]
end
if params[:print_state]=='print'
report = Spreadsheet::Workbook.new
info = report.create_worksheet :name => 'User Information'
info.row(1).push 'User ID', #userid
#outfile = "Report_for_#{#userid}.xls"
require 'stringio'
data = StringIO.new ''
report.write data
send_data data.string, :type=>"application/excel", :disposition=>'attachment', :filename => #outfile
end
respond_to do |format|
format.js { }
end
end
end
The log file reads
2010-10-18 14:13:59 INFO -- Sending data Report_for_jjohnson.xls
but no download begins in-browser. I've succeed in using send_data on this app before, which is confusing.
I'm using Rails v2.3, Ruby v1.8.7, and Spreadsheet v6.4.1 at spreadsheet.rubyforge.org.
Just change the line:
send_data data.string, :type=>"application/excel", :disposition=>'attachment', :filename => #outfile
to:
send_data data.string.bytes.to_a.pack("C*"), :type=>"application/excel", :disposition=>'attachment', :filename => #outfile
Even though I dont like to write and delete , but with spreadsheet seems like the only solution.
# write the file
book.write "Employee_History_#{ params[:id]}.xls"
# send the file
send_file "Employee_History_#{ params[:id]}.xls", :type => "application/vnd.ms-excel", :filename => "data.xls", :stream => false
# and then delete the file
File.delete("Employee_History_#{ params[:id]}.xls")
For someone looking at this in (or after) 2022, a possible solution to this would be to use Axlsx Gem. The interface provides a method for converting it to a StringIO object. (From Axlsx Documentation)
#serialize to a file
p = Axlsx::Package.new
# ......add cool stuff to your workbook......
# Serialize to a stream
s = p.to_stream()
send_data(
s.read,
:type => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
:disposition => 'attachment',
:filename => #filename
)

Resources