0

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

但是,当通过 发送相同的文件时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
4

1 回答 1

1

我无法在此提交的 wicked_pdf_issues 项目中重现您的问题

此代码下载并显示得很好:

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

您可以前往此处并单击下载 PDF 链接查看结果。

于 2017-07-28T23:22:57.487 回答