lib/prawn/document.rb in prawn-0.2.3 vs lib/prawn/document.rb in prawn-0.3.0
- old
+ new
@@ -7,23 +7,27 @@
# This is free software. Please see the LICENSE and COPYING files for details.
require "stringio"
require "prawn/document/page_geometry"
require "prawn/document/bounding_box"
-require "prawn/document/text"
+require "prawn/document/text"
require "prawn/document/table"
require "prawn/document/internals"
require "prawn/document/span"
+require "prawn/document/annotations"
+require "prawn/document/destinations"
module Prawn
class Document
include Prawn::Document::Internals
- include Prawn::Graphics
+ include Prawn::Document::Annotations
+ include Prawn::Document::Destinations
+ include Prawn::Graphics
include Prawn::Images
- include Text
- include PageGeometry
+ include Text
+ include PageGeometry
attr_accessor :y, :margin_box
attr_reader :margins, :page_size, :page_layout
# Creates and renders a PDF document.
@@ -57,40 +61,43 @@
# Creates a new PDF Document. The following options are available:
#
# <tt>:page_size</tt>:: One of the Document::PageGeometry::SIZES [LETTER]
# <tt>:page_layout</tt>:: Either <tt>:portrait</tt> or <tt>:landscape</tt>
- # <tt>:on_page_start</tt>:: Optional proc run at each page start
- # <tt>:on_page_stop</tt>:: Optional proc run at each page stop
# <tt>:left_margin</tt>:: Sets the left margin in points [ 0.5 inch]
# <tt>:right_margin</tt>:: Sets the right margin in points [ 0.5 inch]
# <tt>:top_margin</tt>:: Sets the top margin in points [ 0.5 inch]
# <tt>:bottom_margin</tt>:: Sets the bottom margin in points [0.5 inch]
# <tt>:skip_page_creation</tt>:: Creates a document without starting the first page [false]
# <tt>:compress</tt>:: Compresses content streams before rendering them [false]
+ # <tt>:background</tt>:: An image path to be used as background on all pages [nil]
#
# Usage:
#
# # New document, US Letter paper, portrait orientation
# pdf = Prawn::Document.new
#
# # New document, A4 paper, landscaped
# pdf = Prawn::Document.new(:page_size => "A4", :page_layout => :landscape)
#
+ # # New document, with background
+ # pdf = Prawn::Document.new(:background => "#{Prawn::BASEDIR}/data/images/pigs.jpg")
+ #
def initialize(options={},&block)
- Prawn.verify_options [:page_size, :page_layout, :on_page_start,
- :on_page_stop, :left_margin, :right_margin, :top_margin,
- :bottom_margin, :skip_page_creation, :compress, :skip_encoding, :text_options ], options
+ Prawn.verify_options [:page_size, :page_layout, :left_margin,
+ :right_margin, :top_margin, :bottom_margin, :skip_page_creation,
+ :compress, :skip_encoding, :text_options, :background ], options
@objects = []
@info = ref(:Creator => "Prawn", :Producer => "Prawn")
- @pages = ref(:Type => :Pages, :Count => 0, :Kids => [])
- @root = ref(:Type => :Catalog, :Pages => @pages)
+ @pages = ref(:Type => :Pages, :Count => 0, :Kids => [])
+ @root = ref(:Type => :Catalog, :Pages => @pages)
@page_size = options[:page_size] || "LETTER"
@page_layout = options[:page_layout] || :portrait
@compress = options[:compress] || false
@skip_encoding = options[:skip_encoding]
+ @background = options[:background]
text_options.update(options[:text_options] || {})
@margins = { :left => options[:left_margin] || 36,
:right => options[:right_margin] || 36,
@@ -132,11 +139,13 @@
@pages.data[:Kids] << @current_page
@pages.data[:Count] += 1
add_content "q"
- @y = @bounding_box.absolute_top
+ @y = @bounding_box.absolute_top
+
+ image(@background, :at => [0,@y]) if @background
end
# Returns the number of pages in the document
#
# pdf = Prawn::Document.new
@@ -145,10 +154,17 @@
# pdf.page_count #=> 4
#
def page_count
@pages.data[:Count]
end
+
+ # The current y drawing position relative to the innermost bounding box,
+ # or to the page margins at the top level.
+ #
+ def cursor
+ y - bounds.absolute_bottom
+ end
# Renders the PDF document to string
#
def render
output = StringIO.new
@@ -268,10 +284,10 @@
@current_page = ref(:Type => :Page,
:Parent => @pages,
:MediaBox => page_dimensions,
:Contents => @page_content)
- font.add_to_current_page if @font_name
+ font.add_to_current_page if @font
update_colors
end
def generate_margin_box
old_margin_box = @margin_box