Module: Pludoni::Pdfutils

Defined in:
lib/pludoni/pdfutils.rb,
lib/pludoni/pdfutils/joiner.rb,
lib/pludoni/pdfutils/compressor.rb,
lib/pludoni/pdfutils/file_wrapper.rb,
lib/pludoni/pdfutils/local_file_wrapper.rb,
lib/pludoni/pdfutils/convert_image_to_pdf.rb,
lib/pludoni/pdfutils/active_storage_wrapper.rb,
lib/pludoni/pdfutils/convert_document_to_pdf.rb

Defined Under Namespace

Classes: ActiveStorageWrapper, CompressionFailed, Compressor, ConversionFailedError, ConvertDocumentToPdf, ConvertImageToPdf, Error, FileWrapper, Joiner, JoiningFailedError, LocalFileWrapper

Class Method Summary collapse

Class Method Details

.compress_if_large(file, max_size:) ⇒ File, Tempfile

compresses the given file if larger than given max_size

Parameters:

  • file (File, Tempfile, ActiveStorage::Blob)
  • max_size (Integer)

    in bytes

Returns:

  • (File, Tempfile)


14
15
16
17
18
19
20
21
22
# File 'lib/pludoni/pdfutils.rb', line 14

def compress_if_large(file, max_size:)
  file = FileWrapper.make(file)
  if max_size && file.filesize > max_size
    yield(file) if block_given?
    Compressor.new(file).run
  else
    file
  end
end

.convert_all_to_pdf_and_join_max_size(blobs, max_files:, max_size: nil, &block) ⇒ Object

converts all images to pdf and joins them if there are more than max_files

Parameters:

  • blobs (Array<ActiveStorage::Blob>)
  • max_files (Integer)

    keep max_files - 1 files, and join the rest

  • max_size (Integer) (defaults to: nil)

    in bytes Convert individual file if larger than max_size



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/pludoni/pdfutils.rb', line 28

def convert_all_to_pdf_and_join_max_size(blobs, max_files:, max_size: nil, &block)
  files = blobs.map { |upload| convert_to_pdf(upload, &block) }
  if files.length > max_files
    if block_given?
      yield("More than #{max_files}; joining, keeping first #{max_files - 1} files; joining the rest")
    end
    keep = files.take(max_files - 1)
    join = Joiner.new(files.drop(max_files - 1)).run(&block)
    files = keep + [join]
  end
  files.map do |file|
    compress_if_large(file, max_size: max_size) do
      yield("Compressing file") if block_given?
    end
  end
end

.convert_to_pdf(blob, &block) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/pludoni/pdfutils.rb', line 45

def convert_to_pdf(blob, &block)
  blob = FileWrapper.make(blob)
  case blob.content_type.to_s
  when /image/
    block.call("converting #{blob.filename.to_s} to pdf") if block_given?
    ConvertImageToPdf.new(blob).run(&block)
  when "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
    "application/msword",
    "application/vnd.ms-powerpoint",
    "application/vnd.oasis.opendocument.text",
    "application/vnd.openxmlformats-officedocument.presentationml.presentation"
    block.call("converting #{blob.filename.to_s} to pdf") if block_given?
    ConvertDocumentToPdf.new(blob).run(&block)
  else
    blob
  end
end