Clint Laskowski
Clint Laskowski

Reputation: 171

Counting PDF pages in ROR with Docsplit

I need to get the count of pages in PDF files stored in Ruby on Rails 5.2.3 ActiveStorage using Docsplit.

I'm uploading PDF documents using Ruby on Rails ActiveStorage. I understand these documents are stored as a blob. I was hoping I could pass the reference to the PDF file to Docsplit with something like:

pages = Docsplit.extract_length(@car_record.crecord)

But the above resulted in an error:

no implicit conversion of ActiveStorage::Attached::One into String

Upvotes: 0

Views: 540

Answers (1)

Josh Brody
Josh Brody

Reputation: 5363

Docsplit.extract_length expects a string (presumably a path to a local file), and @car_record.crecord returns an ActiveRecord object.

You should be able to do something like,

file = @car_record.crecord.download_blob_to_tempfile
Docsplit.extract_length(file.path)

Edit: ActiveStorage::Downloading is being removed in 6.1. Try something like the following:

tempfile = Tempfile.new 
tempfile.binmode

begin
  @car_record.crecord.download { |chunk| tempfile.write(chunk) } 
  tempfile.flush
  tempfile.rewid
ensure 
  tempfile.close!
end

Docsplit.extract_length(tempfile.path)

Upvotes: 1

Related Questions