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

  • SEARCH
  • Home
  • 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 8392169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:27:02+00:00 2026-06-09T19:27:02+00:00

I need to implement a _to_pdf method on my ProjectBill model which generates a

  • 0

I need to implement a _to_pdf method on my ProjectBill model which generates a PDF of my bill at this place:

/public/xls/bills/project_#{project.number}_bill_#{bill.number}.pdf

My application is using HTMLDoc for generating PDF. I am using Rails 2.3.11. With the HTMLDoc gem, I need to pass a render_to_string of my partial view _bill.pdf.haml, wich is not accessible in the model (only in the controllers), to HTMLDoc.

I already have an export_to_pdf action in my controller, triggered by the user when he wants an export (this one works). The model method will be called by a scheduled task, an emailer sending the bills when the scheduled_date is equal to Date.today.

I’ve already tried tons of solutions:

  • http://www.omninerd.com/articles/render_to_string_in_Rails_Models_or_Rake_Tasks/print_friendly
  • Have a to_pdf action on my ProjectBill controller and call it in from Model (but not working, saying that render_to_string is not defined even if its called in the Controller)
  • Use my working export_to_pdf method with a get request sent from my Model (but I figured out that I cannot really send a request from a Model…)
  • Use a Helper with the render_to_string inside it (not working: undefined method)
  • and even more !

But still not working.

Can somebody help me with this issue? Im stuck and can’t find any solution…

  • 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-09T19:27:03+00:00Added an answer on June 9, 2026 at 7:27 pm

    Ahh I finally did it !

    Thanks for your answer Sean but I was not looking for a better Gem, I was looking for a solution to my issue.

    For some reason I couldn’t use the render_to_string method in my Model…
    So I created my export_to_pdf method on my model:

      def export_to_pdf
        bill = self
        project = self.project
    
        path = "facture_#{self.project.id}_#{self.bill_number}_#{Date.today.strftime('%Y_%m_%d')}.pdf"
    
        Plus::BillingItemsController.new._to_pdf(bill.id)
    
        if File.exists?(path)
          return path
        else
          return nil
        end
      end
    

    As you can see I call my Controller in my Model. Here is my _to_pdf method:

     def _to_pdf(bill_id)
        @bill = Plus::ProjectBill.find_by_id(bill_id)
        @project = @bill.project
        path = "#{RAILS_ROOT}/public/xls/facture_#{@project.id}_#{@bill.bill_number}_#{Date.today.strftime('%Y_%m_%d')}.pdf"
    
        av = ActionView::Base.new(Rails::Configuration.new.view_path)
        av.class_eval do
          include ApplicationHelper
          include ActionController::UrlWriter
          default_url_options[:host] = 'mysite.com'
        end
    
        av.extend ApplicationController.master_helper_module
        html = av.render(:partial => "/plus/billing_items/bill_for_pdf.haml", :locals => {:bill => @bill})
        data = to_pdf(html, false, {:header => '', :headfootsize => 0, :outfile => path})
      end
    

    My to_pdf function is just doing the PDF::HTMLDoc.new and the set_option of links, logoimage , etc. and return pdf.generate

    I hope this can help someone someday somewhere !

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to implement the following: There is a table A which is supposed
So I have this method that get executed repeatedly public static boolean isReady(String dirPath,
i have one trouble, me need make scrollview with content of pdf(pages), this content
I need to implement portable code, but I do not know how to deal
I need to implement an efficient excel-like app. I'm looking for a data structure
i need to implement the email signature with image.As of now we only support
I need to implement AI for game based on fuzzy logic. I need to
I need to implement some new functions on an editor. I picked Emacs -
I need to implement a simple monitoring app in Excel. It is for monitoring
I need to implement a thread pool in Java (java.util.concurrent) whose number of threads

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.