Class: ImgToPdf::FitPageDocument

Inherits:
Object
  • Object
show all
Defined in:
lib/img_to_pdf/fit_page_document.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page_dimension_pt:, margin_pt:, image:, n_horizontal_pages:, n_vertical_pages:) ⇒ FitPageDocument

Returns a new instance of FitPageDocument.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/img_to_pdf/fit_page_document.rb', line 22

def initialize(page_dimension_pt:, margin_pt:, image:,
               n_horizontal_pages:, n_vertical_pages:)
  @page_dimension_pt = page_dimension_pt
  @margin_pt = margin_pt
  @image = image
  @n_horizontal_pages = n_horizontal_pages
  @n_vertical_pages = n_vertical_pages

  @pdf = Prawn::Document.new(
    page_size: @page_dimension_pt.to_a,
    margin: @margin_pt.to_a,
    skip_page_creation: true,
    info: {
      Creator: "img_to_pdf version #{ImgToPdf::VERSION}",
    },
  )
end

Class Method Details

.create(page_dimension_pt:, margin_pt:, image:, n_horizontal_pages: 1, n_vertical_pages: 1) ⇒ ImgToPdf::FitPageDocument

Returns document to render PDF.

Parameters:

  • page_dimension_pt (ImgToPdf::Dimension)

    page size. points.

  • margin_pt (ImgToPdf::Margin)

    margin size. points.

  • image (ImgToPdf::Image)

    source image.

  • n_horizontal_pages (Integer) (defaults to: 1)

    number of horizontal pages.

  • n_vertical_pages (Integer) (defaults to: 1)

    number of vertical pages.

Returns:



12
13
14
15
16
17
18
19
20
# File 'lib/img_to_pdf/fit_page_document.rb', line 12

def self.create(page_dimension_pt:, margin_pt:, image:,
                n_horizontal_pages: 1, n_vertical_pages: 1)
  result = new(page_dimension_pt: page_dimension_pt, margin_pt: margin_pt,
               n_horizontal_pages: n_horizontal_pages,
               n_vertical_pages: n_vertical_pages,
               image: image)
  result.draw
  return result
end

Instance Method Details

#drawObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/img_to_pdf/fit_page_document.rb', line 40

def draw
  canvas_dimension_pt = determine_canvas_dimension_pt
  canvas_scale = determine_canvas_scale(canvas_dimension_pt)
  sub_image_dimension_px = ImgToPdf::Dimension.new(
    width: @image.dimension_px.width / @n_horizontal_pages,
    height: @image.dimension_px.height / @n_vertical_pages,
  )
  each_sub_image(sub_image_dimension_px) do |sub_image|
    @pdf.start_new_page
    @pdf.image(
      sub_image.path,
      scale: canvas_scale,
      at: [0, canvas_dimension_pt.height / @n_vertical_pages],
    )
  end
end

#render_file(output_path) ⇒ Object

Parameters:

  • output_path (Pathname)

    path to PDF output.



58
59
60
# File 'lib/img_to_pdf/fit_page_document.rb', line 58

def render_file(output_path)
  @pdf.render_file(output_path)
end