Anbazhagan p
Anbazhagan p

Reputation: 943

page layout on prawn unable to set inside class

i have searched in prawn about page layout in prawn and it showed this

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

pdf.text doesnt print anything

but when i try this i'm getting undefined method

page_layout :landscape

added after super()

this is my whole code

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

Upvotes: 0

Views: 532

Answers (2)

randmin
randmin

Reputation: 948

This is a fairly old question, however I found it during my journey through the Prawn basics and thus want to give an answer nevertheless (that hopefully helps future visitors).

According to the Prawn Manual (Page 4), there are three ways to instanciate a new PDF. Here, the OP has chosen the first way @document = Prawn::Document.new.... To change the page (e.g. add text to it), you would now have to call

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

instead of just text "Model: #{model.name}".

However, there is a way to omit the repeated call of @document, being a block call like so:

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

This way, the method "text" can be successfully mapped to the object created by calling the block method "generate" and needs no further specification (e.g. @document.text).

Happy coding.

Upvotes: 1

Ren
Ren

Reputation: 1372

Try this syntax, which works for my code:

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

Upvotes: 1

Related Questions