Sha256: 6a9fca0d6ae34229863228227083b307f1a897136cfdd7d6d835cfe057841409

Contents?: true

Size: 797 Bytes

Versions: 1

Compression:

Stored size: 797 Bytes

Contents

module Docsplit
  # Include a method to transparently convert non-PDF arguments to temporary
  # PDFs. Allows us to pretend to natively support docs, rtf, ppt, and so on.
  module TransparentPDFs
    # Temporarily convert any non-PDF documents to PDFs before running them
    # through further extraction.
    def ensure_pdfs(docs)
      [docs].flatten.map do |doc|
        if is_pdf?(doc)
          doc
        else
          tempdir = File.join(Dir.tmpdir, 'docsplit')
          extract_pdf([doc], output: tempdir)
          File.join(tempdir, File.basename(doc, File.extname(doc)) + '.pdf')
        end
      end
    end

    def is_pdf?(doc)
      File.extname(doc).casecmp('.pdf').zero? || File.open(doc, 'rb', &:readline) =~ /\A\%PDF-\d+(\.\d+)?/
    end
  end

  extend TransparentPDFs
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
burisu-docsplit-0.7.9 lib/docsplit/transparent_pdfs.rb