Class: CBETA::HTMLToPDF

Inherits:
Object
  • Object
show all
Defined in:
lib/cbeta/html_to_pdf.rb

Instance Method Summary collapse

Constructor Details

#initialize(input, output, converter) ⇒ HTMLToPDF

Returns a new instance of HTMLToPDF.

Examples:

c = CBETA::HTMLToPDF.new('/temp/cbeta-html', '/temp/cbeta-pdf', "prince %{in} -o %{out}")

Parameters:

  • input (String)

    folder of source HTML, HTML can be produced by CBETA::P5aToHTMLForPDF.

  • output (String)

    output folder

  • converter (String)

    shell command to convert HTML to PDF

    • suggestion: www.princexml.com/

    • wkhtmltopdf has font problem to display unicode extb characters



12
13
14
15
16
# File 'lib/cbeta/html_to_pdf.rb', line 12

def initialize(input, output, converter)
  @input = input
  @output = output
  @converter = converter
end

Instance Method Details

#convert(target = nil) ⇒ Object

Convert CBETA HTML to PDF

T 是大正藏的 ID, CBETA 的藏經 ID 系統請參考: www.cbeta.org/format/id.php

Examples:

for convert Taisho (大正藏) Volumn 1:


c = CBETA::HTMLToPDF.new('/PATH/TO/CBETA/XML/P5a', '/OUTPUT/FOLDER')
c.convert('T01')

for convert all in Taisho (大正藏):


c = CBETA::HTMLToPDF.new('/PATH/TO/CBETA/XML/P5a', '/OUTPUT/FOLDER')
c.convert('T')

for convert Taisho Vol. 5~7:


c = CBETA::P5aToHTMLForPDF.new('/PATH/TO/CBETA/XML/P5a', '/OUTPUT/FOLDER')
c.convert('T05..T07')


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/cbeta/html_to_pdf.rb', line 36

def convert(target=nil)
  return convert_all if target.nil?

  arg = target.upcase
  if arg.size <= 2
    convert_collection(arg)
  else
    if arg.include? '..'
      arg.match(/^([^\.]+?)\.\.([^\.]+)$/) {
        convert_vols($1, $2)
      }
    else
      convert_vol(arg)
    end
  end
end

#convert_collection(c) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/cbeta/html_to_pdf.rb', line 53

def convert_collection(c)
  @canon = c
  puts 'convert_collection ' + c
  
  output_folder = File.join(@output, @canon)
  FileUtils.mkdir_p(output_folder) unless Dir.exist? output_folder
  
  folder = File.join(@input, @canon)
  Dir.foreach(folder) { |f|
    next if f.start_with? '.'
    src = File.join(folder, f, 'main.htm')
    dest = File.join(output_folder, "#{f}.pdf")
    convert_file(src, dest)
  }
end

#convert_file(html_fn, pdf_fn) ⇒ Object



69
70
71
72
73
# File 'lib/cbeta/html_to_pdf.rb', line 69

def convert_file(html_fn, pdf_fn)
  puts "convert file: #{html_fn} to #{pdf_fn}"
  cmd = @converter % { in: html_fn, out: pdf_fn}
  `#{cmd}`
end