Skip to content

Instantly share code, notes, and snippets.

@metacritical
Created December 19, 2012 04:57
Show Gist options
  • Save metacritical/4334474 to your computer and use it in GitHub Desktop.
Save metacritical/4334474 to your computer and use it in GitHub Desktop.
module GeneratePdf
class UserLog
def self.pdf(data)
disp_fields = DisplayField.find_all_by_report_id(yield.id).map do |field|
{ :field_name => field.field_name , :field_label => field.field_label.capitalize }
end
list_field_names = disp_fields.collect{ |names| names[:field_name] }
list_field_labels = disp_fields.collect{ |label| label[:field_label] }
list_field_labels.empty? ? table_width = 0 : table_width = 550
Prawn::Document.new do |pdf|
pdf.text "#{ yield.name } Report", :align => :left
pdf.move_up 10
pdf.text Time.now.strftime("%m/%d/%y"),:align => :right
pdf.stroke_horizontal_rule
pdf.move_down 10
unless list_field_names.empty? || list_field_labels.empty?
pdf.table([list_field_labels],
:width => table_width,
:cell_style => {:size => 8, :borders => [], :padding => 2 , :align => :left}) do
row(0).style( :background_color => 'dddddd', :size => 8, :font_style => :bold)
end
data.each do |patient|
pdf.table([
list_field_names.collect do |field_value|
if patient.send(field_value).class.eql?(Date)
patient.send(field_value).strftime("%m/%d/%Y")
else
patient.send(field_value)
end
end
],:width => table_width, :cell_style => {:size => 8, :borders => [], :padding => 2 , :align => :left})
end
end
end.render
end
end
end
@metacritical
Copy link
Author

a pdf generation code for code in my app

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment