使用Ruby on Rails提供PDF文件 - 下载文件中的空白/白页

时间:2015-05-20 08:07:52

标签: ruby-on-rails ruby pdf

为了简化这个问题,让我们假设我有一个正确生成的PDF文件存储在内存中,并在Base64中编码(在Ruby之外生成),例如:

JVBERi0xLjQKJcOkw7zDtsOfCjIgMCBvYmoKPDwvTGVuZ3RoIDMgMCBSL0ZpbHRlci9GbGF0ZURlY29kZT4+CnN0cmVhbQp4nCWKsQoCMRAF+/2KVwsmu2vinRBSiKdgF1ywEDv1Oo/LFf6+EXnNDPPYCT40g8GN4i46RR/E9ahPuq7w/re2OtLeKG5b6rrQzvaAPwpEYa9bYsmaWLMk3uS1/vxuZxqMChXM8MPpEjEu8FYDDhMKfQFvKRtXCmVuZHN0cmVhbQplbmRvYmoKCjMgMCBvYmoKMTE4CmVuZG9iagoKNCAwIG9iago8PC9UeXBlL1hPYmplY3QKL1N1YnR5cGUvRm9ybQovQkJveFsgLTExMiA0MjAgNzA4IDQyMC4xIF0KL0dyb3VwPDwvUy9UcmFuc3BhcmVuY3kvQ1MvRGV2aWNlUkdCL0sgdHJ1ZT4+Ci9MZW5ndGggOAovRmlsdGVyL0ZsYXRlRGVjb2RlCj4+CnN0cmVhbQp4nAMAAAAAAQplbmRzdHJlYW0KZW5kb2JqCgo1IDAgb2JqCjw8L0NBIDAuNQogICAvY2EgMC41Cj4+CmVuZG9iagoKNyAwIG9iago8PC9MZW5ndGggOCAwIFIvRmlsdGVyL0ZsYXRlRGVjb2RlL0xlbmd0aDEgNjU2MD4+CnN0cmVhbQp4nOVYfWwb53l/3jt+SZRF0pZo2bTFo8+UJfNLFi1LchSLkkiK+rBFfdAh5cgmJZ5EOpLIkpRcO8mstE3q0fHipZ0TN0aTFNsQrAV8tNtCKdJYBVoMA5akQ1F0WePUwAoMWK3Z8+ZgSGNrz3s8yZKbD2AdsD92Iu+e5/f8no/3ed/3eKdcZlaAMpgHFjzj07F0BVEwAPD3AGTj+FyOW+7wcCjfAGCME+nJ6Vr3r/8NgP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filename: "download.pdf", type: "application/pdf", disposition: "inline"

虽然相同的代码适用于其他文件类型(CSV,Excel电子表格),但它的二进制PDF文件似乎画了一个空白(双关语)。奇怪的是,当我有一个包含多个页面的PDF文件时,文件中包含了适量的页面,但它们都是空白的。

我还试过从磁盘上提供PDF(我使用LibreOffice生成PDF):

send_file "/path/to/file.pdf", :filename => "download.pdf", :type => "application/pdf"

但结果是一样的。我的想法已经不多了,所以我很感激任何提示。

注意:我没有使用Ruby / RoR生成PDF文件,因此使用Prawn等建议不合适。文件本身是独立生成的,并存储为base64。我知道一个事实,即生成过程没有问题,因为上面包含的基本编码PDF是有效的(我已经使用在线解码器和其他编程语言对其进行了检查)。

1 个答案:

答案 0 :(得分:0)

经过一番广泛的调试后,我发现问题实际上并不是由我的Rails应用程序或其配置引起的。为了简化开发过程,我们使用Grunt运行和观察任务,这些任务随其自己的HTTP服务器一起提供(Grunt正在侦听端口9000,而RoR在3000上侦听。)

使用默认的Rails WEBRick服务器取消配置Grunt服务器。事实证明,Grunt在传递正确的Content-Length标题时遇到了一些问题,这导致了问题。由于我们只使用Grunt进行开发,因此这不会影响我们的生产,所以我要关闭这个问题。

我希望这会在某些时候帮助某人。