From dbd8616a9e8ba9855d86561a58a3c085e6a6dae4 Mon Sep 17 00:00:00 2001 From: takahashim Date: Mon, 24 Jul 2023 23:08:50 +0900 Subject: [PATCH 1/7] Use Ferrum for PDF --- Gemfile | 2 ++ Gemfile.lock | 7 ++++ config/initializers/decidim.rb | 1 + lib/decidim/exporters/pdf.rb | 64 ++++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+) create mode 100644 lib/decidim/exporters/pdf.rb diff --git a/Gemfile b/Gemfile index 1890a3756..4c92bc7d8 100644 --- a/Gemfile +++ b/Gemfile @@ -35,6 +35,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" diff --git a/Gemfile.lock b/Gemfile.lock index abd33be8d..bed2d7eae 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -456,6 +456,11 @@ GEM faraday-net_http (3.1.0) net-http ffi (1.16.3) + ferrum (0.13) + addressable (~> 2.5) + concurrent-ruby (~> 1.1) + webrick (~> 1.7) + websocket-driver (>= 0.6, < 0.8) figaro (1.2.0) thor (>= 0.14.0, < 2) file_validators (3.0.0) @@ -900,6 +905,7 @@ GEM webpush (1.1.0) hkdf (~> 0.2) jwt (~> 2.0) + webrick (1.8.1) websocket-driver (0.7.6) websocket-extensions (>= 0.1.0) websocket-extensions (0.1.5) @@ -932,6 +938,7 @@ DEPENDENCIES dotenv-rails factory_bot_rails faker (~> 3.2) + ferrum figaro fog-aws image_processing diff --git a/config/initializers/decidim.rb b/config/initializers/decidim.rb index f66e4f8ae..5380b4af7 100644 --- a/config/initializers/decidim.rb +++ b/config/initializers/decidim.rb @@ -490,6 +490,7 @@ Decidim.register_assets_path File.expand_path("app/packs", Rails.application.root) require "decidim/map/provider/static_map/cfj_osm" +require "decidim/exporters/pdf" ## Set default OGP description length limit. It's used in Decidim::Blogs components Rails.application.config.default_blog_ogp_description_limit = ENV.fetch("DECIDIM_BLOG_OGP_DESCRIPTION_LIMIT", 150).to_i diff --git a/lib/decidim/exporters/pdf.rb b/lib/decidim/exporters/pdf.rb new file mode 100644 index 000000000..d92b49c5b --- /dev/null +++ b/lib/decidim/exporters/pdf.rb @@ -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 From 91fd8f54ef8ebc3753df43759de73a9fc9252789 Mon Sep 17 00:00:00 2001 From: takahashim Date: Mon, 31 Jul 2023 19:46:51 +0900 Subject: [PATCH 2/7] Fix some settings to generate PDF --- lib/decidim/exporters/pdf.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/decidim/exporters/pdf.rb b/lib/decidim/exporters/pdf.rb index d92b49c5b..2b1704d26 100644 --- a/lib/decidim/exporters/pdf.rb +++ b/lib/decidim/exporters/pdf.rb @@ -22,14 +22,17 @@ def export 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 = browser.pdf(path: nil, + encoding: :binary, + landscape: orientation != "Portrait", + printBackground: true, + scale: 0.8, + format: :A4) - document = File.read(pdf_path) ExportData.new(document, "pdf") end end From d09804be1b2f9d6cfdee50b33272214d95339d64 Mon Sep 17 00:00:00 2001 From: takahashim Date: Fri, 4 Aug 2023 10:30:41 +0900 Subject: [PATCH 3/7] WIP --- lib/decidim/exporters/pdf.rb | 38 ++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/lib/decidim/exporters/pdf.rb b/lib/decidim/exporters/pdf.rb index 2b1704d26..9e7ea11d4 100644 --- a/lib/decidim/exporters/pdf.rb +++ b/lib/decidim/exporters/pdf.rb @@ -19,22 +19,9 @@ def export locals: locals ) - Dir.mktmpdir do |dir| - html_path = File.join(dir, "tmp.html") - File.write(html_path, html) - url = URI::File.build([nil, html_path]) - - browser = Ferrum::Browser.new - browser.go_to(url) - document = browser.pdf(path: nil, - encoding: :binary, - landscape: orientation != "Portrait", - printBackground: true, - scale: 0.8, - format: :A4) + document = pdf_from_string(html, orientation:) - ExportData.new(document, "pdf") - end + ExportData.new(document, "pdf") end # may be overwritten if needed @@ -62,6 +49,27 @@ def locals def controller raise NotImplementedError end + + private + + def pdf_from_string(html, orientation:) + Dir.mktmpdir do |dir| + html_path = File.join(dir, "tmp.html") + File.write(html_path, html) + url = URI::File.build([nil, html_path]) + + browser = Ferrum::Browser.new + browser.go_to(url) + document = browser.pdf(path: nil, + encoding: :binary, + landscape: orientation != "Portrait", + printBackground: true, + scale: 0.8, + format: :A4) + end + + document + end end end end From 9f71b456da9f55c6d2417b954d6229b43e2faaff Mon Sep 17 00:00:00 2001 From: takahashim Date: Wed, 24 Apr 2024 23:18:26 +0900 Subject: [PATCH 4/7] fix Gemfile.lock --- Gemfile.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile.lock b/Gemfile.lock index bed2d7eae..82944f457 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -455,12 +455,12 @@ GEM multipart-post (~> 2) faraday-net_http (3.1.0) net-http - ffi (1.16.3) ferrum (0.13) addressable (~> 2.5) concurrent-ruby (~> 1.1) webrick (~> 1.7) websocket-driver (>= 0.6, < 0.8) + ffi (1.16.3) figaro (1.2.0) thor (>= 0.14.0, < 2) file_validators (3.0.0) From af694ca613a768e5ac3764acfb5559e1953d3820 Mon Sep 17 00:00:00 2001 From: takahashim Date: Wed, 24 Apr 2024 23:34:51 +0900 Subject: [PATCH 5/7] fix syntax --- lib/decidim/exporters/pdf.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/decidim/exporters/pdf.rb b/lib/decidim/exporters/pdf.rb index 9e7ea11d4..64a8eff3c 100644 --- a/lib/decidim/exporters/pdf.rb +++ b/lib/decidim/exporters/pdf.rb @@ -19,7 +19,7 @@ def export locals: locals ) - document = pdf_from_string(html, orientation:) + document = pdf_from_string(html, orientation: orientation) ExportData.new(document, "pdf") end @@ -53,6 +53,8 @@ def controller private def pdf_from_string(html, orientation:) + document = nil + Dir.mktmpdir do |dir| html_path = File.join(dir, "tmp.html") File.write(html_path, html) From 3a8b0427b9bddd1fa3e5283e3639ddc08f0b3d93 Mon Sep 17 00:00:00 2001 From: takahashim Date: Wed, 8 May 2024 00:32:47 +0900 Subject: [PATCH 6/7] fix error in Decidim::Forms::Admin::QuestionnaireParticipantPresenter --- lib/decidim/exporters/pdf.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/decidim/exporters/pdf.rb b/lib/decidim/exporters/pdf.rb index 64a8eff3c..100b75e9f 100644 --- a/lib/decidim/exporters/pdf.rb +++ b/lib/decidim/exporters/pdf.rb @@ -1,5 +1,8 @@ # frozen_string_literal: true +# To be correctly loaded by Zeitwerk +Decidim::Forms::Admin::QuestionnaireAnswerPresenter + module Decidim module Exporters # Exports a PDF using the provided hash, given a collection and a From 6abcf84e4bd5e0e33c5b74d9d57046e484dbcd2d Mon Sep 17 00:00:00 2001 From: takahashim Date: Wed, 8 May 2024 00:47:30 +0900 Subject: [PATCH 7/7] fix by rubocop --- lib/decidim/exporters/pdf.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/decidim/exporters/pdf.rb b/lib/decidim/exporters/pdf.rb index 100b75e9f..bee591d62 100644 --- a/lib/decidim/exporters/pdf.rb +++ b/lib/decidim/exporters/pdf.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # To be correctly loaded by Zeitwerk -Decidim::Forms::Admin::QuestionnaireAnswerPresenter +Decidim::Forms::Admin::QuestionnaireAnswerPresenter # rubocop:disable Lint/Void module Decidim module Exporters