Dipak Panchal
Dipak Panchal

Reputation: 6026

Read PDF in rails application

Read PDF in Rails Application.

My Application is in RAILS 4 and I want to read PDF in my application. I don't want to download PDF. I Upload PDF and view in my application.

How it is possible or How can i do this?

Help Me Please...

Thanks In Advance

Upvotes: 1

Views: 6478

Answers (3)

diomede.t
diomede.t

Reputation: 1

You can use send_data method

def view_pdf
  File.open("path/to/pdf", 'rb') do |f|
    send_data f.read, :type => "application/pdf", :disposition => "inline"
  end
end

but the user's browser can override and download the pdf anyway.

If you want force the pdf viewing, can you use pdf.js created by mozilla

https://github.com/mozilla/pdf.js

Upvotes: 0

ck3g
ck3g

Reputation: 5929

If I understood you correctly you want to show PDF to users.

As a solution you could use FancyBox2 to display PDF in the frame:

<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/

Upvotes: 0

Rajesh Kolappakam
Rajesh Kolappakam

Reputation: 2125

Use the PDF reader gem. https://github.com/yob/pdf-reader. It allows you to read over the web via a URL.

For example:

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

Upvotes: 3

Related Questions