Reputation: 203
I am trying to create a .pdf that lists all of my projects(#index).
I found a great link-How do generate PDFs in Rails with Prawn, however it was from 2008 and wanted me to use the prawnto plugin.
I am using Rails 3.2.13 so I decided to use the gem prawn
and RailsCast #153 PDFs with Prawn (revised), for reference. I was able to successfully get Prawn
working in my:
projects_controller
def show
I am having trouble getting the .pdfs working in my def index
though.
I tried to just mimic what I did, using the tutoiral for def show
, for def index
but am getting a routing error.
Here is my code thus far:
Gemfile
gem 'prawn', '0.12.0'
projects_controller.rb
class ProjectsController < ApplicationController
def index
redirect_to action: :active, search =>params[:search]
end
def active
@action = "active"
....
.... // search code
.... // kaminari code
@projects = Project.order(sort_column + "" + sort_direction)
respond_to do |format|
format.json { render "index" }
format.html { render "index" }
format.pdf do
pdf = ProjectAllPdf.new(@projects)
send_data pdf.render, filename: "project_#{@project.product}.pdf",
type: "application/pdf",
disposition: "inline"
end
end
end
def show
@project = Project.find(params[:id])
respond_to do |format|
format.json { render json:@project }
format.html # show.html.erb
format.pdf do
pdf = ProjectPdf.new(@project)
send_data pdf.render, filename: "project_#{@project.product}.pdf",
type: "application/pdf",
disposition: "inline"
end
end
end
end
show.html.erb
<p><%= link_to "Printable Receipt (PDF)", project_path(@project, format: "pdf") %></p>
index.html.erb
<p><%= link_to "Printable Receipt (PDF)", projects_path(@projects, format: "pdf") %></p>
I then formatted my file project_pdf.rb
class ProjectPdf < Prawn::Document
def initialize(project)
super(top_margin: 70)
@project = project
overview_print
end
def overview_print
text "Project #{@project.product}", size: 24, style: :bold, align: :center
move_down 30
text "<b>Product:</b> #{@project.product}", :inline_format => true
move_down 8
text "<b>Version Number:</b> #{@project.version_number}", :inline_format => true
move_down 8
....
....
end
end
I then tried to mimic the last file to get #index working
projectall_pdf.rb
class ProjectAllPdf < Prawn::Document
def initialize(project)
super(top_margin: 70)
@project = project
overview_print
end
def overview_print
@projects.each do |project|
text "<b>Product:</b> #{@project.product}", :inline_format => true
move_down 8
text "<b>Version Number:</b> #{@project.version_number}", :inline_format => true
move_down 8
....
....
end
end
end
Everything works great for #show
. I just obviously have gotten myself mixed up on how to do the #index
portions (def active, linking the .pdf in index.html.erb and projectall_pdf.rb)
Upvotes: 0
Views: 531
Reputation: 203
I thought I would post an answer to my question, hopefully it helps somebody.
I actually went ahead and used the 'gem prawnto_2', :require => "prawnto"
It allowed me to use the prawnto
and the prawnto tutorial with Rails 3.2
I then just created (method).pdf.prawn
pages in my app/views/projects folder.
Then just add your custom pdf code to have you want to layout your pdf views.
Upvotes: 1