Rails send_file / send_data发送没有内容的pdf文件

时间:2017-07-13 12:38:21

标签: ruby-on-rails-4 wkhtmltopdf ruby-on-rails-4.2 wicked-pdf ruby-2.3

我正在使用WickedPdf.new.pdf_from_string生成pdf文件。保存在磁盘中并打开时,文件本身生成正常。

但是当通过send_data发送同一文件时,浏览器会根据生成的文件下载带有页面的文件,但不包含任何文本/内容。所有页面都是空白的。这是代码段

send_data File.open(pdf_file.path, 'rb').read, type: 'application/pdf', filename: 'abc.pdf'

我也试过send_file但没有成功。

环境变量

Rails Version: 4.2.8
Ruby Version: 2.3.3
WickedPdf Ver: 1.1.0

1 个答案:

答案 0 :(得分:0)

我无法在in the wicked_pdf_issues project

中重现您的问题this commit

此代码下载并显示正常:

respond_to do |format|
  format.pdf do
    pdf = WickedPdf.new.pdf_from_string('<html><head><title>foo</title></head><body><h1>This is a PDF</h1></body></html>')
    Tempfile.create do |t|
      t.binmode
      t.write(pdf)
      t.rewind
      t.close
      send_data File.open(t.path, 'rb').read, type: 'application/pdf', filename: 'abc.pdf'
    end
  end
end

您可以按going here and clicking the download PDF link查看结果。