Add this gem to the gemfile of your application and run the ‘bundle’ command.
gem 'prawn' Create a PDF Mime::Type inside config/initializers/mime_types.rb. Mime::Type.register "application/pdf", :pdf
I have created a respond block with HTML and PDF format inside my controller. Inside the format.pdf block, the send_data method is called.
respond_to do |format| format.html format.pdf do pdf = CompanylistPdf.new(@company) send_data pdf.render, disposition: "inline" end end
Create a new directory called pdfs inside apps folder and then create a companylist_pdf.rb (you can give any other name depending on the requirement) file inside that directory. After that, create a class inside it and inherit it from the prawn document.
Then we call the super method and display the string that we need to show on our PDF.
You can create different methods inside the class and call it in intialize method to display it on your pdf.
Over here, I have created a logo method and have called it inside intialze method. So the logo appears on the PDF.
class CompanylistPdf < Prawn::Document def initialize(company) super(top_margin: 70) @companies = users text "Mycompany is #{company}", size: 30, style: :bold logo end def logo logopath = "#{Rails.root}/app/assets/images/logo.png" image logopath, :width => 197, :height => 91 end end