prawnto displaying tables that don't break when new page - ruby-on-rails

I have a variable number of tables with variable number of rows and I want to have them displaying one after the other but if a table doesn't fit on the current page put it on the next then continue on. I have put the table in a transaction so I can roll back then print it if the height will fit on curent page but how do I get the table height?
I have this code at the moment
pdf.transaction do
pdf.table #data,
:font_size => 12,
:border_style => :grid,
:horizontal_padding => 10,
:vertical_padding => 3,
:border_width => 2,
:position => :left,
:row_colors => ["FFFFFF","DDDDDD"]
pdf.move_down 20
#pdf.rollback
end
Any help on this would be great. Or any other way to do this ?
Best Regards
Rick

4 years later... :)
As #m-x wrote it, rollback was disabled for security reason, like "group", and is still not implemented. So, here how I deal with break pages for tables :
Big and simple table (one row per data)
Just use header option
pdf.table #data,
header: true, # You can use 'header: 2' if your header take two rows
font_size: 12,
border_style: :grid,
horizontal_padding: 10,
vertical_padding: 3,
border_width: 2,
position: :left,
row_colors: ["FFFFFF","DDDDDD"]
Small table or complexe table
make table
check if you need break page
draw table
With your example :
t = pdf.make_table #data,
font_size: 12,
border_style: :grid,
horizontal_padding: 10,
vertical_padding: 3,
border_width: 2,
position: :left,
row_colors: ["FFFFFF","DDDDDD"]
if cursor - t.height < 0
start_new_page
end
t.draw
Hope that helps

#current_page = pdf.page_count
#roll = pdf.transaction do
pdf.move_down 20
pdf.table #data,
:font_size => 12,
:border_style => :grid,
:horizontal_padding => 10,
:vertical_padding => 3,
:border_width => 2,
:position => :left,
:row_colors => ["FFFFFF","DDDDDD"]
pdf.rollback if pdf.page_count > #current_page
end
if #roll == false
pdf.start_new_page
pdf.table #data,
:font_size => 12,
:border_style => :grid,
:horizontal_padding => 10,
:vertical_padding => 3,
:border_width => 2,
:position => :left,
:row_colors => ["FFFFFF","DDDDDD"]
end
I hope this works for you as for me :-)

I'm a Prawn beginner, so this might not be the best solution, but it should work.
You can get the table height if you consider the font size and vertical padding and the number of records you have in #data and you can get the current cursor position by calling Prawn::Document.cursor method.
Having these two numbers you should be able to check whether the table fits on this page or not. If not, just start a new one (by calling Prawn::Document.start_new_page method).
Otherwise the table will break automatically and will continue on the next page.

