Sha256: b15aa8d0f92d733c7fccf1b4261b8ec8654c9483fa985aac28e830408c08ab46

Contents?: true

Size: 970 Bytes

Versions: 25

Compression:

Stored size: 970 Bytes

Contents

# frozen_string_literal: true

module ActiveStorage
  class Previewer::PopplerPDFPreviewer < Previewer
    class << self
      def accept?(blob)
        blob.content_type == "application/pdf" && pdftoppm_exists?
      end

      def pdftoppm_path
        ActiveStorage.paths[:pdftoppm] || "pdftoppm"
      end

      def pdftoppm_exists?
        return @pdftoppm_exists if defined?(@pdftoppm_exists)

        @pdftoppm_exists = system(pdftoppm_path, "-v", out: File::NULL, err: File::NULL)
      end
    end

    def preview
      download_blob_to_tempfile do |input|
        draw_first_page_from input do |output|
          yield io: output, filename: "#{blob.filename.base}.png", content_type: "image/png"
        end
      end
    end

    private
      def draw_first_page_from(file, &block)
        # use 72 dpi to match thumbnail dimensions of the PDF
        draw self.class.pdftoppm_path, "-singlefile", "-r", "72", "-png", file.path, &block
      end
  end
end

Version data entries

25 entries across 25 versions & 4 rubygems

Version Path
activestorage-6.0.3.7 lib/active_storage/previewer/poppler_pdf_previewer.rb
activestorage-6.0.3.6 lib/active_storage/previewer/poppler_pdf_previewer.rb
activestorage-6.0.3.5 lib/active_storage/previewer/poppler_pdf_previewer.rb
activestorage-6.0.3.4 lib/active_storage/previewer/poppler_pdf_previewer.rb
activestorage-6.0.3.3 lib/active_storage/previewer/poppler_pdf_previewer.rb
activestorage-6.0.3.2 lib/active_storage/previewer/poppler_pdf_previewer.rb
activestorage-6.0.3.1 lib/active_storage/previewer/poppler_pdf_previewer.rb
activestorage-6.0.3 lib/active_storage/previewer/poppler_pdf_previewer.rb
activestorage-6.0.3.rc1 lib/active_storage/previewer/poppler_pdf_previewer.rb
activestorage-6.0.2.2 lib/active_storage/previewer/poppler_pdf_previewer.rb
argon-1.3.1 vendor/bundle/ruby/2.7.0/gems/activestorage-6.0.2.1/lib/active_storage/previewer/poppler_pdf_previewer.rb
symbolic_enum-1.1.5 vendor/bundle/ruby/2.7.0/gems/activestorage-6.0.2.1/lib/active_storage/previewer/poppler_pdf_previewer.rb
activestorage-6.0.2.1 lib/active_storage/previewer/poppler_pdf_previewer.rb
activestorage-6.0.2 lib/active_storage/previewer/poppler_pdf_previewer.rb
activestorage-6.0.2.rc2 lib/active_storage/previewer/poppler_pdf_previewer.rb
activestorage-6.0.2.rc1 lib/active_storage/previewer/poppler_pdf_previewer.rb
activestorage-6.0.1 lib/active_storage/previewer/poppler_pdf_previewer.rb
chatops-rpc-0.0.2 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/activestorage-6.0.0/lib/active_storage/previewer/poppler_pdf_previewer.rb
activestorage-6.0.1.rc1 lib/active_storage/previewer/poppler_pdf_previewer.rb
chatops-rpc-0.0.1 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/activestorage-6.0.0/lib/active_storage/previewer/poppler_pdf_previewer.rb