Class: HTMLDocPDF::HTMLDoc

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

Overview

The wrapper class around HTMLDOC, providing methods for setting the options for the application and retriving the generate output either as a file, diretory or string.

Constant Summary collapse

@@basic_options =
[:bodycolor, :bodyfont, :bodyimage, :bottom, :browserwidth,
:charset, :continuous, :cookies, :datadir, :effectduration,
:firstpage, :fontsize, :fontspacing, :footer, :gray, :header,
:headerfontfoot, :headfontsize, :headingfont, :landscape,
:left, :linkcolor, :linkstyle, :logoimage, :nup, :outdir,
:outfile, :owner_password, :pageduration, :pageeffect,
:pagelayout, :pagemode, :path, :permissions, :portrait,
:referer, :right, :size, :textcolor, :textfont, :titlefile,
:titleimage, :tocfooter, :tocheader, :toclevels, :toctitle,
:user_password, :webpage]
@@extra_options =
[:compression, :duplex, :embedfonts, :encryption, :jpeg,
:links, :localfiles, :numbered, :pscommands, :strict, :title,
:toc, :xrxcomments]
@@all_options =
@@basic_options + @@extra_options
@@program_path =

The path to HTMLDOC in the system. E.g, /usr/bin/html or "C:\Program Files\HTMLDOC\HTMLDOC.exe".

"htmldoc"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(format = PDF) ⇒ HTMLDoc

Creates a blank HTMLDOC wrapper, using format to indicate whether the output will be HTML, PDF or PS. The format defaults to PDF, and can change using one of the module contants.



66
67
68
69
70
71
72
# File 'lib/htmldoc.rb', line 66

def initialize(format = PDF)
  @format = format
  @options = {}
  @pages = []
  @tempfiles = []
  reset
end

Instance Attribute Details

#errorsObject (readonly)

The last error messages generate by the command. It’s a hash where they key represents the error number, and the value represents the error message. If the error number is zero, HTMLDOC was called with invalid parameters. Errors can happen even if generation succeeds, for example, if an image can’t be found in the course of the generation.



60
61
62
# File 'lib/htmldoc.rb', line 60

def errors
  @errors
end

#resultObject (readonly)

The last result from the generation of the output file(s). It’s a hash comprising three pairs:

bytes

The number of bytes generated in the last request or nil

pages

The number of pages generated in the last request or nil

output

The raw output of the command



52
53
54
# File 'lib/htmldoc.rb', line 52

def result
  @result
end

Class Method Details

.create(format = PDF, &block) ⇒ Object

Creates a blank HTMLDOC wrapper and passes it to a block. When the block finishes running, the generate method is automatically called. The result of generate is then passed back to the application.



78
79
80
81
82
83
84
# File 'lib/htmldoc.rb', line 78

def self.create(format = PDF, &block)
  pdf = HTMLDoc.new(format)
  if block_given?
    yield pdf
    pdf.generate
  end
end

.program_pathObject

Gets the current path for the HTMLDOC executable. This is a class method.



88
89
90
# File 'lib/htmldoc.rb', line 88

def self.program_path
  @@program_path
end

.program_path=(value) ⇒ Object

Sets the current path for the HTMLDOC executable. This is a class method.



94
95
96
# File 'lib/htmldoc.rb', line 94

def self.program_path=(value)
  @@program_path = value
end

Instance Method Details

#add_page(page) ⇒ Object Also known as: <<

Adds a page for generation. The page can be a URL beginning with either http:// or https://; a file, which will be verified for existence; or any text.



129
130
131
132
133
134
135
136
137
138
# File 'lib/htmldoc.rb', line 129

def add_page(page)
  if /\A(http|https)/ =~ page && page !~ /\r|\n/
    type = :url
  elsif File.exists?(page)
    type = :file
  else
    type = :text
  end
  @pages << { :type => type, :value => page }
end

Sets the footer. It’s the same as set_option :footer, value.



122
123
124
# File 'lib/htmldoc.rb', line 122

def footer(value)
  set_option :footer, value
end

#generateObject

Invokes HTMLDOC and generates the output. If an output directory or file is provided, the method will return true or false to indicate completion. If no output directory or file is provided, it will return a string representing the entire output. Generate will raise a HTMLDocPDF::HTMLDocException if the program path can’t be found.



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/htmldoc.rb', line 148

def generate
  tempfile = nil
  unless @options[:outdir] || @options[:outfile]
    tempfile = Tempfile.new("htmldoc.temp")
    @options[:outfile] = tempfile.path
  end
  execute
  if @result[:bytes]
    if tempfile
      File.open(tempfile.path, "rb") { |f| f.read }
    else
      true
    end
  else
    false
  end
ensure
  if tempfile
    tempfile.close
    @options[:outfile] = nil
  end
  @tempfiles.each { |t| t.close }
end

#header(value) ⇒ Object

Sets the header. It’s the same as set_option :header, value.



117
118
119
# File 'lib/htmldoc.rb', line 117

def header(value)
  set_option :header, value
end

#set_option(option, value) ⇒ Object

Sets an option for the wrapper. Only valid HTMLDOC options will be accepted. The name of the option is a symbol, but the value can be anything. Invalid options will throw an exception. To unset an option, use nil as the value. Options with negated counterparts, like :encryption, can be set using false, :no or :none as the value.



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/htmldoc.rb', line 104

def set_option(option, value)
  if @@all_options.include?(option)
    if !value.nil?
      @options[option] = value
    else
      @options.delete(option)
    end
  else
    raise HTMLDocException.new("Invalid option #{option.to_s}")
  end
end