Topic: Generating a rtf file and automatic download

Hello everybody,

I'm developing a small app for myself and I want to generate a rtf or latex file form Markdown with pandoc (and pandoc-ruby). I have wrote a function called render_to_rtf that invoque Pandoc, generate and save the file:

def render_to_rtf(option)
      rtf = PandocRuby.new(option, :from => :markdown, :to => :rtf)
      file = File.new("write.rtf", "w+")
      file.puts(rtf.convert)
      file.close
  end

and I put in my respond_to : format.rtf {render_to_rtf(@report.content)}

This create a formatted rtf file, but instead of starting an automatic download like prawnto, it store it in my root directory.

One other solution I was exploring after reading this article : http://kangaroobox.blogspot.com/2009/07 … rawn.html, was this respond_to block :

format.rtf do
        send_data render_to_rtf(@report.content),
        :type => 'text/rtf',
        :disposition  => 'inline'
end

But the result is only this error, which is strange for me because @report.content exist and is not null :

You have a nil object when you didn't expect it!
The error occurred while evaluating nil.bytesize

Thanks in advance and sorry for my english

Last edited by Monkeyweb (2009-12-02 20:29:35)

Re: Generating a rtf file and automatic download

Are you absolutely sure that @report.content is returning something? Try raising @report.content to find out.

Re: Generating a rtf file and automatic download

Yes I'm sure.

Actually I found a way that almost work :

#applicationController
  def render_to_rtf(text)
   return PandocRuby.convert(text, :from => :markdown, :to => :rtf)
  end

#reportController

  def show
    @report = Report.find(params[:id])
    @content = @report.content
    # prawnto :filename => @report.title + ".pdf", :prawn => {:page_size => 'A4'}
    # rtfto :filename => @report.title + ".rtf"
    respond_to do |format|
      format.html do
      end
     
      format.rtf do
        send_data(render_to_rtf(@content), :filename => "#{@report.title}.rtf", :type => :rtf)
      end
    end
  end

But the rtf file that is generating contains only the first paragraph of the text, despite the fact that @content contains all the text.

Re: Generating a rtf file and automatic download

Does the file get created with the proper filename? Are there any limitations on the reports table at all?

Re: Generating a rtf file and automatic download

Yes, the filename is ok, and the database has no particular restriction (just create with a generator). And when I print @content, I see all the paragraph, not only the first.

Maybe send_data generate the file before he has receive all the data?

Re: Generating a rtf file and automatic download

Possibly, but I would've thought send_data would be the way to do it hmm

I'll have a look into this. I'm curious myself.

Re: Generating a rtf file and automatic download

Monkeyweb wrote:

But the rtf file that is generating contains only the first paragraph of the text, despite the fact that @content contains all the text.

The "standalone" option is needed to generate complete files:

 PandocRuby.convert(text, {:from => :markdown, :to => :rtf}, :standalone) 

Re: Generating a rtf file and automatic download

Hey, that actually works ! Just in time for christmas smile thanks