Class: Paperclip::Document::Processors::Freezer

Inherits:
Paperclip::Document::Processor show all
Defined in:
lib/paperclip/document/processors/freezer.rb

Overview

This processor converts document to PDF

Instance Attribute Summary

Attributes inherited from Paperclip::Document::Processor

#instance, #tmp_dir

Instance Method Summary collapse

Methods inherited from Paperclip::Document::Processor

#basename, #file_path

Constructor Details

#initialize(file, options = {}, attachment = nil) ⇒ Freezer

Returns a new instance of Freezer.



6
7
8
9
10
11
12
# File 'lib/paperclip/document/processors/freezer.rb', line 6

def initialize(file, options = {}, attachment = nil)
  super
  @format = options[:format]
  unless @format == :pdf
    raise Paperclip::Error, 'Valid format (pdf) must be specified'
  end
end

Instance Method Details

#makeObject

Convert the document to pdf



15
16
17
18
19
20
21
22
23
24
# File 'lib/paperclip/document/processors/freezer.rb', line 15

def make
  destination_path = tmp_dir.to_s
  destination_file = File.join(destination_path, basename + ".#{@format}")
  if pdf_format?
    destination_file = file_path.to_s
  else
    Docsplit.extract_pdf(file_path.to_s, output: destination_path)
  end
  File.open(destination_file)
end

#pdf_format?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/paperclip/document/processors/freezer.rb', line 26

def pdf_format?
  File.open(file_path, 'rb', &:readline).to_s =~ /\A\%PDF-\d+(\.\d+)?$/
end