3 ruby pdf rubygems ruby-on-rails prawn
我只能使用以下方法将页面大小设置为默认字母大小:
def show
@card = Card.find(params[:id])
respond_to do |format|
format.html
format.pdf do
pdf = CardPdf.new(@card)
send_data pdf.render, filename: "#{@card.entrant_first_name}_#{@card.entrant_surname}_#{@card.section}.#{@card.category}.pdf",
type: "application/pdf",
disposition: "inline"
end
end
end
Run Code Online (Sandbox Code Playgroud)
当我更改此行时:
pdf = CardPdf.new(@card)
Run Code Online (Sandbox Code Playgroud)
对此:
pdf = Prawn::Document.new(:page_size => "A6", :page_layout => :landscape)
Run Code Online (Sandbox Code Playgroud)
它可以工作,但我不再从card_pdf.rb文件中看到内容。
这是我的CardPdf:
class CardPdf < Prawn::Document
def initialize(card)
super()
@card = card
font_families.update("Roboto" => {
:normal => "#{Prawn::BASEDIR}/fonts/Roboto.ttf"
})
font_families.update("SourceCodePro" => {
:normal => "#{Prawn::BASEDIR}/fonts/SourceCodePro.ttf"
})
font("Roboto")
header
move_down 25
page_title
move_down 20
card_info
end
def horizontal
stroke do
move_down 15
stroke_color 'f3f3f3'
line_width 2
stroke_horizontal_rule
move_down 15
end
end
def header
text "Dalgety Bay Horticultural Society", size: 10, :color => "b9b9b9", :character_spacing => 1
end
def page_title
text "Card", size: 32,:color => "222222", :character_spacing => 1
end
def card_info
horizontal
end
end
Run Code Online (Sandbox Code Playgroud)
这主要是因为你不再调用@card中
pdf = Prawn::Document.new(:page_size => "A6", :page_layout => :landscape)
Run Code Online (Sandbox Code Playgroud)
最好使用这样的东西:
def initialize(...,...)
super :page_size => "A4", :page_layout => :landscape
end
Run Code Online (Sandbox Code Playgroud)
在这个文件中:
class ....Pdf < Prawn::Document
Run Code Online (Sandbox Code Playgroud)