Class: BenefitsIntakeService::Utilities::ConvertToPdf

Inherits:
Object
  • Object
show all
Defined in:
lib/benefits_intake_service/utilities/convert_to_pdf.rb

Constant Summary collapse

CAN_CONVERT =
%w[.jpg .jpeg .png .gif .bmp .txt].freeze
IMG_TYPES =
%w[.jpg .jpeg .png .gif .bmp].freeze
NON_IMG_TYPES =
CAN_CONVERT - IMG_TYPES
NOTE_PAGE =
'This PDF has been generated/converted by va.gov from a non-PDF document supplied by the end-user.
This cover page has been auto-generated.
The user\'s content begins on page #2.'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ ConvertToPdf

Returns a new instance of ConvertToPdf.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/benefits_intake_service/utilities/convert_to_pdf.rb', line 19

def initialize(file)
  @original_file = file
  @original_filename = File.basename(@original_file)
  @entropy = "#{Common::FileHelpers.random_file_path}.#{Time.now.to_i}"
  @converted_filename = "#{@entropy}.converted_from_#{@original_filename}.pdf"
  extension = File.extname(@original_filename).downcase
  case extension
  when *IMG_TYPES
    convert_img!
  when *NON_IMG_TYPES
    convert_txt!
  else
    raise "Unsupported file type (#{extension}), cannot convert to PDF."
  end
end

Instance Attribute Details

#converted_fileObject

Returns the value of attribute converted_file.



16
17
18
# File 'lib/benefits_intake_service/utilities/convert_to_pdf.rb', line 16

def converted_file
  @converted_file
end

#converted_filenameObject

Returns the value of attribute converted_filename.



16
17
18
# File 'lib/benefits_intake_service/utilities/convert_to_pdf.rb', line 16

def converted_filename
  @converted_filename
end

#entropyObject (readonly)

Returns the value of attribute entropy.



17
18
19
# File 'lib/benefits_intake_service/utilities/convert_to_pdf.rb', line 17

def entropy
  @entropy
end

#original_fileObject

Returns the value of attribute original_file.



16
17
18
# File 'lib/benefits_intake_service/utilities/convert_to_pdf.rb', line 16

def original_file
  @original_file
end

#original_filenameObject

Returns the value of attribute original_filename.



16
17
18
# File 'lib/benefits_intake_service/utilities/convert_to_pdf.rb', line 16

def original_filename
  @original_filename
end

Instance Method Details

#convert_img!Object (private)



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/benefits_intake_service/utilities/convert_to_pdf.rb', line 49

def convert_img!
  tmp_cover_page_pdf = generate_pdf_title_page_pdf
  convert = MiniMagick::Tool::Convert.new
  convert.resize('2550x3300') # 8.5x11 in pixels
  convert << '-density' << '300' # 300 dpi
  convert << tmp_cover_page_pdf # instruction doc (first page of resulting pdf)
  convert << @original_file # original filename (input)
  convert << @converted_filename # output filename (output)
  convert.call # do it
  # Delete tmp cover/info page for pdf after new file generated
  Common::FileHelpers.delete_file_if_exists(tmp_cover_page_pdf)
end

#convert_txt!Object (private)



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/benefits_intake_service/utilities/convert_to_pdf.rb', line 62

def convert_txt!
  # The default PDF fonts only supports 'Windows-1252' encoding.
  # We COULD download more fonts if we needed more char/unicode support.
  # But for now sending through with non-'Windows-1252' encoded chars stripped should work.
  # Can expand later if needed.
  content = File.read(@original_file).encode('Windows-1252', invalid: :replace, undef: :replace, replace: '')
  Prawn::Document.generate(@converted_filename) do |pdf|
    pdf.text generate_pdf_title_page
    pdf.start_new_page
    pdf.text content
  end
end

#generate_pdf_title_pageObject (private)



37
38
39
# File 'lib/benefits_intake_service/utilities/convert_to_pdf.rb', line 37

def generate_pdf_title_page
  NOTE_PAGE + "\n\nOriginal Filename: \"#{@original_filename}\"\nConverted DateTime: #{Time.zone.now}\n"
end

#generate_pdf_title_page_pdfObject (private)



41
42
43
44
45
46
47
# File 'lib/benefits_intake_service/utilities/convert_to_pdf.rb', line 41

def generate_pdf_title_page_pdf
  tmp = "#{@entropy}.converted_from_#{@original_filename}_cover_page.pdf"
  Prawn::Document.generate(tmp) do |pdf|
    pdf.text generate_pdf_title_page
  end
  tmp
end