Thanks Igor
I am currently setting the current page and then in the transaction after the new table has been rendered and before the roll back setting new_page variable. Then i can roll back and chek if the new page var > current page var and if it is start new page and print the table. See code below.
The problem is now the pdf.start_new_page says error but if i just take the pdf.rollback line out it works. See error below.
Any ideas any one or any easier ways, there must be one!!
thanks
rick
#current_page = pdf.page_count
pdf.transaction do
pdf.move_down 20
pdf.table #data,
:font_size => 12,
:border_style => :grid,
:horizontal_padding => 10,
:vertical_padding => 3,
:border_width => 2,
:position => :left,
:row_colors => ["FFFFFF","DDDDDD"]
#the_next_page = pdf.page_count
pdf.rollback
end
if #the_next_page > #current_page
pdf.start_new_page
pdf.table #data,
:font_size => 12,
:border_style => :grid,
:horizontal_padding => 10,
:vertical_padding => 3,
:border_width => 2,
:position => :left,
:row_colors => ["FFFFFF","DDDDDD"]
end
The error
> You have a nil object when you didn't expect it!
The error occurred while evaluating nil.identifier
Extracted source (around line #158):
155: end
RAILS_ROOT: C:/InstantRails/rails_apps/Macrotec-Application
Application Trace | Framework Trace | Full Trace
c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/prawn-core-0.7.1/lib/prawn/document.rb:302:in `go_to_page'
c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/prawn-core-0.7.1/lib/prawn/document/internals.rb:128:in `finalize_all_page_contents'
c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/prawn-core-0.7.1/lib/prawn/document/internals.rb:127:in `each'
c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/prawn-core-0.7.1/lib/prawn/document/internals.rb:127:in `finalize_all_page_contents'
c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/prawn-core-0.7.1/lib/prawn/document.rb:344:in `render'
C:/InstantRails/rails_apps/Macrotec-Application/app/views/quotations/show.pdf.prawn:158:in `_run_prawn_app47views47quotations47show46pdf46prawn'

Related

Print "signature_required" option in Fedex label - Rails

I'm using the fedex gem version 3.9 and cannot get the "signature required" option printed on my fedex label.
This is how it looks:
label = fedex.label(
:filename => "public/labels/#{label_name}",
:shipper => shipper,
:recipient => recipient,
:packages => packages,
:service_type => service_type,
:signature_required => true,
:shipping_options => shipping_options)
I remember this used to work a couple of months ago but now it doesn't.
Let me know if you have any suggestions.
Best,
Turns out that the signature options should be sent as part of the packages. Then it prints the label with the right signature initials
packages = [{
:weight => {:units => "LB", :value => 3},
:dimensions => {:length => 10, :width => 5, :height => 4, :units => "IN" },
:special_services_requested => {:special_service_types => "SIGNATURE_OPTION", :signature_option_detail => {:signature_option_type => "DIRECT"} }
}]
label = fedex.label(
:filename => "public/labels/#{label_name}",
:shipper => shipper,
:recipient => recipient,
:packages => packages
:shipping_options => shipping_options)

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.

Fedex Plugin Rails

I working on an existing code base for a client. The app is built in rails 2.3 and uses the fedex plugin. Now i have develop a functionality for shipping the goods using international priority service type, the problem i'm facing is when ever i make a call to the fedex api for a shipping label it returns an error
"Fedex::FedexError: Unable to get label from Fedex: Customs Value is required"
Can some one help me how to mention the customs value for the label.
This is how my code is:
price, labels, tracking_number = fedex.label(
:shipper => { :contact => shipper, :address => origin },
:recipient => { :contact => recipient, :address => destination.merge(:residential => false) },
:service_type => service_type,
:packages => [{:length => 3,
:height => 4,
:width => 5,
:weight => 5,
}],
:weight => 5,
:commodities => commodities,
:customs_value =>{:currency => "USD", :amount => "100" },
:residential => is_residential_destination
)
Thanks in advance.

'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

Rails Prawn PDF Header With Vertical Text

I have a functioning prawn document generated in rails. I built the table using text_box() to wrap the text in the header in order to rotate the text_box, and thus the text, 90 degrees. It works great, until the header reproduces on the next page...in which case the table header is intact but the text (inside the text_box()) is not reproduced because of how each text_box is positioned. Im wondering if anyone has a better idea for rotation the text so that it is simply in the table head. I couldn't get anything other than this method to produce what I want.
You can view my functioning PDF here...
http://flightrecord.herokuapp.com/vfrlarge.pdf?rating=6
The table data is quit extensive, so Im only gonna include how Im generating the table header:
def flights_table
table **flight_data_rows**, :width => 1200 do
self.header = true
def **flight_data_rows**
y_pos = 518
height = 500
x = 155
size = 7
[[
text_box("Lesson", :at => [5, y_pos], :rotate => 90, :size => size),
text_box("Date", :at => [24, y_pos+6], :size => size),
text_box("CFI", :at => [66, y_pos+6], :size => size),
text_box("N# / Type", :at => [109, y_pos+6], :size => size),
text_box("Total Time", :at => [x, y_pos], :rotate => 90, :size => size, :width => 300),
]] +
#data.map do |data|
[
data.the_data,
data.the_data,
data.the_data,
data.the_data,
data.the_data
]
This doesn't seem to be a viable option in prawn as of 1.0.0rc1. I tried many approaches, but none worked. I finally just removed the header row completely...in hopes of finishing this project later. I'll be giving wicked_pdf a shot.

Resources