首页 > 解决方案 > Rails 将 PDF 作为可下载文件发送

问题描述

我想将 pdf 文件从 rails 发送到浏览器。它只是正常调用最后带有 .pdf 的 api。喜欢

/detail/ac67857a-ad71-4a97-9718-c71c47e2e4bc.pdf

这是我的控制器代码

format.pdf {
        download_result_from_aws = export_pdf_service.get_file_from_aws(upload_result[:key])
        File.open(download_result_from_aws, 'r') do |f|
          send_data f.read.force_encoding('BINARY'), :filename => export_pdf_service.filename, :type => "application/pdf", :disposition => "attachment"
        end
      }

但它没有被下载。就像这样的回应

%PDF-1.4
1 0 obj
<<
/Title (þÿ)
/Creator (þÿwkhtmltopdf 0.12.4)
/Producer (þÿQt 4.8.7)
/CreationDate (D:20190702155100+07'00')
>>
endobj
3 0 obj
<<
/Type /ExtGState
/SA true
/SM 0.02
/ca 1.0
/CA 1.0
/AIS false
/SMask /None>>
endobj
4 0 obj
[/Pattern /DeviceRGB]
endobj
7 0 obj
<<
/Type /XObject
/Subtype /Image
/Width 600
/Height 714
/BitsPerComponent 8
/ColorSpace /DeviceGray
/Length 8 0 R
/Filter /FlateDecode
>>
stream
xíwÅÇ7×£7éÒ-tTP¢"ØfAE¨ØP~º
©ôÞ98:å¸^r)óý

如何解决?非常感谢你

标签: ruby-on-rails

解决方案


推荐阅读