added priting and pdf

This commit is contained in:
rulingcom 2025-10-31 23:10:37 +08:00
parent 8b3049b1c7
commit c5fe53d745
10 changed files with 148 additions and 6 deletions

View File

@ -132,6 +132,23 @@ class Admin::PropertyHiresController < OrbitAdminController
redirect_to params[:referer_url] redirect_to params[:referer_url]
end end
def print_hire
@phire = PHire.find(params[:id]) rescue nil
property = @phire.property
@html = print_format(property.print_format, @phire)
end
def print_pdf
@phire = PHire.find(params[:id]) rescue nil
property = @phire.property
pdf_content = generate_pdf(print_format(property.print_format, @phire))
send_data pdf_content,
filename: "reservation_#{property.title.strip.downcase.gsub(/\s+/, "_")}_#{@phire.hiring_person_name.strip.downcase.gsub(/\s+/, "_")}.pdf",
type: 'application/pdf',
disposition: 'attachment'
end
def create def create
tmp_property_params = property_params tmp_property_params = property_params
if tmp_property_params["copy_id"] && tmp_property_params["clone_p_hires"].blank? if tmp_property_params["copy_id"] && tmp_property_params["clone_p_hires"].blank?

View File

@ -65,6 +65,7 @@ module Admin::PropertyHiresHelper
end end
return data return data
end end
def render_custom_text_field(f,field_name,type) def render_custom_text_field(f,field_name,type)
text = "<div> text = "<div>
<div class=\"input-append\"> <div class=\"input-append\">
@ -80,10 +81,11 @@ module Admin::PropertyHiresHelper
end end
text += "</div> text += "</div>
</div> </div>
</div> </div>
</div>" </div>"
text.html_safe text.html_safe
end end
def check_if_user_is_manager? def check_if_user_is_manager?
ma = ModuleApp.find_by_key("property_hire") ma = ModuleApp.find_by_key("property_hire")
if OrbitHelper.current_user.nil? if OrbitHelper.current_user.nil?
@ -93,6 +95,90 @@ module Admin::PropertyHiresHelper
end end
return is_user_manager return is_user_manager
end end
def print_format(print_format, phire)
# Extract field names that are wrapped in curly braces
field_names = print_format.scan(/\{([^}]+)\}/).flatten
# Loop through extracted field names
field_names.each do |field_name|
field = PHire.fields[field_name]
if field
case field.type.to_s.downcase
when "date"
print_format = print_format.gsub("{#{field_name}}", phire.send(field_name).strftime("%Y/%m/%d")) rescue print_format
when "datetime"
print_format = print_format.gsub("{#{field_name}}", phire.send(field_name).strftime("%Y/%m/%d %H:%M")) rescue print_format
else
print_format = print_format.gsub("{#{field_name}}", phire.send(field_name).to_s)
end
else
field = PHireField.where(:key => field_name, :property_id => phire.property_id).first
if field
value = PHireFieldValue.where(:p_hire_field_id => field.id, :p_hire_id => phire.id).first
print_format = print_format.gsub("{#{field_name}}", value ? value.get_value_by_locale(I18n.locale.to_s) : "")
end
end
end
return print_format
end
def generate_pdf(formatted_text)
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>
#{formatted_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
module HireMethod module HireMethod
extend ActionView::Helpers::UrlHelper extend ActionView::Helpers::UrlHelper
extend ActionView::Helpers::TagHelper extend ActionView::Helpers::TagHelper

View File

@ -35,7 +35,7 @@ class Property
field :p_display_end_time, type: DateTime field :p_display_end_time, type: DateTime
field :recurring_enable, type: Boolean, :default => false field :recurring_enable, type: Boolean, :default => false
field :property_color, :default => "#000000" field :property_color, :default => "#000000"
field :print_format field :print_format, type: String, default: ""
mount_uploader :image, ImageUploader mount_uploader :image, ImageUploader

View File

