Ruby on Rails Prawn PDF Basic Example

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

You need to add Gem and PDF MIME:Type inside mime_types.rb as we need to notify rails about PDF mime type.

After that we can generate Pdf with Prawn in following basic ways

This is the basic assignment

pdf = Prawn::Document.new
pdf.text "Hello World"
pdf.render_file "assignment.pdf"

We can do it with Implicit Block

Prawn::Document.generate("implicit.pdf") do
 text "Hello World"
end

With Explicit Block

Prawn::Document.generate("explicit.pdf") do |pdf|
 pdf.text "Hello World"
end


Got any Ruby on Rails Question?