【问题标题】:Issues creating PDF using wicked and Rails API使用 wicked 和 Rails API 创建 PDF 的问题
【发布时间】:2016-11-23 19:54:26
【问题描述】:

我正在尝试使用 wicked 和 Rails API 创建可下载的 PDF。目前只能下载PDF,但内容为空,文件名为response.pdf.pdf。

当对特定分数发出 GET 请求时,这是我用来生成 PDF 的方法。

def download_pdf(score)
    html = render_to_string(:action => :show, :layout => "pdf.html.erb", :template => "scores/show.pdf.erb", locals:{:score => score}) 
    pdf = WickedPdf.new.pdf_from_string(html) 

    send_data(pdf,  
      :filename => 'test.pdf',
      :disposition => 'attachment')
end

【问题讨论】:

    标签: ruby-on-rails pdf rails-api wicked-pdf


    【解决方案1】:

    我今天遇到了这个问题。很晚了,但对寻找解决方案的人很有用。

    您必须从 ActionController::Base 而不是 ActionController::API 继承。或者您可以使用以下代码覆盖 render_to_string。

    def render_to_string(*args)
      controller = ActionController::Base.new
      controller.locale = I18n.locale
      controller.render_to_string(*args)
    end
    

    参考:https://github.com/mileszs/wicked_pdf/issues/652

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-28
      • 1970-01-01
      • 2018-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-03
      • 1970-01-01
      相关资源
      最近更新 更多