Skip to content

Commit e3f28cc

Browse files
authored
Merge pull request #663 from ShelterTechSF/1188-html-to-pdf-endpoint
1188 html to pdf endpoint
2 parents 3687db2 + d050c89 commit e3f28cc

5 files changed

Lines changed: 26 additions & 0 deletions

File tree

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,5 @@ end
5353
group :test do
5454
gem 'rspec-collection_matchers'
5555
end
56+
57+
gem 'pdfcrowd'

Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ GEM
149149
parallel (1.21.0)
150150
parser (3.0.2.0)
151151
ast (~> 2.4.1)
152+
pdfcrowd (5.10.0)
152153
pg (1.2.3)
153154
phonelib (0.6.54)
154155
prometheus-client (2.1.0)
@@ -264,6 +265,7 @@ DEPENDENCIES
264265
jsonite
265266
jwt
266267
lograge
268+
pdfcrowd
267269
pg
268270
phonelib
269271
prometheus-client

app/controllers/services_controller.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,23 @@ def show
5454
render json: ServicesWithResourcePresenter.present(service)
5555
end
5656

57+
def init_pdf_crowd_client
58+
Pdfcrowd::HtmlToPdfClient.new(Rails.configuration.x.pdfcrowd.username, Rails.configuration.x.pdfcrowd.api_key)
59+
end
60+
61+
def html_to_pdf
62+
if Rails.configuration.x.pdfcrowd.api_key && Rails.configuration.x.pdfcrowd.username
63+
client = init_pdf_crowd_client
64+
pdf = client.convertString(params[:html])
65+
send_data pdf,
66+
{ type: "application/pdf",
67+
disposition: "attachment; filename*=UTF-8''#{ERB::Util.url_encode('result.pdf')} }" }
68+
end
69+
rescue Pdfcrowd::Error => e
70+
puts "Failed to convert HTML to PDF: #{e}"
71+
render plain: "There was an error getting the PDF. Please try again", status: 500
72+
end
73+
5774
def featured
5875
category_id = params[:category_id]
5976
featured_services = services.includes(

config/environments/development.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,8 @@
3636
config.lograge.enabled = true
3737

3838
config.hosts << 'askdarcel.lvh.me'
39+
40+
# Public API key provided by PDF Crowd
41+
config.x.pdfcrowd.api_key = 'ce544b6ea52a5621fb9d55f8b542d14d'
42+
config.x.pdfcrowd.username = 'demo'
3943
end

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
get :pending
4545
get :count
4646
get :search
47+
post :html_to_pdf
4748
end
4849
end
4950
resources :notes do

0 commit comments

Comments
 (0)