Permalink
Browse files

Allow importing a stream for page template

  • Loading branch information...
1 parent b314894 commit c8dfebf2c35857e297a7c742715ba6d7209f1b1f Wes Garrison committed Dec 23, 2011
Showing with 47 additions and 4 deletions.
  1. +17 −4 lib/prawn/core/object_store.rb
  2. +7 −0 manual/templates/page_template.rb
  3. +23 −0 spec/template_spec.rb
View
21 lib/prawn/core/object_store.rb
@@ -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
View
7 manual/templates/page_template.rb
@@ -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
@@ -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"
View
23 spec/template_spec.rb
@@ -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
@@ -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"

0 comments on commit c8dfebf

Please sign in to comment.