lib/pdf/wrapper.rb in pdf-wrapper-0.3.0 vs lib/pdf/wrapper.rb in pdf-wrapper-0.3.1
- old
+ new
@@ -101,11 +101,12 @@
# create a new PDF::Wrapper class to compose a PDF document
# Params:
# <tt>output</tt>:: Where to render the PDF to. Can be a string containing a filename,
# or an IO object (File, StringIO, etc)
# Options:
- # <tt>:paper</tt>:: The paper size to use (default :A4)
+ # <tt>:paper</tt>:: The paper size to use (default :A4). Can be a predefined paper size,
+ # or an array of [width, height]
# <tt>:orientation</tt>:: :portrait (default) or :landscape
# <tt>:background_color</tt>:: The background colour to use (default :white)
# <tt>:margin_top</tt>:: The size of the default top margin (default 5% of page)
# <tt>:margin_bottom</tt>:: The size of the default bottom margin (default 5% of page)
# <tt>:margin_left</tt>:: The size of the default left margin (default 5% of page)
@@ -152,11 +153,11 @@
:background_color => :white
}
options.merge!(opts)
# test for invalid options
- options.assert_valid_keys(:paper, :orientation, :background_color, :margin_left, :margin_right,
+ options.assert_valid_keys(:paper, :orientation, :background_color, :margin_left, :margin_right,
:margin_top, :margin_bottom, :io, :template)
set_dimensions(options[:orientation], options[:paper])
# set page margins and dimensions of usable canvas
@@ -383,11 +384,11 @@
finish
case @output
when StringIO then
File.open(filename, "w") do |of|
of.write(@output.string)
- end
+ end
when File then return FileUtils.cp(@output.path, filename)
else
return FileUtils.cp(@output, filename)
end
end
@@ -605,9 +606,28 @@
end
private
def set_dimensions(orientation, paper)
+ return if orientation.nil? || paper.nil?
+
+ if paper.is_a?(Array)
+ set_manual_dimensions(*paper)
+ else
+ set_predefined_dimensions(orientation, paper)
+ end
+ end
+
+ def set_manual_dimensions(*args)
+ @page_width, @page_height = *args
+ if @page_width > @page_height
+ @orientation = :landscape
+ else
+ @orientation = :portrait
+ end
+ end
+
+ def set_predefined_dimensions(orientation, paper)
# use the defaults if none were provided
orientation ||= @orientation
paper ||= @paper
# safety check