-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f5137b2
commit ce8b4a4
Showing
4 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |