Read my latest article: 8 things I look for in a Ruby on Rails app (posted Thu, 06 Jul 2017 17:59:00 GMT)

Rendering a ERb template stored in the database to a PDF

Posted by Sat, 04 Jun 2005 22:59:00 GMT

In my PDF generation project, I would like the PDF templates to be stored in the database rather than on the file system. (not everyone who admins the system should have access to the file system).

So, what I did, after playing with all the different render_* methods in Action Controller, was create a new method called render_string_to_string. This marks my first attempt to submit a patch. After doing so, I noticed that Rails trunk included a completely refactored version of action_controller.rb. Perhaps this functionality exists in trunk now, I haven’t had a chance to play with it yet.

However, in the meantime, I can do this:

    letter_template = LetterTemplate.find_by_id(1)

    foo = render_string_to_string(letter_template.body)

    pdf=FPDF.new
    pdf.AddPage
    pdf.SetFont('Times')
    pdf.SetFontSize(12)

    pdf.Write(5, foo)

    pdf.Output


This is the first time that I have started to tinker (all 4 lines) with the actual Rails code base. I’ve wandered around it, but have stuck to using Rails rather than trying to modify it.

If there is a better way to handle this, please let me know. :-)

Get help with your Rails project

comments powered by Disqus