From 676fb4bfe3c017ed177cbae312115df7a6a6458a Mon Sep 17 00:00:00 2001 From: takahashim Date: Fri, 4 Aug 2023 10:30:41 +0900 Subject: [PATCH] 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