Skip to content

Commit

Permalink
Fix Renderer errors with --enable-frozen-string-literal
Browse files Browse the repository at this point in the history
There were several instances in which adding .b to string literals was
necessary to not throw this error when --enable-frozen-string-literal
was enabled. Exposed with tests.
  • Loading branch information
anthonykaufman committed Dec 6, 2024
1 parent 44f46fa commit a30667c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/combine_pdf/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,15 @@ def format_hash_to_pdf(object)
end
object[:indirect_reference_id] ||= 0
object[:indirect_generation_number] ||= 0
return "#{object[:indirect_reference_id]} #{object[:indirect_generation_number]} R".force_encoding(Encoding::ASCII_8BIT)
return "#{object[:indirect_reference_id]} #{object[:indirect_generation_number]} R".b.force_encoding(Encoding::ASCII_8BIT)
end

# if the object is indirect...
out = []
if object[:indirect_reference_id]
object[:indirect_reference_id] ||= 0
object[:indirect_generation_number] ||= 0
out << "#{object[:indirect_reference_id]} #{object[:indirect_generation_number]} obj\n".force_encoding(Encoding::ASCII_8BIT)
out << "#{object[:indirect_reference_id]} #{object[:indirect_generation_number]} obj\n".b.force_encoding(Encoding::ASCII_8BIT)
if object[:indirect_without_dictionary]
out << object_to_pdf(object[:indirect_without_dictionary])
out << "\nendobj\n"
Expand All @@ -126,11 +126,11 @@ def format_hash_to_pdf(object)
# (using LESS-THAN SIGNs (3Ch) and GREATER-THAN SIGNs (3Eh)).
out << "<<\n".b
object.each do |key, value|
out << "#{object_to_pdf key} #{object_to_pdf value}\n".force_encoding(Encoding::ASCII_8BIT) unless PDF::PRIVATE_HASH_KEYS.include? key
out << "#{object_to_pdf key} #{object_to_pdf value}\n".b.force_encoding(Encoding::ASCII_8BIT) unless PDF::PRIVATE_HASH_KEYS.include? key
end
object.delete :Length
out << '>>'.b
out << "\nstream\n#{object[:raw_stream_content]}\nendstream".force_encoding(Encoding::ASCII_8BIT) if object[:raw_stream_content]
out << "\nstream\n#{object[:raw_stream_content]}\nendstream".b.force_encoding(Encoding::ASCII_8BIT) if object[:raw_stream_content]
out << "\nendobj\n" if object[:indirect_reference_id]
out.join.force_encoding(Encoding::ASCII_8BIT)
end
Expand Down
26 changes: 26 additions & 0 deletions test/combine_pdf/renderer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,30 @@ def test_numeric_array_to_pdf

assert_equal(expected, actual)
end

def test_object_to_pdf_indirect_reference_id
actual = TestRenderer.new.test_object(
:indirect_reference_id => 1,
:indirect_generation_number => 2
)
assert_match /^1 2 obj/, actual
assert_match /endobj$/, actual
end

def test_object_to_pdf_is_reference_only
actual = TestRenderer.new.test_object(
:Pages => {
:referenced_object => { :Type => :Pages, :Count => 0, :indirect_reference_id => 3 },
:is_reference_only => true
}
)
assert_match /Pages 3 0 R/, actual
end

def test_object_to_pdf_raw_stream_content
actual = TestRenderer.new.test_object(
:raw_stream_content => 'Testing'
)
assert_match /stream\nTesting\nendstream/, actual
end
end

0 comments on commit a30667c

Please sign in to comment.