Skip to content

Commit

Permalink
Add Composer#write_to_string
Browse files Browse the repository at this point in the history
  • Loading branch information
gettalong committed Sep 24, 2024
1 parent 9db2c9a commit dc66c47
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

* [HexaPDF::Task::MergeAcroForm] for merging AcroForm information for imported
pages
* [HexaPDF::Document#write_to_string] for easily writing a document to a String
* [HexaPDF::Document#write_to_string] and [HexaPDF::Composer#write_to_string]
for easily writing a document to a String

### Changed

Expand Down
7 changes: 7 additions & 0 deletions lib/hexapdf/composer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,13 @@ def write(output, optimize: true, **options)
@document.write(output, optimize: optimize, **options)
end

# Writes the created PDF document to a string and returns that string.
#
# See HexaPDF::Document#write for details.
def write_to_string(optimize: true, **options)
@document.write_to_string(optimize: optimize, **options)
end

# :call-seq:
# composer.style(name) -> style
# composer.style(name, base: :base, **properties) -> style
Expand Down
8 changes: 8 additions & 0 deletions test/hexapdf/test_composer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@
end
end

it "writes the document to a string" do
pdf = HexaPDF::Composer.new
pdf.new_page
str = pdf.write_to_string
doc = HexaPDF::Document.new(io: StringIO.new(str))
assert_equal(2, doc.pages.count)
end

describe "new_page" do
it "creates a new page" do
c = HexaPDF::Composer.new(page_size: [0, 0, 50, 100], margin: 10)
Expand Down

0 comments on commit dc66c47

Please sign in to comment.