1

I am new to Ruby and RoR. I made a pdf-generator in Ruby using Prawn. Now I want to execute that from my rails view.

<%= 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 %>

To execute my prawn-app in ruby I use 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

My controller and modelfile:

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

I hope it is clear what I intend to do. My apologies to ask a similar question as others, but I really don't see the solution in there with my code.

2 Answers 2

3

You need to create an action in your controller which should call your script and return generated pdf. Then you could use send_file or save file in public folder and return url for it to user.

Sign up to request clarification or add additional context in comments.

1 Comment

Ahh I think I get it. I need to send a request with my parameters from my controller and have a URL returned from the script. Am I right?
1

As Yuri pointed out, you have to make a controller action to do it. Web application 101: To get an action from the browser to do something on the server, you have to do the request cycle.

One way to generate pdf is illustrated in a railscast episode. If not with that, you do at least need to set the response headers so the browser understands it is a pdf and not html.

1 Comment

Thanks, Web app 101 shows the cycle very well :-) I saw the cast, thought of using it, but I want to get this going first!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.