1

我是 Ruby 和 RoR 的新手。我使用 Prawn 在 Ruby 中制作了一个 pdf 生成器。现在我想从我的 Rails 视图中执行它。

<%= form_for @assemble do |f| %>
      <div class="row">
        <div class="span2">
          <h2 style="text-align: right;">Your</h2>
          <ul id="download_ul">
            <li style="margin-top: 5px;"><%= f.label :title_top %> </li>
            <li style="margin-top: 13px;"><%= f.label :text_top %></li>
            <li style="margin-top: 13px;"><%= f.label :link %></li>
            <li style="margin-top: 13px;"><%= f.label :title_bottom %></li>
            <li style="margin-top: 13px;"><%= f.label :text_bottom %></li>
            <li style="margin-top: 13px;"><%= f.label :qr_code_url %></li>
            <li style="margin-top: 13px;"><%= f.label :photo %></li>
            <li style="margin-top: 13px;"><%= f.label :logo %></li>
            <li style="margin-top: 10px;"><%= f.label :format %></li>
            <li style="margin-top: 0px;"><%= f.label :cut_lines %></li>
          </ul>
        </div>
        <div class="span3">
          <h2>Information</h2>
          <form method="post" action="../generate_pdf.rb">
            <ul class="download_ul">
              <li><%= f.text_field :title_top %></li>
              <li><%= f.text_area :text_top%></li>
              <li><%= f.text_field :link%></li>
              <li><%= f.text_field :title_bottom %></li>
              <li><%= f.text_area :text_bottom %></li>
              <li><%= f.text_field :qr_code_url %></li>
              <li><%= f.text_field :photo %></li>
              <li><%= f.text_field :logo %></li>
              <li style="margin-top: 6px;">
                A4<%= f.check_box :format_a4 %>
                A5<%= f.check_box :format_a5 %>
                Letter<%= f.check_box :format_letter %>
                Business card<%= f.check_box :format_business_card %>
              </li>
              <li style="margin-top: 6px;">
                True<%= f.check_box :cut_lines_true, f.row %>
                False<%= f.check_box :cut_lines_false %>
              </li>
            </ul>
            <p><%= f.submit %>></p>
          </form>
        </div>
        <div class="span6">
          <h2>Example</h2>
          <p><%= image_tag("a5.png", :size => "420x600") %></p>
        </div>
      </div>
  <% end %>

要在 ruby​​ 中执行我的 prawn-app,我使用 run.rb:

require '../lib/generator.rb'

PDFGenerator::A5.new(filename: "a5.pdf",
  colors: {
    instructions:         "525149",
    instructions_header:  "000000",
    panel:                "001430",
    experience:           "FFFFFF",
    qr_reader:            "525149"
  },
  texts: {
    qr_code_instructions: "*Some text ",
    instructions_header:  "Some text",
    instructions:         "Some text",
    link:                 "Some text",
    experience_header:    "Some text",
    experience:           "Some text"
  },
  photo:   "images/img.jpg",
  qr_code: "images/qr.png",
  logo:    "images/logo.png").generate

我的控制器和模型文件:

class AssembleController < ApplicationController
  def index
    redirect_to new_assemble_path
  end

  def new
    @assemble = Assemble.new
  end
end

class Assemble < ActiveRecord::Base
  # attr_accessible :title, :body
end

我希望很清楚我打算做什么。我很抱歉提出与其他人类似的问题,但我真的没有在我的代码中看到解决方案。

4

2 回答 2

3

您需要在控制器中创建一个操作,该操作应该调用您的脚本并返回生成的 pdf。然后您可以send_file在公共文件夹中使用或保存文件并将其 url 返回给用户。

于 2012-07-18T13:25:26.323 回答
1

正如 Yuri 指出的那样,你必须做出一个控制器动作来做到这一点。 Web 应用程序 101:要从浏览器获取操作以在服务器上执行某些操作,您必须执行请求周期。

railscast剧集中说明了一种生成 pdf 的方法。如果不是这样,您至少需要设置响应标头,以便浏览器理解它是 pdf 而不是 html。

于 2012-07-18T14:24:47.490 回答