How Create and Send PDF with a mail in Ruby on Rails.

Step 1- Create a send button.

Example-
<span class="fl">Email To Partner : </span>
<%= autocomplete_field_tag "preferred_partner", '', autocomplete_preferred_partner_email_referral_ic_preferred_partners_path(current_ic), class: "form-control width_half mr_left_30 fl autocomplete_for_id", id: "partner_autocomplete" %>
<input type="hidden" class="hidden_field_id">
<button id="send_fs_btn" class="btn btn-success mr_left_30 fl">Send</button>


Step 2- Call action which perform send operation...

<script type="text/javascript">
autocomplete_for_id();
initialize_tipsy_tool();

$(document).on("click", "#send_fs_btn", function(){
$.ajax({
type : "get",
data : { "partner_id" : $(".hidden_field_id").val() },
url : "<%= send_facesheet_referral_ic_patient_feature_path(current_ic, @patient.id) %>"
});
});
</script>


Step3- Create a action which generate and pdf

Example.

class Referral::PatientFeaturesController < BaseController
include Authenticatable
skip_before_filter :current_bc, :current_bc_user
before_filter :find_patient, only: [:send_facesheet]

def send_facesheet
respond_to do |format|
format.js{
if PreferredPartner.find_by_id(params[:partner_id]).present?
SendFacesheetWorker.perform_at(1.minutes, @patient.id, params[:partner_id])
flash[:notice] = "Facesheet has been sent successfully."
else
flash[:error] = "Preferred partner not found."
end
}
format.html{ redirect_to referral_ic_patients_path(current_ic) }
end
end
private

def find_patient
@patient = Patient.where(id: params[:id]).first
if @patient.blank?
flash[:alert] = "You don't have access to the requested url."
js_redirect_to(referral_ic_patients_path(current_ic)) and return
end
end
end


Step-4 Create a worker which send the pdf

Example

class SendFacesheetWorker
include Sidekiq::Worker

def perform(patient_id, partner_id)
patient = Patient.find_by_id(patient_id)
PatientMailer.send_facesheet(patient.get_facesheet, patient, PreferredPartner.find_by_id(partner_id))
end
end


Step-5 inside model create the get_facesheet method.

def get_facesheet
locals = { patient: self, employer: self.referral_employer, insurance: self.referral_insurance,
provider: self.referring_provider, diagnostic: self.patient_diagnostic, therapy: self.patient_therapy }
html = ActionView::Base.new(Rails.configuration.paths["app/views"])
html = html.render(
partial: "referral/patients/facesheet", format: :txt,
locals: locals )
html = html.gsub("https://", "http://")
kit = PDFKit.new(html,
page_size: 'Letter',
margin_top: '0.40in',
margin_right: '0.40in',
margin_left: '0.40in'
)
kit.stylesheets << "#{ Rails.root }/app/assets/stylesheets/dashboard/custom2.css"
kit.stylesheets << "#{ Rails.root }/app/assets/stylesheets/dashboard/bootstrap.min.css"
return kit.to_pdf
end




Step 6 Create the view of pdf


<style type="text/css">
.facesheet-inner-box td {
padding: 5px !important;
font-size: 12px !important;
}
.mrg_lf_5px {
margin-left: 5px;
}