@ -0,0 +1,30 @@
<style>
input[type="button"],button,input[type="submit"] {
outline: 0;
border-radius: 1.3em;
background: #d9e4f7;
font-weight: bold;
outline: 0;
}
input[type="button"]:hover,button:hover,input[type="submit"]:hover {
background: #be8a8a;
}
.print_text{
padding: 2em;
width: 650px;
margin: auto;
}
@media print
{
.no-print, .no-print *
{
display: none !important;
}
}
</style>
<div class="no-print" style="text-align: center;">
<a href="<%= print_pdf_admin_property_hire_path(@phire) %>" target="_blank"><%= t('property_hire.save_data') %></a>
</div>
<div class="print_text">
<%= @html.html_safe %>
</div>

View File

@ -122,6 +122,7 @@
</td> </td>
<td> <td>
<% if can_edit_or_delete?(p_hire.property) %> <% if can_edit_or_delete?(p_hire.property) %>
<a href="<%= print_hire_admin_property_hire_path(p_hire) %>" class="btn btn-info"><%= t("property_hire.print") %></a>
<a href="<%= edit_hire_admin_property_hire_path(p_hire) %>" class="btn btn-info"><%= t("property_hire.edit") %></a> <a href="<%= edit_hire_admin_property_hire_path(p_hire) %>" class="btn btn-info"><%= t("property_hire.edit") %></a>
<a href="<%= show_booking_details_admin_property_hire_path(p_hire, :page => params[:page]) %>" class="btn btn-info"><%= t("property_hire.view") %></a> <a href="<%= show_booking_details_admin_property_hire_path(p_hire, :page => params[:page]) %>" class="btn btn-info"><%= t("property_hire.view") %></a>
<% if p_hire.passed %> <% if p_hire.passed %>

View File

@ -76,6 +76,7 @@
<% if manager %> <% if manager %>
<% if @booking.passed %> <% if @booking.passed %>
<a href="<%= pass_booking_admin_property_hire_path(@booking, :status => "reject") %>" class="btn btn-warning"><%= t("property_hire.reject") %></a> <a href="<%= pass_booking_admin_property_hire_path(@booking, :status => "reject") %>" class="btn btn-warning"><%= t("property_hire.reject") %></a>
<a href="<%= print_hire_admin_property_hire_path(@booking) %>" class="btn btn-info"><%= t("property_hire.print") %></a>
<% else %> <% else %>
<a href="<%= pass_booking_admin_property_hire_path(@booking, :status => "accept") %>" class="btn btn-success"><%= t("property_hire.accept") %></a> <a href="<%= pass_booking_admin_property_hire_path(@booking, :status => "accept") %>" class="btn btn-success"><%= t("property_hire.accept") %></a>
<% end %> <% end %>

View File

@ -217,3 +217,5 @@ en:
recommendation: Recommendation recommendation: Recommendation
print_format: Print Format print_format: Print Format
print_settings: Print Settings print_settings: Print Settings
print: Print
save_data: Print PDF

View File

@ -239,3 +239,5 @@ zh_tw:
recommendation: 擬辦/註解 recommendation: 擬辦/註解
print_format: 列印格式 print_format: 列印格式
print_settings: 列印格式設定 print_settings: 列印格式設定
print: 列印
save_data: Print PDF

View File

@ -16,6 +16,8 @@ Rails.application.routes.draw do
delete "destroy_location" delete "destroy_location"
get "show_booking_details" get "show_booking_details"
get "pass_booking" get "pass_booking"
get "print_hire"
get "print_pdf"
delete "delete_booking_details" delete "delete_booking_details"
get "edit_hire" get "edit_hire"
get "add_hire" get "add_hire"

View File

@ -39,5 +39,6 @@ Gem::Specification.new do |s|
s.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.rdoc"] s.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.rdoc"]
s.test_files = Dir["test/**/*"] s.test_files = Dir["test/**/*"]
s.add_dependency 'wicked_pdf', '~> 2.6'
s.add_dependency 'wkhtmltopdf-binary', '~> 0.12.6'
end end