Class: SilkLayout::Render::PdfRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/silk_layout/render/pdf_renderer.rb

Constant Summary collapse

CSS_DPI =
96.0
PDF_DPI =
72.0
PAGE_WIDTH_PX =
800
PAGE_HEIGHT_PX =
1000

Class Method Summary collapse

Class Method Details

.px_to_pt(px) ⇒ Object



26
27
28
# File 'lib/silk_layout/render/pdf_renderer.rb', line 26

def self.px_to_pt(px)
  px * PDF_DPI / CSS_DPI
end

.render(box_tree, output_path) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/silk_layout/render/pdf_renderer.rb', line 14

def self.render(box_tree, output_path)
  doc = HexaPDF::Document.new

  page_width_pt = px_to_pt(PAGE_WIDTH_PX)
  page_height_pt = px_to_pt(PAGE_HEIGHT_PX)

  page = doc.pages.add([0, 0, page_width_pt, page_height_pt])
  Painter.paint(page.canvas, box_tree, page_height_pt)

  doc.write(output_path)
end