【问题标题】:Is there a way for acts_as_flying_saucer to save a file without calling render_pdf?有没有办法让acts_as_flying_saucer 在不调用render_pdf 的情况下保存文件?
【发布时间】:2012-02-19 15:48:45
【问题描述】:

正如标题所说,我使用运行良好的 Rails gem,但我想将 pdf 单独保存到文件夹中。有没有办法在不调用 render_pdf 的情况下做到这一点?

这是行不通的:

  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

这确实适用于另一个页面:

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

【问题讨论】:

    标签: ruby-on-rails pdf pdf-generation flying-saucer


    【解决方案1】:

    acts_as_flying_saucer 插件只是 Flying Saucer java 库的包装器。在源码中,可以看到只定义了一个包装方法:render_pdf()。

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

    但是,您可以使用 render_pdf() 来渲染到文件,例如 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' }
    

    【讨论】:

    • 谢谢,但我已经尝试了所有这些示例,但我需要这样的场景:保存文件并重定向到另一个页面。有趣的是,当我这样做时,它抱怨我以正确方式编写的元标记,因为该页面在我使用 send_file 选项的另一个页面上工作。似乎飞碟库也在验证,这很好,但是当它抱怨不存在或编写正确的东西时。我将在这里编辑我的代码给你看。
    • 现在可以使用了。我需要一个 :layout => nil 在 render_pdf 中。在另一页看到了。感谢您的帮助。
    猜你喜欢
    • 2021-12-19
    • 2021-09-24
    • 1970-01-01
    • 1970-01-01
    • 2020-09-12
    • 1970-01-01
    • 1970-01-01
    • 2014-07-13
    • 2018-01-27
    相关资源
    最近更新 更多