首页 文章

Rails Prawn将我的索引表转换为pdf

提问于
浏览
0

我正在尝试创建一个列出我所有项目的.pdf(#index) .

我发现了一个很棒的链接 - How do generate PDFs in Rails with Prawn,但它是从2008年开始的,并希望我使用prawnto插件 .

我正在使用Rails 3.2.13所以我决定使用 gem prawnRailsCast #153 PDFs with Prawn (revised)作为参考 . 我能成功地在我的工作中得到 Prawn

projects_controller

    def show

我无法在我的 def index 中使用.pdfs .

我试图模仿我做了什么,使用tutoiral for def showdef index 但是我遇到了路由错误 .

这是我到目前为止的代码:

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>

我然后格式化我的文件 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

然后我试图模仿最后一个文件以使#index工作

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

一切都很适合 #show . 我显然已经让自己搞砸了如何做 #index 部分(def active,链接index.html.erb和projectall_pdf.rb中的.pdf)

1 回答

  • 1

    我想我会回答我的问题,希望它对某人有所帮助 .

    我实际上继续使用 'gem prawnto_2', :require => "prawnto"

    它允许我使用 prawnto 和prawnto教程 Rails 3.2

    然后我在app / views / projects文件夹中创建了 (method).pdf.prawn 页面 .

    然后只需添加自定义pdf代码即可布局pdf视图 .

相关问题