ruby-on-rails - 如何使用 Prawn 设置页面大小?

标签 ruby-on-rails ruby pdf rubygems 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

当我改变这一行时:

pdf = CardPdf.new(@card)

为此:

pdf = Prawn::Document.new(:page_size => "A6", :page_layout => :landscape)

它有效,但我不再看到我的 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

最佳答案

这主要是因为您不再在

中调用 @card
pdf = Prawn::Document.new(:page_size => "A6", :page_layout => :landscape)

最好使用这样的东西:

def initialize(...,...)
  super :page_size => "A4", :page_layout => :landscape
end

在这个文件中:

class ....Pdf < Prawn::Document

关于ruby-on-rails - 如何使用 Prawn 设置页面大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35142383/

相关文章:

ruby-on-rails - 多个服务器上 Rails 中奇怪的 ActiveRecord 时区行为

ruby-on-rails - 删除字符串第一个字符的最有效方法是什么?

Python PDF/图像表重建选项

c# - .NET 中的 PDF 阅读器

ruby-on-rails - Rails 中的迁移(sqlite)

ruby-on-rails - Rails image_tag 宽度属性使用百分比/自动值?

ruby-on-rails - 使用远程 JSON 数据每 30 秒更新一次 MongoDB?

ruby - github-pages gem 失败,即使我有 Ruby 版本 2.1.2

ruby-on-rails - 是否有 Ruby 数据库迁移 gem,可帮助您将内容从旧结构移动到新结构?

c# - 使用 MigraDoc DocumentRenderer 在 PDF 中显示 Unicode 字符