在 Rails Application 中阅读 PDF。
我的应用程序处于RAILS 4
状态,我想在我的应用程序中阅读 PDF。我不想下载 PDF。我上传PDF并在我的应用程序中查看。
如何可能或我该怎么做?
请帮帮我...
提前致谢
使用 PDF 阅读器宝石。 https://github.com/yob/pdf-reader。它允许您通过 URL 在网络上阅读。
例如:
def count_words_in_pdf_file(filepath)
io = filepath.start_with?('http') ? open(filepath) : filepath
reader = PDF::Reader.new(io)
total_count = 0
if reader
reader.pages.each do |page|
total_count += count_words_in_text(page.text)
end
end
total_count
end
如果我理解正确,您希望向用户显示PDF。
作为解决方案,您可以使用FancyBox2在框架中显示PDF:
<a class="fancybox" data-fancybox-type="iframe" href="http://samplepdf.com/sample.pdf">Test pdf</a>
$(".fancybox").fancybox({
openEffect : 'none',
closeEffect : 'none',
iframe : {
preload: false
}
});
http://jsfiddle.net/RHaAX/
send_data
方法
def view_pdf
File.open("path/to/pdf", 'rb') do |f|
send_data f.read, :type => "application/pdf", :disposition => "inline"
end
end
但是用户的浏览器无论如何都可以覆盖和下载PDF。
如果你想强制查看pdf,你可以使用pdf吗.js由Mozilla创建
https://github.com/mozilla/pdf.js