*Suppose we want to download a show
page as a pdf*
step-1
Create a downlolad button inside show
page
<a href="<%=
generate_pdf_referral_ic_patient_path(current_ic, @patient.id) %>"
class="header_button" style="margin-left:20px;"><i
class="fa fa-download"></i> Download</a>
step-2
Create a action for download as pdf
inside paisent controller.
*Suppose we want to download paitent
info*
def generate_pdf
kit =
pdf_data_generator("generate_pdf")
send_data(kit.to_pdf, filename:
"#{@patient.name}_preview_report", :type =>
"application/pdf", :disposition => "attachment")
end
step-3
Create the route for download action.
resources :patients do
member do
get :generate_pdf
end
end
Step-4
craete a method pdf_data_generator
inside application controller
def pdf_data_generator(action_name)
html_file =
render_to_string(action: action_name, layout: false)
kit = PDFKit.new(
html_file,
page_size: 'Letter',
margin_top: '0.40in'
)
return kit
end
Step-5
Generate a pdf view inside patient view
folder
<div class="form-horizontal
fill-up validatable">
<div class="padded">
<div class="row-fluid">
<div class="span12">
<div class="field
control-group">
<span
class="control-label">Employee Name:</span>
<div class="controls"><%=
@employee.full_name %></div>
</div>
<div class="field
control-group">
<span
class="control-label">Employee Number:</span>
<div class="controls"><%=
@employee.employee_number %></div>
</div>
<div class="field
control-group">
<span
class="control-label">Job duty/Title:</span>
<div class="controls"><%=
@accident_report.job_duty %></div>
</div>
<div class="field
control-group">
<span
class="control-label">Injury Date and Time:</span>
<div class="controls">
<%=
month_name_dd_yy_helper(@accident_report.injury_datetime) %>
</div>
</div>
<div class="field
control-group">
<span
class="control-label">Client name:</span>
<div class="controls"><%=
@accident_report.client_name %></div>
</div>
<div class="field
control-group">
<span
class="control-label">Supervisor name:</span>
<div class="controls"><%=
@accident_report.supervisor_name %></div>
</div>
<div class="field
control-group">
<span
class="control-label">Supervisor Phone:</span>
<div class="controls"><%=
@accident_report.phone_number %></div>
</div>
<div class="field
control-group">
<span
class="control-label">Accident Location:</span>
<div class="controls"><%=
@accident_report.accident_location %></div>
</div>
<div class="field
control-group">
<span
class="control-label">Length Of Employment:</span>
<div class="controls"><%=
@accident_report.length_of_employment %></div>
</div>
<div class="field
control-group">
<span
class="control-label">Injury Occur At:</span>
<div class="controls"><%=
@accident_report.injury_occur_day %></div>
</div>
<div class="field
control-group">
<span
class="control-label">Day Of The Week:</span>
<div class="controls"><%=
@accident_report.accident_day %></div>
</div>
<div class="field
control-group">
<span
class="control-label">Body Part:</span>
<div class="controls
body_part_check_box">
<%
BodyPart.all.each_with_index do |body_part, index| %>
<div
class="checkbox_margin">
<%= check_box_tag
body_part.name, body_part.id, @body_part_ids.try(:include?,
body_part.id) %>
<label><%=
body_part.name %></label>
</div>
<% end %>
<div
class="checkbox_margin">
<span
class="control-label"><label>Other Body
Part:</label></span>
<div
class="controls"><%=
@accident_report.other_body_part.try(:other) %>
</div>
</div>
</div>
</div>
</div>
</div>
save it as action name
generate_pdf.html.erb
step-6
Click on download button.
Comments
Post a Comment