yob / pdf-reader

The PDF::Reader library implements a PDF parser conforming as much as possible to the PDF specification from Adobe.
MIT License
1.82k stars 271 forks source link

Compatibility with Down::ChunkedIO (https://github.com/janko/down) #501

Open raivil opened 1 year ago

raivil commented 1 year ago

Hi, I'm working with the Shrine gem and uploading PDF files to S3 (direct upload). During the upload, the code is adding some metadata such as the page count.

I noticed that PDF::Reader accepts IO objects as input, but it's not working with the Down::ChunkedIOclass.

This is the code from the shrine uploader:

add_metadata do |file, metadata:, record:, **|
    case metadata["mime_type"]
    when "application/pdf"
      io = file.to_io if file.respond_to?(:to_io)
      reader = PDF::Reader.new(io)

      { page_count: reader.page_count }

Exception:

ArgumentError: input must be an IO-like object or a filename (Down::ChunkedIO)

Code from PDF::Reader that will try to read the IO object. https://github.com/yob/pdf-reader/blob/625e8dca295d8b6a48f634cba812f14fd3b805a4/lib/pdf/reader/object_hash.rb#L601

Is it possible to PDF::Reader to support Down::ChunkedIO?

Any thoughts?

Thank you.