From 7442993e35bd866c377d5633ed3e502d9d9e04b2 Mon Sep 17 00:00:00 2001 From: rulingcom Date: Wed, 22 Oct 2025 22:48:46 +0800 Subject: [PATCH] added pdf generation and email --- app/controllers/admin/asks_controller.rb | 113 ++++++++++++++++++++++- app/models/ask_question.rb | 1 + app/views/admin/asks/_form.html.erb | 6 ++ app/views/admin/asks/print.html.erb | 2 +- ask.gemspec | 2 + config/locales/en.yml | 2 + config/locales/zh_tw.yml | 2 + config/routes.rb | 1 + 8 files changed, 127 insertions(+), 2 deletions(-) diff --git a/app/controllers/admin/asks_controller.rb b/app/controllers/admin/asks_controller.rb index fe9421d..0cf5d17 100644 --- a/app/controllers/admin/asks_controller.rb +++ b/app/controllers/admin/asks_controller.rb @@ -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 = ""+ print_text + print_text = process_format_text(print_text,custom_fields) + # Render the print format explanation partial + html_content = <<-HTML + + + + + + + + #{print_text} + + + 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 diff --git a/app/models/ask_question.rb b/app/models/ask_question.rb index ff121b3..9c7ce38 100644 --- a/app/models/ask_question.rb +++ b/app/models/ask_question.rb @@ -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: {} diff --git a/app/views/admin/asks/_form.html.erb b/app/views/admin/asks/_form.html.erb index cd67f6e..522992f 100644 --- a/app/views/admin/asks/_form.html.erb +++ b/app/views/admin/asks/_form.html.erb @@ -151,6 +151,12 @@ <%= t('ask.paper') %> +
+ +
<%= f.label :reply, class: "control-label muted" %> diff --git a/app/views/admin/asks/print.html.erb b/app/views/admin/asks/print.html.erb index aa2116a..dce6369 100644 --- a/app/views/admin/asks/print.html.erb +++ b/app/views/admin/asks/print.html.erb @@ -41,7 +41,7 @@ }