Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API endpoint /combine_pdf to combine multiple pdfs into one single pdf #330

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

source 'https://rubygems.org'

ruby '2.6.10'
ruby '3.3.0'

gem 'haml', '~> 6.3'
gem 'iso-639', '~> 0.3.8'
Expand All @@ -19,6 +19,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'
Expand Down
10 changes: 8 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -152,7 +158,7 @@ DEPENDENCIES
yard

RUBY VERSION
ruby 2.6.10p210
ruby 3.3.0p0

BUNDLED WITH
2.4.22
29 changes: 29 additions & 0 deletions lib/app.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'haml'
require 'combine_pdf'
require 'net/http'
require 'pathname'
require 'pp'
Expand Down Expand Up @@ -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<n> - 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
#
Expand Down
1 change: 1 addition & 0 deletions lib/colore.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
require_relative 'heathen'
require_relative 'sidekiq_workers'
require_relative 'tika_config'
require_relative 'combine_pdfs'
9 changes: 9 additions & 0 deletions lib/combine_pdfs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CombinePdfs
def call(pdfs)
combined_pdf = CombinePDF.new

pdfs.each {|pdf| combined_pdf << CombinePDF.parse(pdf) }

combined_pdf.to_pdf
end
end
Binary file added spec/fixtures/pdfs/1.pdf
Binary file not shown.
Binary file added spec/fixtures/pdfs/2.pdf
Binary file not shown.
15 changes: 15 additions & 0 deletions spec/lib/app_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 18 additions & 0 deletions spec/lib/combine_pdfs_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Loading