【问题标题】:page layout on prawn unable to set inside class虾的页面布局无法在课堂内设置
【发布时间】:2017-01-29 11:25:05
【问题描述】:

我在大虾中搜索了大虾的页面布局,它显示了这个

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

pdf.text 不打印任何东西

但是当我尝试这个时,我得到了未定义的方法

page_layout :landscape

在 super() 之后添加

这是我的全部代码

class ProductPdfGenerate < Prawn::Document
    require 'open-uri'
    def initialize(order_items)
        super()
        @document = Prawn::Document.new(:page_size => "A4", :page_layout => :landscape)
        @order_items = order_items
        @order_items.each_with_index do |oi, i|
            if oi.case.present? && Model.where(magento_model_id: oi.case.model_id).first.present?
                style_image = oi.case.image_preview.url(:custom_image)
                model = Model.where(magento_model_id: oi.case.model_id).first
                # image open(style_image), width: "200".to_f, height: "400".to_f
                image open(style_image), width: "#{model.aspect_ratio_width.to_f/2.54*72}".to_f, height: "#{model.aspect_ratio_height.to_f/2.54*72}".to_f
                text "\n \n \n"
                text "Model: #{model.name}"
                text "Model Category: #{model.category_type}"
                text "Style: #{oi.case.style.try(:name)} "
                text "Order Id: #{oi.order_id}"
            else
                image open("https://s3.ap-south-1.amazonaws.com/take-my-order/default/missing.png")
            end
        end
    end
end

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4 prawn


    【解决方案1】:

    试试这个适用于我的代码的语法:

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

    【讨论】:

      【解决方案2】:

      这是一个相当古老的问题,但是我在阅读 Prawn 基础知识的过程中发现了它,因此还是想给出一个答案(希望对未来的访问者有所帮助)。

      根据the Prawn Manual(第4页),有三种方法可以实例化一个新的PDF。在这里,OP选择了第一种方式 @document = Prawn::Document.new...。 要更改页面(例如向其中添加文本),您现在必须调用

      @document.text "Model: #{model.name}"
      

      而不仅仅是text "Model: #{model.name}"

      但是,有一种方法可以省略@document 的重复调用,就像这样的块调用:

      Prawn::Document.generate(<generator options like layout etc here>) do
        text "Model: #{model.name}"
      end
      

      这样,方法“text”可以成功映射到调用block方法“generate”创建的对象,不需要进一步的说明(例如@document.text)。

      编码愉快。

      【讨论】:

        猜你喜欢
        • 2015-01-12
        • 1970-01-01
        • 1970-01-01
        • 2011-07-04
        • 1970-01-01
        • 1970-01-01
        • 2012-10-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多