Skip to content

Commit

Permalink
Use Ferrum for PDF
Browse files Browse the repository at this point in the history
  • Loading branch information
takahashim committed Jul 31, 2023
1 parent f5137b2 commit ce8b4a4
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ gem "decidim-user_extension", path: "decidim-user_extension"

gem "slack-ruby-client"

gem "ferrum"

group :development, :test do
gem "byebug", "~> 11.0", platform: :mri
gem "figaro"
Expand Down
7 changes: 7 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,11 @@ GEM
faraday-multipart (1.0.4)
multipart-post (~> 2)
faraday-net_http (3.0.2)
ferrum (0.13)
addressable (~> 2.5)
concurrent-ruby (~> 1.1)
webrick (~> 1.7)
websocket-driver (>= 0.6, < 0.8)
ffi (1.15.5)
figaro (1.2.0)
thor (>= 0.14.0, < 2)
Expand Down Expand Up @@ -870,6 +875,7 @@ GEM
rack-proxy (>= 0.6.1)
railties (>= 5.2)
semantic_range (>= 2.3.0)
webrick (1.8.1)
websocket-driver (0.7.5)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
Expand Down Expand Up @@ -903,6 +909,7 @@ DEPENDENCIES
dotenv-rails
factory_bot_rails
faker (~> 2.14)
ferrum
figaro
fog-aws
image_processing
Expand Down
1 change: 1 addition & 0 deletions config/initializers/decidim.rb
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@
end

require "decidim/map/provider/static_map/cfj_osm"
require "decidim/exporters/pdf"

Rails.application.config.i18n.available_locales = Decidim.available_locales
Rails.application.config.i18n.default_locale = Decidim.default_locale
Expand Down
64 changes: 64 additions & 0 deletions lib/decidim/exporters/pdf.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# frozen_string_literal: true

module Decidim
module Exporters
# Exports a PDF using the provided hash, given a collection and a
# Serializer. This is an abstract class that should be inherited
# to create PDF exporters, with each PDF exporter class setting
# the desired template, layout and orientation.
#
class PDF < Exporter
# Public: Exports a PDF version of the collection by rendering
# the template into html and then converting it to PDF.
#
# Returns an ExportData instance.
def export
html = controller.render_to_string(
template: template,
layout: layout,
locals: locals
)

Dir.mktmpdir do |dir|
html_path = File.join(dir, "tmp.html")
File.write(html_path, html)
pdf_path = File.join(dir, "tmp.pdf")
url = URI::File.build([nil, html_path])

browser = Ferrum::Browser.new
browser.go_to(url)
browser.pdf(path: pdf_path, landscape: orientation != "Portrait", printBackground: true)

document = File.read(pdf_path)
ExportData.new(document, "pdf")
end
end

# may be overwritten if needed
def orientation
"Portrait"
end

# implementing classes should return a valid ERB path here
def template
raise NotImplementedError
end

# implementing classes should return a valid ERB path here
def layout
raise NotImplementedError
end

# This method may be overwritten if the template needs more local variables
def locals
{ collection: collection }
end

protected

def controller
raise NotImplementedError
end
end
end
end

0 comments on commit ce8b4a4

Please sign in to comment.