added pdf generation and email

This commit is contained in:
rulingcom 2025-10-22 22:48:46 +08:00
parent 40722b7070
commit 7442993e35
8 changed files with 127 additions and 2 deletions

View File

@ -339,6 +339,17 @@ class Admin::AsksController < OrbitAdminController
@print_text = process_format_text(@print_text,custom_fields)
@save_name = process_format_text(@save_name,custom_fields)
end
def print_pdf
@ask_question = AskQuestion.find(params[:id])
pdf_content = generate_pdf(@ask_question)
send_data pdf_content,
filename: "ask_question_#{@ask_question.id}.pdf",
type: 'application/pdf',
disposition: 'attachment'
end
def edit
@ask_setting = AskCategorySetting.where(category_id: @ask_question.category_id.to_s, :use_default.ne=>true).first
@ask_setting = AskSetting.first if @ask_setting.nil?
@ -404,14 +415,85 @@ class Admin::AsksController < OrbitAdminController
# 比對相鄰兩筆
histories.each_cons(2) do |h1, h2|
# 判斷是否為重複:同一狀態,且時間差小於 1 秒
if h2.status == h1.status && (h2.created_at - h1.created_at).abs < 1.second
if h2.status == h1.status && (h2.created_at - h1.created_at).abs < 1.second
h2.destroy
end
end
redirect_to admin_asks_path(:locale=>locale), notice: t('ask.reply_success')
end
# Adding this helper method for PDF generation
def generate_pdf(ask_question)
@ask_question = ask_question # Set instance variable for process_format_text
# Get settings similar to print method
print_setting = AskCategoryPrintSetting.where(category_id: ask_question.category_id.to_s).first
print_setting = AskPrintSetting.first if print_setting.nil?
# Get custom fields
ask_setting = AskCategorySetting.where(category_id: ask_question.category_id.to_s, :use_default.ne=>true).first
ask_setting = AskSetting.first if ask_setting.nil?
custom_fields = ask_setting.custom_fields
print_text = print_setting.print_format rescue ''
print_text = "<style>img{width: 100%;}</style>"+ print_text
print_text = process_format_text(print_text,custom_fields)
# Render the print format explanation partial
html_content = <<-HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
font-family: Arial, sans-serif;
font-size: 12px;
line-height: 1.4;
}
table {
width: 100%;
border-collapse: collapse;
margin-bottom: 20px;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
img {
max-width: 100%;
height: auto;
}
</style>
</head>
<body>
#{print_text}
</body>
</html>
HTML
# Generate PDF using WickedPDF
WickedPdf.new.pdf_from_string(
html_content,
encoding: 'UTF-8',
page_size: 'A4',
margin: {
top: 20,
bottom: 20,
left: 20,
right: 20
},
print_media_type: true,
zoom: 1,
dpi: 300,
footer: {
font_size: 8,
right: '[page] of [topage]',
spacing: 5
}
)
end
# Modify the build_email method to include PDF attachment
def build_email(email_er)
email = Email.new
email.save
@ -432,6 +514,32 @@ class Admin::AsksController < OrbitAdminController
host_url = request.protocol + request.host_with_port
end
# Generate PDF
pdf_file_record = nil
if email_er.generate_pdf
pdf_content = generate_pdf(email_er)
# Create temporary file
temp_file = Tempfile.new(['reply_template', '.pdf'])
temp_file.binmode
temp_file.write(pdf_content)
temp_file.rewind
# Create EmailFile record
pdf_file_record = EmailFile.new(
title: "reply_template_#{email_er.id}.pdf"
)
# Attach the temporary file to the EmailFile record
pdf_file_record.file = temp_file
pdf_file_record.save
# Close and delete temp file
temp_file.close
temp_file.unlink
end
# Update email with template data including PDF
email_er.email.update_attributes(
:mail_lang=> site['default_locale'],
:create_user=>current_user,
@ -446,6 +554,9 @@ class Admin::AsksController < OrbitAdminController
"attachment" => (new_history ? new_history.file.url : nil)
}
)
if pdf_file_record
email_er.email.email_files << pdf_file_record
end
end
def export

View File

@ -23,6 +23,7 @@ class AskQuestion
field :bcc_emails, type: String
field :situation, type: String, default: "is_email_not_confirmed" # 預設email未驗證
field :send_email, type: Integer, default: 0
field :generate_pdf, type: Boolean, default: false
field :email_id
field :verify_email_id
field :custom_values, type: Hash, default: {}

View File

@ -151,6 +151,12 @@
<%= t('ask.paper') %>
</label>
</div>
<div class="controls">
<label class="checkbox inline">
<%= f.check_box :generate_pdf %>
<%= t('ask.attach_pdf_to_email') %>
</label>
</div>
</div>
<div class="control-group">
<%= f.label :reply, class: "control-label muted" %>

View File

@ -41,7 +41,7 @@
}
</script>
<div class="no-print" style="text-align: center;">
<input onclick="save_data()" value="<%= I18n.t('ask.save_data') %>" type="button">
<a href="/<%= I18n.locale %>/admin/asks/<%= @ask_question.id.to_s %>/print_pdf" target="_blank"><%= t('ask.save_data') %></a>
</div>
<div class="print_text">
<%= @print_text.html_safe %>

View File

@ -37,4 +37,6 @@ Gem::Specification.new do |s|
"global_hash" => "{last_ticket_key: (AskSetting.pluck(:last_ticket_key)[0].to_i rescue 0), last_serial_number: (AskSetting.pluck(:last_serial_number)[0].to_i rescue 0)}"
}
# s.add_dependency ~> "gotcha"
s.add_dependency 'wicked_pdf', '~> 2.6'
s.add_dependency 'wkhtmltopdf-binary', '~> 0.12.6'
end

View File

@ -144,6 +144,8 @@ en:
check_status: Check Status
check_history: Check History
email_id: Email
attach_pdf_to_email: Attach PDF template to email replies
pdf_attachment_notice: "A PDF document has been attached to this email"
mongoid:
attributes:

View File

@ -154,6 +154,8 @@ zh_tw:
check_status: 查詢案件進度
check_history: 查詢
email_id: 電子郵件
attach_pdf_to_email: 回覆信附加PDF範本
pdf_attachment_notice: "此郵件已附加PDF文件"
mongoid:
attributes:

View File

@ -64,6 +64,7 @@ Rails.application.routes.draw do
delete 'delete'
post 'batch_modify_status'
get ':id/print', to: 'asks#print'
get ':id/print_pdf', to: 'asks#print_pdf'
get ':ask_status_id/download', to: 'asks#download_file'
get 'export'
get 'setting'