Search code examples
ruby-on-railspdfpdf-generationflying-saucer

Is there a way for acts_as_flying_saucer to save a file without calling render_pdf?


As the title says I use the Rails gem that works good but I want to separately save pdf's to a folder. Is there a way to do this without calling render_pdf?

This is what does not work:

  render_pdf :template => 'caseprinttemplates/create_pdf_to_print.erb', :pdf_file => "/home/mattias/www/Inkasso/public/uploadedfiles/" + results[:title]

  redirect_to new_interventionreminder_path(:case_id => @interventionreminder.case_id, :saved => "1")
  return

This does work on another page:

render_pdf :template => 'caseprinttemplates/create_pdf_to_print.erb', :layout => nil, :send_file => { :filename => "samladutskrift.pdf"}

Solution

  • The acts_as_flying_saucer plugin is just a wrapper around the Flying Saucer java library. In the source, you can see that only a single wrapper method is defined: render_pdf().

    https://github.com/dagi3d/acts_as_flying_saucer/blob/master/lib/acts_as_flying_saucer_controller.rb

    You can, however, use render_pdf() to render to a file, like the examples found here:

    # Renders the template located at '/foo/bar/pdf.html.erb' and stores the pdf 
    # in the temp path with a filename based on its md5 digest
    render_pdf :file => '/foo/bar/pdf.html.erb'
    
    # renders the template located at 'app/views/foo.html.erb' and saves the pdf
    # in '/www/docs/foo.pdf'
    render_pdf :template => 'foo', :pdf_file => '/www/docs/foo.pdf'
    
    # renders the 'app/views/foo.html.erb' template, saves the pdf in the temp path
    # and sends it to the browser with the name 'bar.pdf'
    render_pdf :template => 'foo', :send_file => { :filename => 'bar.pdf' }