Class: Ebookie::Rendering::Pdf

Inherits:
Base
  • Object
show all
Defined in:
lib/ebookie/rendering/pdf.rb

Constant Summary

Constants inherited from Base

Base::IMAGE_SRC_REGEX

Instance Attribute Summary

Attributes inherited from Base

#document

Instance Method Summary collapse

Methods inherited from Base

#clean_images, #format, format, inherited, #initialize, #output_path, #render, #render_erb_to_file, #sanitize_html, set, #template_dir, #template_file, #tmp_dir

Constructor Details

This class inherits a constructor from Ebookie::Rendering::Base

Instance Method Details

#after_initializeObject



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ebookie/rendering/pdf.rb', line 12

def after_initialize
  @wkhtmltopdf_path = Gem.bin_path("wkhtmltopdf-binary-edge")

  @ghostscript_path = `which gs`.chomp
  raise "GhostScript not installed" unless @ghostscript_path

  @tmp_path = tmp_dir.join("document.pdf")
  @pages = []

  settings[:files] << 'cover.html.erb' if convert_cover?
end

#convert_coverObject



73
74
75
76
77
78
79
80
81
# File 'lib/ebookie/rendering/pdf.rb', line 73

def convert_cover
  borrow document.cover.to_s, to: tmp_dir.join('images', File.basename(document.cover))
  cover_path = tmp_dir.join("cover.pdf")

  margins = ['--margin-top 0', '--margin-bottom 0', '--margin-left 0', '--margin-right 0']
  convert_page(tmp_dir.join('cover.html'), cover_path, margins)

  return cover_path
end

#convert_cover?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/ebookie/rendering/pdf.rb', line 69

def convert_cover?
  document.cover && File.extname(document.cover) != '.pdf'
end

#convert_page(input_path, output_path, args = []) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/ebookie/rendering/pdf.rb', line 59

def convert_page(input_path, output_path, args=[])
  args << "--quiet"
  args << "--page-size Letter"
  args << "--title '#{document.title}'"
  command = [@wkhtmltopdf_path] + args
  command << input_path
  command << output_path
  system command.join(' ')
end

#process!Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ebookie/rendering/pdf.rb', line 24

def process!
  convert_page(tmp_dir.join('document.html'), @tmp_path)

  ::PDF::Reader.new(@tmp_path).pages.each_with_index do |page, idx|
    prune_blank_page(idx)
  end

  if document.cover
    if File.extname(document.cover) != '.pdf'
      cover_path = convert_cover
      @pages.unshift cover_path
    else
      borrow document.cover.to_s, to: tmp_dir.join('cover.pdf')
      @pages.unshift tmp_dir.join('cover.pdf')
    end
  end

  pdf = CombinePDF.new
  @pages.compact.each do |page|
    pdf << CombinePDF.new(page)
  end
  pdf.save output_path
end

#prune_blank_page(index) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ebookie/rendering/pdf.rb', line 83

def prune_blank_page(index)
  output_file = tmp_dir.join("page-#{index}.pdf")
  options = ["-q",
             "-dBATCH",
             "-dNOPAUSE",
             "-sDEVICE=pdfwrite",
             "-dFirstPage=#{index+1}",
             "-dLastPage=#{index+1}",
             "-sOutputFile=#{output_file}"]

  system [@ghostscript_path, options.join(' '), @tmp_path].join(' ')

  colors = `#{@ghostscript_path} -q -o - -sDEVICE=inkcov #{output_file}`
  colors = colors.split(" ").uniq[0..-3]
  unless colors.length == 1 && colors.first == "0.00000"
    @pages << tmp_dir.join("page-#{index}.pdf")
  end
end

#sanitize(html) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/ebookie/rendering/pdf.rb', line 48

def sanitize(html)
  html = html.dup
  {
    "" => "&mdash;"
  }.each do |k,v|
    html.gsub! k, v
  end

  sanitize_html clean_images(html, tmp_dir.join("images"))
end