Skip to content

Commit

Permalink
Merge pull request #663 from ShelterTechSF/1188-html-to-pdf-endpoint
Browse files Browse the repository at this point in the history
1188 html to pdf endpoint
  • Loading branch information
schroerbrian authored Feb 1, 2023
2 parents 3687db2 + d050c89 commit e3f28cc
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,5 @@ end
group :test do
gem 'rspec-collection_matchers'
end

gem 'pdfcrowd'
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ GEM
parallel (1.21.0)
parser (3.0.2.0)
ast (~> 2.4.1)
pdfcrowd (5.10.0)
pg (1.2.3)
phonelib (0.6.54)
prometheus-client (2.1.0)
Expand Down Expand Up @@ -264,6 +265,7 @@ DEPENDENCIES
jsonite
jwt
lograge
pdfcrowd
pg
phonelib
prometheus-client
Expand Down
17 changes: 17 additions & 0 deletions app/controllers/services_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,23 @@ def show
render json: ServicesWithResourcePresenter.present(service)
end

def init_pdf_crowd_client
Pdfcrowd::HtmlToPdfClient.new(Rails.configuration.x.pdfcrowd.username, Rails.configuration.x.pdfcrowd.api_key)
end

def html_to_pdf
if Rails.configuration.x.pdfcrowd.api_key && Rails.configuration.x.pdfcrowd.username
client = init_pdf_crowd_client
pdf = client.convertString(params[:html])
send_data pdf,
{ type: "application/pdf",
disposition: "attachment; filename*=UTF-8''#{ERB::Util.url_encode('result.pdf')} }" }
end
rescue Pdfcrowd::Error => e
puts "Failed to convert HTML to PDF: #{e}"
render plain: "There was an error getting the PDF. Please try again", status: 500
end

def featured
category_id = params[:category_id]
featured_services = services.includes(
Expand Down
4 changes: 4 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@
config.lograge.enabled = true

config.hosts << 'askdarcel.lvh.me'

# Public API key provided by PDF Crowd
config.x.pdfcrowd.api_key = 'ce544b6ea52a5621fb9d55f8b542d14d'
config.x.pdfcrowd.username = 'demo'
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
get :pending
get :count
get :search
post :html_to_pdf
end
end
resources :notes do
Expand Down

0 comments on commit e3f28cc

Please sign in to comment.