Prawn show the message TypeError: "pdf" is not a class/module

44 views
Skip to first unread message

Diego Bertaso

unread,
Sep 29, 2019, 11:17:46 AM9/29/19
to Prawn

Hi Everybody.
Greetings.

I am presented with the error "" pdf "is not a class / module" when I try to create an instance of the PriceGacetaPdf class which inherits from Prawn :: Document.

Below I attach the code to see if any of you can tell me what I am doing wrong.

I am using Ruby 2.4.6, Rails 4.2.10 and Prawn 2.2.2.

mime_types.rb

# Be sure to restart your server when you modify this file.

# Add new mime types for use in respond_to blocks:
# Mime::Type.register "text/richtext", :rtf
# Mime::Type.register_alias "text/html", :iphone
 Mime::Type.register "application/pdf", :pdf unless Mime::Type.lookup_by_extension(:pdf)


Code in Controller

  def show
    @precio_gaceta = PrecioGaceta.find(params[:id])

     respond_to do |format|
       format.html
       format.pdf do
         pdf = PrecioGacetaPdf.new
         send_data pdf.render, filename: "Precio#{@precio_gaceta.id.to_s}.pdf",
                                         type: "application/pdf",
                                         disposition: "inline"
       end
     end
  end


Code in file precio_gaceta_pdf,rb

class PrecioGacetaPdf < Prawn::Document

  def initialize
    puts "Entro en Initialize"
    super(:page_size=>"LETTER")
    puts "paso por super"
    text "Precio Gaceta va aquí"
  end

  def new
  end
end



Result after execute the sentence pdf = PrecioGacetaPdf.new

TypeError: "pdf" is not a class/module
    from /home/dbertaso/.rvm/gems/ruby-2.4.6@rails42-ruby246/gems/pdf-core-0.7.0/lib/pdf/core/document_state.rb:14:in `initialize'
    from /home/dbertaso/.rvm/gems/ruby-2.4.6@rails42-ruby246/gems/prawn-2.2.2/lib/prawn/document.rb:211:in `new'
    from /home/dbertaso/.rvm/gems/ruby-2.4.6@rails42-ruby246/gems/prawn-2.2.2/lib/prawn/document.rb:211:in `initialize'
    from /home/dbertaso/.rvm/gems/ruby-2.4.6@rails42-ruby246/gems/prawn-2.2.2/lib/prawn/document.rb:142:in `new'
    from /home/dbertaso/.rvm/gems/ruby-2.4.6@rails42-ruby246/gems/prawn-2.2.2/lib/prawn/document.rb:142:in `generate'
    from (irb):2
    from /home/dbertaso/.rvm/gems/ruby-2.4.6@rails42-ruby246/gems/railties-4.2.10/lib/rails/commands/console.rb:110:in `start'
    from /home/dbertaso/.rvm/gems/ruby-2.4.6@rails42-ruby246/gems/railties-4.2.10/lib/rails/commands/console.rb:9:in `start'
    from /home/dbertaso/.rvm/gems/ruby-2.4.6@rails42-ruby246/gems/railties-4.2.10/lib/rails/commands/commands_tasks.rb:68:in `console'
    from /home/dbertaso/.rvm/gems/ruby-2.4.6@rails42-ruby246/gems/railties-4.2.10/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /home/dbertaso/.rvm/gems/ruby-2.4.6@rails42-ruby246/gems/railties-4.2.10/lib/rails/commands.rb:17:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'


Thanks in advance for your help.



Alexander Mankuta

unread,
Sep 29, 2019, 11:23:54 AM9/29/19
to Prawn
Hi,

It's hard to tell what actually is the issue.

However, I should note that you shoul not inherit from Prawn::Document. It's very easy to break it that way. I'd suggest either creating a wrapper class that hold a document instance as one of its internal fields or if you really must use Prawn::View.

--
Regards,
Alex

Diego Bertaso

unread,
Sep 29, 2019, 11:58:35 AM9/29/19
to prawn...@googlegroups.com
Hello Alexander.

Thank you very much for taking time to respond.

I must say that I don't understand very well when you mean "I would suggest creating a container class that contains a document instance as one of its internal fields or if you really should use Prawn :: View.". The code I placed in the consultation I did following the official Prwan documentation, in it I did not find any reference to the class "Prawn :: View", I will review the documentation again to see if I get any reference to that class.

Thanks again for your time.
--
You received this message because you are subscribed to the Google Groups "Prawn" group.
To unsubscribe from this group and stop receiving emails from it, send an email to prawn-ruby+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/prawn-ruby/a0931133-9094-4e6e-92ac-38e46d57a965%40googlegroups.com.

Alexander Mankuta

unread,
Sep 29, 2019, 12:03:57 PM9/29/19
to Prawn
I'm pretty sure documentation never advices to subclass Prawn::Document.

Wrapper class approach example:

class MyDoc
  dev initialize
    @doc = PrawnDocumen.new
  end

  def render
    @doc.text "This is just an example"
    @doc.render
  end
end

And here's documentation for Prawn::View.

--
Regards,
Alex
To unsubscribe from this group and stop receiving emails from it, send an email to prawn-ruby+unsubscribe@googlegroups.com.

Cliff Dickinson

unread,
Jan 23, 2020, 3:42:08 PM1/23/20
to Prawn
Hi Diego,

Did you ever solve the original TypeError: "pdf" is not a class/module error? 

I'm experiencing the same error using prawn 2.2.2 in a Rails 5.2 application with Ruby 2.3.1. I get the error either way I attempt to create a document (calling Prawn::Document.new or creating a class including Prawn::View). Any suggestions in resolving the error?

Thanks,
Cliff

To unsubscribe from this group and stop receiving emails from it, send an email to prawn...@googlegroups.com.

Diego Bertaso

unread,
Mar 6, 2020, 11:20:37 AM3/6/20
to Prawn
Hello Cliff.

Sorry for the delay in responding, but I had no internet service due to problems with the provider, recently restored. In response to your question, I have not yet been able to solve the error, the message still appears despite the fact that I have tried several solutions that appear on the web. If you have any solution, I would greatly appreciate your help. Thank you so much.
Reply all
Reply to author
Forward
0 new messages