Class: Paperclip::DocsplitPdf

Inherits:
DocsplitProcessor show all
Defined in:
lib/docsplit-paperclip-processor.rb

Instance Attribute Summary

Attributes inherited from DocsplitProcessor

#attachment, #options, #src

Instance Method Summary collapse

Methods inherited from DocsplitProcessor

#initialize

Constructor Details

This class inherits a constructor from Paperclip::DocsplitProcessor

Instance Method Details

#makeObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/docsplit-paperclip-processor.rb', line 32

def make
  begin
    src_path = File.expand_path(@src.path)
    dst_dir = Dir.tmpdir
    dst_path = File.join(dst_dir, "#{@basename}.pdf")

    if pdf_format?(src_path)
      dst_path = File.join(dst_dir, "_#{@basename}.pdf")
      FileUtils.copy_file(src_path, dst_path)
    else
      Docsplit.extract_pdf(src_path, :output => dst_dir)
    end
  rescue Exception => e
    Rails.logger.error e.message
    raise PaperclipError, "There was an error converting #{@basename} to pdf"
  end
  File.open(dst_path)
end

#pdf_format?(file) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
# File 'lib/docsplit-paperclip-processor.rb', line 51

def pdf_format?(file)
  file_magic = FileMagic.new
  type = file_magic.file(file)
  file_magic.close
  type =~ /pdf/i
end