Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/databasically/prawn
Browse files Browse the repository at this point in the history
  • Loading branch information
bradediger committed Dec 30, 2011
2 parents a717b54 + c8dfebf commit 080e438
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
21 changes: 17 additions & 4 deletions lib/prawn/core/object_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,26 @@ def object_id_for_page(k)
# Imports nothing and returns nil if the requested page number doesn't
# exist. page_num is 1 indexed, so 1 indicates the first page.
#
def import_page(filename, page_num)
def import_page(input, page_num)
@loaded_objects = {}
unless File.file?(filename)
raise ArgumentError, "#{filename} does not exist"

io = if input.respond_to?(:seek) && input.respond_to?(:read)
input
elsif File.file?(input.to_s)
if File.respond_to?(:binread)
StringIO.new(File.binread(input.to_s))
else
StringIO.new(File.read(input.to_s))
end
else
raise ArgumentError, "input must be an IO-like object or a filename"
end

hash = PDF::Reader::ObjectHash.new(filename)
# unless File.file?(filename)
# raise ArgumentError, "#{filename} does not exist"
# end

hash = PDF::Reader::ObjectHash.new(io)
ref = hash.page_references[page_num - 1]

ref.nil? ? nil : load_object_graph(hash, ref).identifier
Expand Down
7 changes: 7 additions & 0 deletions manual/templates/page_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
# with the <code>start_new_page</code> method. You may pass it a
# <code>:template</code> option with the path for an existing pdf and a
# <code>:template_page</code> option to specify which page to load.
# You can also load a <code>:template</code> using a URI:
#
# <code>require 'open-uri'</code>
#
# <code> start_new_page(:template => open('http://server.com/document.pdf'))</code>
#
# The following example loads some pages from an existing PDF. If we don't
# specify the <code>:template_page</code> option, the first page of the template
Expand All @@ -20,6 +25,8 @@
move_down 10
text "You also might want to look at the pdf used as a template: "
url = "https://github.com/sandal/prawn/raw/master/data/pdfs/form.pdf"
move_down 10

formatted_text [{:text => url, :link => url}]

filename = "#{Prawn::DATADIR}/pdfs/form.pdf"
Expand Down
23 changes: 23 additions & 0 deletions spec/template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@
str = @pdf.render
str[0,4].should == "%PDF"
end

context "with the template as a stream" do
it "should correctly import a template file from a stream" do
filename = "#{Prawn::DATADIR}/pdfs/hexagon.pdf"
io = StringIO.new(File.read(filename))
@pdf = Prawn::Document.new(:template => io)
str = @pdf.render
str[0,4].should == "%PDF"
end
end

end

Expand Down Expand Up @@ -284,6 +294,19 @@
fonts.size.should == 2
end

context "with the template as a stream" do
it "should correctly import a template file from a stream" do
filename = "#{Prawn::DATADIR}/pdfs/hexagon.pdf"
io = StringIO.new(File.read(filename))

@pdf = Prawn::Document.new()
@pdf.start_new_page(:template => io)

str = @pdf.render
str[0,4].should == "%PDF"
end
end

context "using template_page option" do
it "uses the specified page option" do
filename = "#{Prawn::DATADIR}/pdfs/multipage_template.pdf"
Expand Down

0 comments on commit 080e438

Please sign in to comment.