-
Notifications
You must be signed in to change notification settings - Fork 4
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
b65926f
commit b1b9380
Showing
22 changed files
with
405 additions
and
8 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
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
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,58 @@ | ||
# frozen_string_literal: true | ||
class PDFGenerator | ||
attr_reader :resource, :storage_adapter | ||
def initialize(resource:, storage_adapter:) | ||
@resource = resource | ||
@storage_adapter = storage_adapter | ||
end | ||
|
||
def render | ||
CoverPageGenerator.new(self).apply(prawn_document) | ||
canvas_downloaders.each_with_index do |downloader, _index| | ||
prawn_document.start_new_page layout: downloader.layout | ||
page_size = [Canvas::LETTER_WIDTH, Canvas::LETTER_HEIGHT] | ||
page_size.reverse! unless downloader.portrait? | ||
prawn_document.image downloader.download, width: downloader.width, height: downloader.height, fit: page_size | ||
end | ||
prawn_document.render_file(tmp_file.path) | ||
build_node | ||
end | ||
|
||
def build_node | ||
file = IngestableFile.new(file_path: tmp_file.path, mime_type: 'application/pdf', original_filename: 'derivative_pdf.pdf') | ||
node = FileMetadata.for(file: file).new(id: SecureRandom.uuid) | ||
stored_file = storage_adapter.upload(resource: node, file: file) | ||
node.file_identifiers = stored_file.id | ||
node | ||
end | ||
|
||
def prawn_document | ||
@prawn_document ||= Prawn::Document.new(prawn_options) | ||
end | ||
|
||
def prawn_options | ||
default_options = { margin: 0 } | ||
default_options[:page_layout] = :portrait if canvas_downloaders.first | ||
default_options | ||
end | ||
|
||
def canvas_images | ||
@canvas_images ||= manifest['sequences'][0]['canvases'].map { |x| x['images'][0] }.map do |x| | ||
Canvas.new(x) | ||
end | ||
end | ||
|
||
def manifest | ||
@manifest ||= ManifestBuilder.new(resource).build | ||
end | ||
|
||
def canvas_downloaders | ||
@canvas_images ||= canvas_images.map do |image| | ||
CanvasDownloader.new(image, quality: resource.pdf_type.first) | ||
end | ||
end | ||
|
||
def tmp_file | ||
@tmp_file ||= Tempfile.new("pdf") | ||
end | ||
end |
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,25 @@ | ||
# frozen_string_literal: true | ||
class PDFGenerator | ||
class Canvas | ||
# Letter width/height in points for a PDF. | ||
LETTER_WIDTH = PDF::Core::PageGeometry::SIZES["LETTER"].first | ||
LETTER_HEIGHT = PDF::Core::PageGeometry::SIZES["LETTER"].last | ||
BITONAL_SIZE = 2000 | ||
attr_reader :canvas | ||
def initialize(canvas) | ||
@canvas = canvas | ||
end | ||
|
||
def width | ||
canvas["resource"]["width"].to_i | ||
end | ||
|
||
def height | ||
canvas["resource"]["height"].to_i | ||
end | ||
|
||
def url | ||
canvas["resource"]["service"]["@id"] | ||
end | ||
end | ||
end |
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,63 @@ | ||
# frozen_string_literal: true | ||
class PDFGenerator | ||
class CanvasDownloader | ||
attr_reader :canvas | ||
delegate :width, :height, to: :canvas | ||
def initialize(canvas, quality: "grey") | ||
@canvas = canvas | ||
@quality = quality | ||
end | ||
|
||
def download | ||
open(canvas_url, 'rb') | ||
end | ||
|
||
def layout | ||
if portrait? | ||
:portrait | ||
else | ||
:landscape | ||
end | ||
end | ||
|
||
def quality | ||
if @quality == 'gray' | ||
'grey' | ||
else | ||
'default' | ||
end | ||
end | ||
|
||
def portrait? | ||
canvas.width <= canvas.height | ||
end | ||
|
||
private | ||
|
||
def canvas_url | ||
"#{canvas.url}/full/#{max_width},#{max_height}/0/#{quality}.#{format}" | ||
end | ||
|
||
def format | ||
bitonal? ? 'png' : 'jpg' | ||
end | ||
|
||
def max_width | ||
return Canvas::BITONAL_SIZE if bitonal? | ||
[(Canvas::LETTER_WIDTH * scale_factor).round, canvas.width].min | ||
end | ||
|
||
def max_height | ||
return Canvas::BITONAL_SIZE if bitonal? | ||
[(Canvas::LETTER_HEIGHT * scale_factor).round, canvas.height].min | ||
end | ||
|
||
def scale_factor | ||
1.5 | ||
end | ||
|
||
def bitonal? | ||
quality == 'bitonal' | ||
end | ||
end | ||
end |
Oops, something went wrong.