Merge pull request #1595 from siddhantbajaj/mail-pdf

Added ticket pdf prawn document
This commit is contained in:
Moisés Déniz Alemán 2017-07-18 12:02:57 +02:00 committed by GitHub
commit 29aee82a14
3 changed files with 91 additions and 62 deletions

View file

@ -13,5 +13,15 @@ class PhysicalTicketController < ApplicationController
@file_name = "ticket_for_#{@conference.short_title}"
@user = @physical_ticket.user
@ticket_layout = @conference.ticket_layout.to_sym
respond_to do |format|
format.html
format.pdf do
pdf = TicketPdf.new(@conference, @user, @physical_ticket, @ticket_layout, @file_name)
send_data pdf.render,
filename: @file_name,
type: 'application/pdf',
disposition: 'attachment'
end
end
end
end

81
app/pdfs/ticket_pdf.rb Normal file
View file

@ -0,0 +1,81 @@
class TicketPdf < Prawn::Document
def initialize(conference, user, physical_ticket, ticket_layout, file_name)
super(page_layout: ticket_layout, page_size: 'A4', filename: file_name)
@user = user
@physical_ticket = physical_ticket
@conference = conference
@left = bounds.left
@right = bounds.right
@mid_vertical = (bounds.top - bounds.bottom) / 2
@mid_horizontal = (bounds.right - bounds.left) / 2
@x = 0
draw_first_square
draw_second_square
draw_third_square
draw_fourth_square
end
def draw_first_square
move_down @mid_vertical
dash(2, space: 1)
stroke_horizontal_rule
stroke_vertical_line bounds.top, bounds.bottom, at: @mid_horizontal
move_up @mid_vertical
draw_text 'TICKET HOLDER', at: [@x, cursor - 30], size: 17
dash(2, space: 0)
stroke_rectangle [@x, cursor - 50], 230, 150
move_down 80
draw_text 'NAME', at: [@x + 10, cursor], size: 13
fill_color '808080'
draw_text @user.name.to_s, at: [@x + 10, cursor - 25], size: 20
fill_color '000000'
draw_text 'EMAIL', at: [@x + 10, cursor - 50], size: 13
fill_color '808080'
draw_text @user.email.to_s, at: [@x + 10, cursor - 75], size: 20
fill_color '000000'
move_up 20
end
def draw_second_square
if @conference.picture?
if 7 * @conference.picture.image[:width] > 12 * @conference.picture.image[:height]
image "#{Rails.root}/public#{@conference.picture_url}", at: [@mid_horizontal + 30, cursor], width: 120
else
image "#{Rails.root}/public#{@conference.picture_url}", at: [@mid_horizontal + 30, cursor], height: 70
end
else
image "#{Rails.root}/public/img/osem-logo.png", at: [@mid_horizontal + 30, cursor], height: 70
end
move_down 70
draw_text @conference.title.to_s, at: [@mid_horizontal + 30, cursor - 30], size: 12
draw_text @conference.organization.name.to_s, at: [@mid_horizontal + 30, cursor - 50], size: 12
draw_text @conference.venue.name.to_s, at: [@mid_horizontal + 30, cursor - 70]
move_up 130
move_down @mid_vertical
end
def draw_third_square
draw_text 'EVENT', at: [@x, cursor - 40], size: 15
fill_color '808080'
draw_text @conference.title.to_s, at: [@x, cursor - 60], size: 12
draw_text @conference.start_date.strftime('%B %d, %Y').to_s, at: [@x, cursor - 80], size: 12
move_down 80
fill_color '000000'
draw_text 'TICKET', at: [@x, cursor - 30], size: 15
fill_color '808080'
draw_text @physical_ticket.ticket.title.to_s, at: [@x, cursor - 50], size: 12
move_down 50
fill_color '000000'
draw_text 'TICKET REF.', at: [@x, cursor - 30], size: 15
fill_color '808080'
draw_text @physical_ticket.ticket_purchase.id.to_s, at: [@x, cursor - 50], size: 12
move_down 50
fill_color '000000'
draw_text 'Powered By OSEM', at: [(@mid_horizontal - @left - 100) / 2, cursor - 100], size: 11
move_up 180
end
def draw_fourth_square; end
end

View file

@ -1,62 +0,0 @@
prawn_document(filename: @file_name, page_layout: @ticket_layout, :page_size =>'A4' ) do |pdf|
# Vertical Layout
top = pdf.bounds.top
bottom = pdf.bounds.bottom
left = pdf.bounds.left
right = pdf.bounds.right
mid_vertical = (pdf.bounds.top-pdf.bounds.bottom)/2
mid_horizontal = (pdf.bounds.right-pdf.bounds.left)/2
x = 0
pdf.move_down mid_vertical
pdf.dash(2, :space => 1)
pdf.stroke_horizontal_rule
pdf.stroke_vertical_line pdf.bounds.top, pdf.bounds.bottom, :at => mid_horizontal
pdf.move_up mid_vertical
pdf.draw_text "TICKET HOLDER", :at => [x,pdf.cursor-30], :size => 17
pdf.dash(2, :space => 0)
pdf.stroke_rectangle [x, pdf.cursor-50], 230, 150
pdf.move_down 80
pdf.draw_text "NAME", :at => [x+10,pdf.cursor], :size => 13
pdf.fill_color "808080"
pdf.draw_text "#{@user.name}", :at => [x+10,pdf.cursor-25], size: 20
pdf.fill_color "000000"
pdf.draw_text "EMAIL", :at => [x+10,pdf.cursor-50], :size => 13
pdf.fill_color "808080"
pdf.draw_text "#{@user.email}", :at => [x+10,pdf.cursor-75], size: 20
pdf.fill_color "000000"
pdf.move_up 20
if @conference.picture?
if 7 * @conference.picture.image[:width] > 12 * @conference.picture.image[:height]
pdf.image "#{Rails.root}/public#{@conference.picture_url}", :at => [mid_horizontal+30, pdf.cursor], :width => 120
else
pdf.image "#{Rails.root}/public#{@conference.picture_url}", :at => [mid_horizontal+30, pdf.cursor], :height => 70
end
else
pdf.image "#{Rails.root}/public/img/osem-logo.png", :at => [mid_horizontal+30, pdf.cursor], :height => 70
end
pdf.move_down 70
pdf.draw_text "#{@conference.title}", :at => [mid_horizontal+30,pdf.cursor-30], :size => 12
pdf.draw_text "#{@conference.organization.name}", :at => [mid_horizontal+30,pdf.cursor-50], :size => 12
pdf.draw_text "#{@conference.venue.name}", :at => [mid_horizontal+30,pdf.cursor-70]
pdf.move_up 130
pdf.move_down mid_vertical
pdf.draw_text "EVENT", :at => [x,pdf.cursor-40], :size => 15
pdf.fill_color "808080"
pdf.draw_text "#{@conference.title}", :at => [x,pdf.cursor-60], size: 12
pdf.draw_text "#{@conference.start_date.strftime('%B %d, %Y')}", :at => [x,pdf.cursor-80], size: 12
pdf.move_down 80
pdf.fill_color "000000"
pdf.draw_text "TICKET", :at => [x,pdf.cursor-30], :size => 15
pdf.fill_color "808080"
pdf.draw_text "#{@physical_ticket.ticket.title}", :at => [x,pdf.cursor-50], size: 12
pdf.move_down 50
pdf.fill_color "000000"
pdf.draw_text "TICKET REF.", :at => [x,pdf.cursor-30], :size => 15
pdf.fill_color "808080"
pdf.draw_text "#{@physical_ticket.ticket_purchase.id}", :at => [x,pdf.cursor-50], size: 12
pdf.move_down 50
pdf.fill_color "000000"
pdf.draw_text "Powered By OSEM", :at => [(mid_horizontal-left-100)/2,pdf.cursor-100], :size => 11
pdf.move_up 180
end