<div class="facesheet-inner-box">
<div class="facesheet-logo">
<img src="http://app.compcorepro.com/assets/new_home_design/logo.png" />
</div>
<div class="sheet-content">
<table class="table facesheet-table">
<tbody>
<tr>
<td class="width_33_per">Date of Referral : <%= month_name_dd_yy_helper(patient.referral_date) %></td>
<td class="width_33_per">Phone : </td>
<td class="width_33_per">Fax : </td>
</tr>
<tr>
<td colspan="3">
Referral Source Name : <%= provider.try(:name) %>
</td>
</tr>
</tbody>
</table>
<table class="table facesheet-table">
<thead>
<tr>
<td colspan="2">Claim Information</td>
</tr>
</thead>
<tbody>
<tr>
<td class="width_half">Claimant/Patient : <%= patient.name.titleize %></td>
<td class="width_half">Claim # : <%= patient.claim_no %></td>
</tr>
<tr>
<td class="width_half">Address : <%= patient.decorate.get_full_address %></td>
<td class="width_half">Date of Injury : <%= month_name_dd_yy_helper(patient.injury_date) %></td>
</tr>
<tr>
<td class="width_half">Zipcode : <%= patient.zipcode %></td>
<td class="width_half">Treating Physician :</td>
</tr>
<tr>
<td class="width_half">Home Phone : <%= patient.phone_no %></td>
<td class="width_half">Treating Physician Phone : <%= provider.try(:phone_no) %></td>
</tr>
<tr>
<td class="width_half">Type of Injury : <%= patient.icd_code_description %></td>
<td class="width_half">DOB : <%= month_name_dd_yy_helper(patient.dob) %></td>
</tr>
<tr>
<td class="width_half">Employer : <%= employer.try(:name) %></td>
<td class="width_half">SSN : <%= patient.ssn %></td>
</tr>
<tr>
<td class="width_half">Employer Contact : <%= employer.try(:contact_no) %></td>
<td class="width_half"></td>
</tr>
</tbody>
</table>
<table class="table facesheet-table">
<thead>
<tr>
<td colspan="2">Carrier Information</td>
</tr>
</thead>
<tbody>
<tr>
<td class="width_half">Company Name : <%= insurance.try(:name) %></td>
<td class="width_half">Fax : </td>
</tr>
<tr>
<td class="width_half">Adjuster Name : <%= insurance.try(:adjuster_name) %></td>
<td class="width_half">Adjuster Phone : <%= insurance.try(:adjuster_phone_no) %></td>
</tr>
</tbody>
</table>
<table class="table facesheet-table">
<thead>
<tr>
<td colspan="3">Reason for Referral</td>
</tr>
</thead>
<tbody>
<tr>
<td colspan="3" class="text_center"><b>Diagnostic Test :</b></td>
</tr>
<tr>
<td>
<%= check_box_tag 'mri_without_contrast', {}, diagnostic.mri_without_contrast, disabled: true %><span class="mrg_lf_5px"> MRI Without Contrast</span>
</td>
<td>
<%= check_box_tag "ct_scan_without_contrast", {}, diagnostic.ct_scan_without_contrast, disabled: true %><span class="mrg_lf_5px"> CT Scan Without Contrast</span>
</td>
<td>
<%= check_box_tag "bone_full_body_scan", {}, diagnostic.bone_full_body_scan, disabled: true %><span class="mrg_lf_5px"> Bone Scan Full Body</span>
</td>
</tr>
<tr>
<td>
<%= check_box_tag "mri_with_contrast", {}, diagnostic.mri_with_contrast, disabled: true %><span class="mrg_lf_5px"> MRI With Contrast</span>
</td>
<td>
<%= check_box_tag "ct_scan_with_contrast", {}, diagnostic.ct_scan_with_contrast, disabled: true %><span class="mrg_lf_5px"> CT Scan With Contrast</span>
</td>
<td>
<%= check_box_tag "bone_three_phase_scan", {}, diagnostic.bone_three_phase_scan, disabled: true %><span class="mrg_lf_5px"> Bone Scan Three Phase</span>
</td>
</tr>
<tr>
<td>
<%= check_box_tag "mri_arthrogram", {}, diagnostic.mri_arthrogram, disabled: true %><span class="mrg_lf_5px"> MRI Arthrogram</span>
</td>
<td>
<%= check_box_tag "myelogram_with_ct", {}, diagnostic.myelogram_with_ct, disabled: true %><span class="mrg_lf_5px"> Myelogram with CT</span>
</td>
<td>
<%= check_box_tag "emg", {}, diagnostic.emg, disabled: true %><span class="mrg_lf_5px"> EMG/NCS</span>
</td>
</tr>
<tr>
<td colspan="3" class="text_center"><b>Therapy :</b></td>
</tr>
<tr>
<td>
<%= check_box_tag 'physical_therapy', {}, therapy.physical_therapy, disabled: true %><span class="mrg_lf_5px"> Physical Therapy</span>
</td>
<td>
<%= check_box_tag 'fce', {}, therapy.fce, disabled: true %><span class="mrg_lf_5px"> FCE</span>
</td>
<td>
<%= check_box_tag 'work_conditioning', {}, therapy.work_conditioning, disabled: true %><span class="mrg_lf_5px"> Work Conditioning</span>
</td>
</tr>
<tr>
<td>
<%= check_box_tag 'occupational_therapy', {}, therapy.occupational_therapy, disabled: true %><span class="mrg_lf_5px"> Occupational Therapy</span>
</td>
<td>
<%= check_box_tag 'work_hardening', {}, therapy.work_hardening, disabled: true %><span class="mrg_lf_5px"> Work Hardening</span>
</td>
<td></td>
</tr>
<tr>
<td>PT/OT Script</td>
<td>
PT <b><%= therapy.pt_per_week.to_i %></b> /week for <b><%= therapy.pt_for_week.to_i %></b> weeks.
</td>
<td>
Total Visits : <b><%= (patient.decorate.get_total_visits) %></b>
</td>
</tr>
<tr>
<td colspan="3" class="border_bottom">
<div class="fs-row">
<div class="mrg_btm_5"><%= patient.additional_comment.html_safe %></div>
<div><%= patient.general_note.html_safe %></div>
</div>
</td>
</tr>
<tr>
<td colspan="3" class="text_center">
2625 W. Harrison Bellwood, IL 60104 Phone (708) 547-3555 Fax (630) 385-0051
</td>
</tr>
</tbody>
</table>
</div>
</div>



custom2.css


.facesheet-box {
border: 1px solid #dbdbdb;
padding: 10px;
border-radius: 5px;
margin-bottom: 20px;
}

.facesheet-logo, .mr_left_30 {
margin-left: 30px;
margin-bottom: 20px;
}

.facesheet-table {
margin: 10px 0 0 !important;
}

.facesheet-table thead {
background: #f5f5f5 none repeat scroll 0 0;
}

.fs-row {
padding: 10px;
}

.width_half {
width: 50%;
}

.width_33_per {
width: 33%;
}

.diagnosis_table {
width: 100%;
margin-top: 0px !important;
}

.text_center {
text-align: center;
}

.border_bottom {
border-bottom: solid 1px #ddd;
}

.mrg_btm_5 {
margin-bottom: 15px;
}


Step-7 Create Mailer class to perform sending operation

class PatientMailer < ApplicationMailer
default from: APP_CONFIG['support_email']

def send_facesheet(facesheet, patient, partner)
attachments["#{ patient.name.capitalize }_Facesheet.pdf"] = facesheet
patient.attachments.each do |file|
attachments[file.file_name] = open(file.attachment_url).read
end
@partner = partner
mail({
to: @partner.email,
subject: "New Referral from CompCorePro"
}).deliver
end
end


Step 8 Create the view of Mail



Comments