Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8636149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:09:24+00:00 2026-06-12T10:09:24+00:00

How I can setup controller/response objects from scratch to ran render_to_string? I developed an

  • 0

How I can setup controller/response objects from scratch to ran render_to_string?

I developed an application which generate a PDF file.
I used erb based template to generate TeX file, then process it to convert to PDF.

It’s working fine, but I now start thinking it is better to generate the report in background. Thus, I created a queuing system using delayed_job and it’s working well.

Now, when combine two of them, I realized that renderer implemented as part of controller.

What I want to do is run render_to_string to generate the PDF. It looks like I need to setup controller and response object from scratch.

I found a question (How do I call controller/view methods from the console in Rails?) relate to this, but setting up a response object by calling post seems awkward.

How I can setup controller and response object to achieve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T10:09:25+00:00Added an answer on June 12, 2026 at 10:09 am

    Here’s one way to do it (generating the PDF via wicked_pdf):

    Controller:

    class FoosController < ApplicationController
    
      def queue_pdf_generation
        @foo = Foo.find(params[:id])
        Delayed::Job.enqueue GeneratePdfJob.new(@foo.id)
      end
    
    end
    

    Delayed Job:

    class GeneratePdfJob < Struct.new(:foo_id)
    
      def perform
        # get the Foo object when the job is run
        foo = Foo.find(foo_id)
    
        # create an instance of ActionView, so we can use render method outside of a controller
        av = ActionView::Base.new()
        av.view_paths = ActionController::Base.view_paths
        pdf_html = av.render :template => "foos/pdf.html.erb", :layout => nil, :locals => {:foo => foo}
    
        # use wicked_pdf gem to create PDF from the foo HTML
        foo_pdf = WickedPdf.new.pdf_from_string(pdf_html, :page_size => 'Letter')
    
        # save PDF to disk
        pdf_path = Rails.root.join('tmp', "#{foo.id}.pdf")
        File.open(pdf_path, 'wb') do |file|
          file << foo_pdf
        end
    
    end
    

    A couple notes:

    • The “perform” method doesn’t take a parameter; inheriting from a
      struct lets us pass a parameter to the GeneratePdfJob object, and it
      sets up a local variable called “foo_id” that we can use in the
      “perform” method.
    • Your view needs to reference the local variable
      “foo” rather than an instance variable “@foo”.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my controller I have model operations that can return empty results. I've setup
I have an ASP.NET MVC2 application that uses a parent controller to setup specific
I'm creating a controller which will pull information of a painter from a database
The setup : The controller contains a method public ActionResult SaveFile() which returns a
How can I setup the following models/specs for mocking in my controller specs. Here
I can setup a connection manager that uses the Directory Services OLE provider and
Does anyone know of a way I can setup a VLAN for my Android
How do you setup a ViewSwitcher like the ICS Dialer has? I can setup
I'm trying to follow the instructions here ( http://docs.pylonsproject.org/projects/pyramid_beaker/en/latest/ ) so I can setup
Can I setup Team Foundation Server in such a manner that every uer have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.