diff --git a/Gemfile b/Gemfile index 09f6441..f7b78aa 100644 --- a/Gemfile +++ b/Gemfile @@ -2,7 +2,8 @@ source 'https://rubygems.org' -ruby '2.6.10' +# ruby '2.6.10' +ruby '3.3.0' gem 'haml', '~> 6.3' gem 'iso-639', '~> 0.3.8' @@ -19,6 +20,7 @@ gem 'sidekiq-cron', '~> 1.9' gem 'sinatra', '~> 3.2' gem 'thin', '~> 1.8' gem 'unicorn', '~> 6.1' +gem 'combine_pdf' group :development do gem 'byebug' diff --git a/Gemfile.lock b/Gemfile.lock index ef9b625..f92aa8d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -3,6 +3,9 @@ GEM specs: base64 (0.2.0) byebug (11.1.3) + combine_pdf (1.0.29) + matrix + ruby-rc4 (>= 0.1.5) concurrent-ruby (1.3.4) connection_pool (2.4.1) csv (3.3.0) @@ -35,6 +38,7 @@ GEM net-imap net-pop net-smtp + matrix (0.4.2) mime-types (3.6.0) logger mime-types-data (~> 3.2015) @@ -88,6 +92,7 @@ GEM rspec-support (~> 3.13.0) rspec-support (3.13.2) ruby-filemagic (0.7.3) + ruby-rc4 (0.1.5) ruby2_keywords (0.0.5) sidekiq (6.5.12) connection_pool (>= 2.2.5, < 3) @@ -125,10 +130,11 @@ GEM yard (0.9.37) PLATFORMS - ruby + arm64-darwin-23 DEPENDENCIES byebug + combine_pdf haml (~> 6.3) iso-639 (~> 0.3.8) json (~> 2.7) @@ -152,7 +158,7 @@ DEPENDENCIES yard RUBY VERSION - ruby 2.6.10p210 + ruby 3.3.0p0 BUNDLED WITH 2.4.22 diff --git a/lib/app.rb b/lib/app.rb index 99bcd00..a402781 100644 --- a/lib/app.rb +++ b/lib/app.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require 'haml' +require 'combine_pdf' require 'net/http' require 'pathname' require 'pp' @@ -168,6 +169,34 @@ class App < Sinatra::Base respond_with_error e end + # + # Combine Multiple PDF's into one single PDF + # + # POST params: + # pdf1 - the first pdf to combine + # pdf2 - the second pdf to combine + # ... + # pdf - the nth pdf to combine + post '/combine_pdf' do + pdf_param_prefix = 'pdf' + pdf_file_counter = 1 + + pdfs = [] + + while (pdf_param = params["#{pdf_param_prefix}#{pdf_file_counter}"]) + pdfs << pdf_param[:tempfile].read + pdf_file_counter += 1 + end + + combine_pdfs = CombinePdfs.new + combined_pdf = combine_pdfs.call(pdfs) + + content_type 'application/pdf' + combined_pdf + rescue StandardError => e + respond_with_error e + end + # # Convert document # diff --git a/lib/colore.rb b/lib/colore.rb index ae1b295..e40eb4f 100644 --- a/lib/colore.rb +++ b/lib/colore.rb @@ -10,3 +10,4 @@ require_relative 'heathen' require_relative 'sidekiq_workers' require_relative 'tika_config' +require_relative 'combine_pdfs' diff --git a/lib/combine_pdfs.rb b/lib/combine_pdfs.rb new file mode 100644 index 0000000..24242d3 --- /dev/null +++ b/lib/combine_pdfs.rb @@ -0,0 +1,8 @@ +class CombinePdfs + def call(pdfs) + combined_pdf = CombinePDF.new + combined_pdf = pdfs.reduce(combined_pdf) { |combined, pdf| combined << pdf; combined } + + combined_pdf.to_pdf + end +end \ No newline at end of file diff --git a/spec/fixtures/pdfs/1.pdf b/spec/fixtures/pdfs/1.pdf new file mode 100644 index 0000000..ca69671 Binary files /dev/null and b/spec/fixtures/pdfs/1.pdf differ diff --git a/spec/fixtures/pdfs/2.pdf b/spec/fixtures/pdfs/2.pdf new file mode 100644 index 0000000..3078434 Binary files /dev/null and b/spec/fixtures/pdfs/2.pdf differ diff --git a/spec/lib/app_spec.rb b/spec/lib/app_spec.rb index 090311b..4a5a64c 100644 --- a/spec/lib/app_spec.rb +++ b/spec/lib/app_spec.rb @@ -389,4 +389,19 @@ def show_backtrace(response) expect(body['error']).to eq 'File does not exist' end end + + describe 'POST /combine_pdf' do + it 'combines the provided pdfs into one single pdf' do + sample_pdf_dir = "#{__dir__}/../fixtures/pdfs" + pdf1 = Rack::Test::UploadedFile.new("#{sample_pdf_dir}/1.pdf", 'application/pdf') + pdf2 = Rack::Test::UploadedFile.new("#{sample_pdf_dir}/2.pdf", 'application/pdf') + post '/combine_pdf', { + pdf1: pdf1, + pdf2: pdf2, + } + expect(last_response.status).to eq 200 + expect(last_response.content_type).to eq 'application/pdf' + expect(last_response.body.include?('PDF')).to be(true) + end + end end diff --git a/spec/lib/combine_pdfs_spec.rb b/spec/lib/combine_pdfs_spec.rb new file mode 100644 index 0000000..0d35979 --- /dev/null +++ b/spec/lib/combine_pdfs_spec.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +require 'spec_helper' +require 'app' + +RSpec.describe CombinePdfs do + subject(:combine_pdfs) { CombinePdfs.new.(pdfs) } + let(:pdfs) { [fixture('pdfs/1.pdf').read, fixture('pdfs/2.pdf').read] } + + describe 'combine' do + it 'creates a new pdf' do + expect { + pdf = combine_pdfs + expect(pdf.size).to be > 0 + }.not_to raise_error + end + end +end